向t-sql中的所有用户表添加列 [英] Adding a column to all user tables in t-sql

查看:143
本文介绍了向t-sql中的所有用户表添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在数据库中的所有40个用户表中添加一个删除标志列。我可以编写一个脚本来循环使用sys.tables,但是我想我会检查一下是否有任何一个更好的解决方案,或者为这种情况预先创建sql。

I need to add a delete flag column to all 40 user tables in a database. 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.

推荐答案

有一个未记录的但着名的存储过程sp_msforeachtable:

There is an undocumented but well known stored procedure sp_msforeachtable:

exec sp_msforeachtable 'alter table ? add flag bit not null default 0';

这篇关于向t-sql中的所有用户表添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆