我正在使用SQL Server.我想将一个名为[DateCreated]的列添加到多个表中.是否可以使用单个语句将此列添加到数据库中的所有表中?
我偶然发现了Joe Steffaneli的回答,他在回答中提出了一个查询,该查询又返回包含Alter表语句的行.查询如下:
select 'alter table ' + quotename(s.name) + '.' + quotename(t.name) + ' add [DateModified] datetime'
from sys.columns c
inner join sys.tables t
on c.object_id = t.object_id
inner join sys.schemas s
on t.schema_id = s.schema_id
left join sys.columns c2
on t.object_id = c2.object_id
and c2.name = 'DateModified'
where c.name = 'DateCreated'
and t.type = 'U'
and c2.column_id is null /* DateModified column does not already exist */
Run Code Online (Sandbox Code Playgroud)
有什么办法可以执行返回的行吗?对不起英文.