• myjobsinus (5/27/2015)


    Sorry i guess the statement 'Archive DB is a copy of the Main DB' is not correct.

    Data in the Source Database and Archive Database is different. so we want to create views on a 3rd combined DB and point all our existing 300+ reports to the 3rd database . that's the ultimate reason.

    Okay, how about this:

    DECLARE @ORIGINAL_DBNAME AS nvarchar(50) = N'DATA_WAREHOUSE';

    DECLARE @SECOND_DBNAME AS nvarchar(50) = N'TEST_DB';

    DECLARE @SQL AS nvarchar(max);

    SET @SQL = 'WITH TABLES AS (

    SELECT name FROM ' + @ORIGINAL_DBNAME + '.sys.tables

    )

    SELECT ''CREATE VIEW dbo.VW_'' + T.name + '' AS SELECT * FROM ' + @ORIGINAL_DBNAME + '.'' + T.name + '' UNION ALL SELECT * FROM ' + @SECOND_DBNAME + '.'' + T.name + '';''

    FROM TABLES AS T;

    ';

    PRINT @SQL;

    EXEC (@SQL);

    Once you modify the database names to your requirements, the result set will have one record for each table, and you can copy the entire result set into a new Query window in SSMS and then connect it to the 3rd database and run it.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)