Gag*_*age 10 sql t-sql sql-server
该列是"CreatedDateTime",这是非常明显的.它跟踪记录提交的任何时间.我需要在超过100个表中更新这个值,并且宁愿有一个很酷的SQL技巧,可以在几行中完成,而不是复制粘贴100行,唯一的区别是表名.
任何帮助将不胜感激,很难找到任何更新表中的列(这很奇怪,可能是不好的做法反正,我很抱歉).
谢谢!
编辑:这篇文章向我展示了如何获得所有具有该列的表
如果这有任何帮助.无论如何,这对我来说都是一个开始.
Eri*_*icZ 10
如果这是一次性任务,只需运行此查询,将结果复制并粘贴到查询窗口并运行它
Select 'UPDATE ' + TABLE_NAME + ' SET CreatedDateTime = ''<<New Value>>'' '
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'CreatedDateTime'
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用光标:像这样
declare cur cursor for Select Table_Name From INFORMATION_SCHEMA.COLUMNS Where column_name = 'CreatedDateTime'
declare @tablename nvarchar(max)
declare @sqlstring nvarchar(max)
open cur
fetch next from cur into @tablename
while @@fetch_status=0
begin
--print @tablename
set @sqlstring = 'update ' + @tablename + ' set CreatedDateTime = getdate()'
exec sp_executesql @sqlstring
fetch next from cur into @tablename
end
close cur
deallocate cur
Run Code Online (Sandbox Code Playgroud)