批量设置主键和自增长

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

posted @ 2014-09-28 14:42  东少  阅读(231)  评论(0)    收藏  举报