ALTER TABLE(s) within a LOOP
(For example) I want to add a "DateStamp" column to all of my tables, in one hit. The following query provides me with a list of those tables that are missing the column...
SELECT [NAME]
FROM sysobjects
WHERE [TYPE] = 'U'
AND id NOT IN
(
SELECT SO.[ID]
FROM sysobjects SO
JOIN syscolumns SC ON SC.ID = SO.ID
WHERE SC.[NAME] = 'DateStamp'
)
And I am aware that I can use this in the following format:
WHILE (SELECT COUNT(*)) > 0
BEGIN
/*LOOP*/
END
In which I know how to word the "SELECT COUNT(*)" to create the loop and I also know the code the first part of the BEGIN to capture the table names into a variable, with:
SELECT TOP 1 @table = [name].....
But, that then doesn't work with:
ALTER TABLE @table ADD 'DateStamp', etc.,
Could someone point me in the right direction here, preferably for a simple solution (as I know that you can completely control a database from within the sys... tables, but I am cautious of how easy it would be to make a mistake there) ?
Regards,
Sean Anderson
__________________
Regards,
Sean Anderson
|