Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 519
seraphym Avatar asked Nov 22 '09 19:11

seraphym


1 Answers

There is an undocumented but well known stored procedure sp_msforeachtable:

exec sp_msforeachtable 'alter table ? add flag bit not null default 0';
like image 85
Remus Rusanu Avatar answered Sep 16 '22 12:09

Remus Rusanu