帮忙写一个游标!!!!!!
SELECT COLUMN_NAME,CONSTRAINT_NAME,A.TABLE_NAME,b.TABLE_NAME FROM
(Select COLUMN_NAME,TABLE_NAME From INFORMATION_SCHEMA.COLUMNS WHERE Table_Name
IN (SELECT Name FROM BMS_HT..SysObjects Where XType='U' )
AND COLUMNPROPERTY(Object_ID(table_Name),Column_Name,'IsIdentity')=1)a ----获取表中的自动增长列
left join
(SELECT CONSTRAINT_NAME,TABLE_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME IN (Select TABLE_NAME From INFORMATION_SCHEMA.COLUMNS WHERE Table_Name
IN (SELECT Name FROM BMS_HT..SysObjects Where XType='U' )
AND COLUMNPROPERTY(Object_ID(table_Name),Column_Name,'IsIdentity')=1) )b ---获取表中的主键
on a.TABLE_NAME=b.TABLE_NAME
ALTER TABLE tablename DROP CONSTRAINT pkname
ALTER TABLE tablename ALTER COLUMN id decimal(18, 0)
alter table tablename add constraint pkname primary key(id)
要求是修改数据库中所有表的自动增长列的长度改变成decimal(18, 0上面SELECT是我写的查出数据库里面所有的自动增长列、主键和表名、不知道怎么用参数放到alter里面 请各位帮忙啊!!!!!
------解决方案--------------------游标??????
现在还有谁在用游标啊...
declare @tbname varchar(50),@colname varchar(50)
declare @sql nvarchar(3000)
while exists(select 1
from sysobjects a inner join syscolumns b on a.id=b.id and columnproperty(b.id, b.name, 'IsIdentity') = 1 and a.type='U'
where b.xusertype<>106)
begin
select top 1 @tbname=a.name,@colname=b.name
from sysobjects a inner join syscolumns b on a.id=b.id and columnproperty(b.id, b.name, 'IsIdentity') = 1 and a.type='U'
where b.xusertype<>106
set @sql='alter table '+@tbname+' alter column '+@colname+' decimal(18,0)'
execute(@sql)
end
------解决方案--------------------
DECLARE @sql NVARCHAR(MAX)
SELECT
@sql=ISNULL(@sql+NCHAR(13)+NCHAR(10),'')
+CASE
WHEN d.name IS NOT NULL THEN 'Alter Table '+a.name+' drop constraint '+d.name
ELSE ''
END+NCHAR(13)+NCHAR(10)
+'Alter Table '+a.name+' Alter Column '+b.name+' Numeric(18,0)'+NCHAR(13)+NCHAR(10)
+'Alter Table '+a.name+' Add constraint '+d.name+' primary key('+b.name+')'
FROM sys.objects AS a
INNER JOIN sys.columns AS b ON a.object_id=b.object_id
LEFT JOIN sys.index_columns AS c ON b.object_id=c.object_id AND b.column_id=c.column_id
LEFT JOIN sys.indexes AS d ON c.object_id=d.object_id AND c.index_id=d.index_id AND d.is_primary_key=1
WHERE a.type='U'
AND b.is_identity=1
EXEC ( @sql )