批量修改表中字段的值

DECLARE @tableName varchar(50),@tableCount int,@index int
SET @index=1
set @tableCount = (SELECT count(*) FROM sys.objects WHERE type = 'u')
WHILE @index<=@tableCount
begin
 SELECT TOP (@index) @tableName = name FROM sys.objects WHERE type = 'u'
 IF exists (SELECT * FROM sys.columns WHERE name='Createdate' and OBJECT_ID=object_id(@tableName))
 begin
  EXEC('update '+@tableName+' set CreateDate = getdate()')
  PRINT @tableName
 end
 SET @index=@index+1
END

你可能感兴趣的:(批量)