Link to home
Start Free TrialLog in
Avatar of jpguillebaud
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.
Avatar of cmhunty
cmhunty

You can get the table names by:
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
ASKER CERTIFIED SOLUTION
Avatar of cmhunty
cmhunty

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jpguillebaud

ASKER

Thanks Chris,
Your script works perfectly!