批量设置主键和自增长
BEGIN
DECLARE @tableName varchar(100);
DECLARE test_curse CURSOR FAST_FORWARD FOR
select name from sysobjects where type='u' and name like 'R%'
OPEN test_curse;
FETCH NEXT FROM test_curse INTO @tableName;
WHILE @@fetch_status=0
BEGIN
declare @PK varchar(100) ;
select @PK=name from sysobjects where xtype='PK' and parent_obj=object_id(@tableName);
PRINT @PK;
exec ('alter table '+ @tableName+' drop CONSTRAINT '+@PK);
EXEC('alter table '+ @tableName+' drop column ID');
EXEC('ALTER TABLE '+ @tableName+' ADD ID int IDENTITY(1,1) PRIMARY KEY');
FETCH NEXT FROM test_curse INTO @tableName;
END;
CLOSE test_curse;
DEALLOCATE test_curse;
END

浙公网安备 33010602011771号