[英]Adding a column to all user tables in t-sql
I need to add a delete flag column to all 40 user tables in a database. 我需要将删除标志列添加到数据库中的所有40个用户表。 I could write a script to loop through sys.tables, but I thought I'd check and see if anyone has either a better solution, or pre-created sql for this scenario. 我可以编写一个脚本来遍历sys.tables,但是我想我会检查是否有人对此方案有更好的解决方案或预先创建的sql。
有一个未记录但众所周知的存储过程sp_msforeachtable:
exec sp_msforeachtable 'alter table ? add flag bit not null default 0';
No, it's a manual loop. 不,这是一个手动循环。
Or you could build up a single SQL statement of course... 或者,您当然可以构建一条SQL语句...
SELECT
'ALTER TABLE ' + T.name + ' ADD foo int NULL'
FROM
sys.tables AS T
WHERE
T.is_ms_shipped = 0
Or the undocumented 或无证件
EXEC sys.sp_MSforeachtable 'ALTER TABLE ? ADD foo int NULL'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.