jpguillebaud
asked on
Adding the same columns to every existing table in a SQL database
I have been asked to add the same 2 columns (CUID and LUID) to every table in a database and script these changes. I am currently modifying each table one by one and copying the resulting change script into the final upgrade script. However, since there are 253 tables it will take a while to do every one! So, I am wondering if there is a faster/simpler way to do this - I am wondering if there is some kind of FOREACH statement I could use which would call the SQL ALTER TABLE function for each.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Chris,
Your script works perfectly!
Your script works perfectly!
SELECT name FROM sysobjects WHERE xtype = 'U'
You could then put this into a cursor and at each step, create a dynamic SQL alter table statement using the table name and execute.
Chris