Question

I need to add 3 new columns to a table named Requirements in all the databases (of same instance). I searched in net to find sp_MSforeachdb can be used to execute same query on multiple databases, but could not find any example with a alter command.

Thanks in advance

Was it helpful?

Solution

Presuming the tables will all be in the same schema then using sp_msforeachdb

EXEC sp_msforeachdb '
    IF DB_ID(''?'') > 4
    BEGIN
        IF EXISTS(SELECT 1 FROM [?].INFORMATION_SCHEMA.TABLES 
                    WHERE TABLE_NAME=''Requirements'' AND TABLE_SCHEMA=''dbo''
                    AND TABLE_TYPE=''BASE TABLE'')
        BEGIN
            ALTER TABLE [?].dbo.Requirements
            ADD col1 INT, col2 INT, col3 INT
        END
    END    
'

Aaron Bertrand wrote a superior version here that you may want to use.

Another way with dynamic sql

DECLARE @sql VARCHAR(MAX) = CAST((SELECT 'IF EXISTS(SELECT 1 FROM [' + name + '].INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=''Requirements'' AND TABLE_SCHEMA=''dbo'' AND TABLE_TYPE=''BASE TABLE'')' + CHAR(10) 
                                          + '    ALTER TABLE [' + name + '].[dbo].[Requirements] ADD Col1 INT, Col2 INT, Col3 INT' + CHAR(10) + CHAR(10)
                                  FROM master.sys.databases
                                  WHERE database_id > 4
                                  FOR XML PATH('')) AS NVARCHAR(MAX))

PRINT @sql
--EXEC(@sql)

If you have to take into consideration the possibility that the table is not in dbo or a common schema then you can use sp_msforeachdb to retrieve the schema information along the lines of

CREATE TABLE ##tmp (DatabaseName SYSNAME, SchemaName SYSNAME, TableName SYSNAME)

EXEC sp_msforeachdb '
    IF DB_ID(''?'') > 4
    BEGIN
        INSERT INTO ##tmp (DatabaseName, SchemaName, TableName)
        SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME
        FROM [?].INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME = ''Requirements''
        AND TABLE_TYPE=''BASE TABLE''
    END    
'

DECLARE @sql VARCHAR(MAX) = CAST((SELECT 'ALTER TABLE [' + DatabaseName + '].[' + SchemaName + '].[' + TableName + '] ADD Col1 INT, Col2 INT, Col3 INT' + CHAR(10) + CHAR(10)
                                  FROM ##tmp
                                  FOR XML PATH('')) AS NVARCHAR(MAX))

PRINT @sql
--EXEC(@sql)

DROP TABLE ##tmp

OTHER TIPS

From mysql command line:

ALTER TABLE database1.table_name ADD column_name column-name;
ALTER TABLE database2.table_name ADD column_name column-name;
..

The following code would generate commands for each database:

select 'ALTER TABLE [' + d.name + '].[dbo].[table_name] ADD column_name column-name;' as cmd
from sys.databases d
where d.name NOT IN ( 'tempdb', 'msdb', 'master','model');

You could also write a cursor to loop over these and execute them, but I would advise looking at what you're executing and making sure you aren't updating the wrong databases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top