高手,指点
更改数据库中所有表,bh字段为主键:
这样写不对:
declare @name varchar(50)
declare @sql nvarchar(4000)
DECLARE abc_cursor CURSOR FOR
SELECT name from sysobjects where xtype= 'U '
open abc_cursor
fetch next from abc_cursor into @name
while @@FETCH_STATUS =0
begin
set @sql= 'ALTER TABLE ' + @name + ' ADD CONSTRAINT bh PRIMARY KEY CLUSTERED(bh) '
exec sp_executesql @sql
fetch next from abc_cursor into @name
end
close abc_cursor
DEALLOCATE abc_cursor
请高手指点
------解决方案--------------------declare @name varchar(50)
declare @sql nvarchar(4000)
declare @i int
set @i=1
DECLARE abc_cursor CURSOR FOR
SELECT name from sysobjects where xtype= 'U '
open abc_cursor
fetch next from abc_cursor into @name
while @@FETCH_STATUS =0
begin
set @sql= 'alter table '+name+ ' alter column bh int not null '
exec(@sql)
set @sql= 'ALTER TABLE ' + @name + ' ADD CONSTRAINT bh '+cast(@i as varchar)+ ' PRIMARY KEY CLUSTERED(bh) ' exec(@sql)
set @i=@i+1
fetch next from abc_cursor into @name
end
close abc_cursor
DEALLOCATE abc_cursor