exec problem in sql 2005

Posted by IordanTanev on Stack Overflow See other posts from Stack Overflow or by IordanTanev
Published on 2010-04-02T07:00:57Z Indexed on 2010/04/02 7:03 UTC
Read the original article Hit count: 349

Filed under:
|
|
|

Hi,
i have the situation where i have two databases whith same structure. The first have some data in its datatables. I need to create a script that will transfer the data from the first database to the second. I have created this script.

    DECLARE @table_name nvarchar(MAX),
            @query nvarchar(MAX)

    DECLARE @table_cursor CURSOR 

    SET @table_cursor = CURSOR FAST_FORWARD
    FOR
    Select TABLE_NAME FROM INFORMATION_SCHEMA.TABLES

    OPEN @table_cursor
    FETCH NEXT FROM @table_cursor
    INTO @table_name

    WHILE @@FETCH_STATUS = 0
        BEGIN

            SET @query = 'INSERT INTO ' + @table_name + ' SELECT * FROM MyDataBase.dbo.' + @table_name  
            print @query
            exec @query

            FETCH NEXT FROM @table_cursor
            INTO @table_name
        END

    CLOSE @table_cursor
    DEALLOCATE @table_cursor 

The problem is that when i run th script the "print @query" statement prints statement like this

INSERT INTO table SELECT * FROM MyDataBase.dbo.table

When i copy this and run it from Management studio it works fine. But when the script trys to run it with exec i get this error

Msg 911, Level 16, State 1, Line 21
Could not locate entry in sysdatabases for database 'INSERT INTO table  SELECT * FROM MPDEV090314'. No entry found with that name. Make sure that the name is entered correctly.

Hope someone can tell me whot is wront with this. Best Regards,
Iordan Tanev

© Stack Overflow or respective owner

Related posts about tsql

Related posts about ms