How to insert result from dynamic openquery into an existing table?

  • How can I insert the result of below dynamic openquery into an existing table

    DECLARE @TSQL varchar(8000), @VAR char(2)

    SELECT @VAR = 'CA'

    SELECT @TSQL = 'SELECT * FROM OPENQUERY(MyLinkedServer,''SELECT * FROM pubs.dbo.authors WHERE state = ''''' + @VAR + ''''''')'

    EXEC (@TSQL)

    I tried Insert into but didn't work.

    INSERT INTO TableA

    SELECT *

    FROM (EXEC (@TSQL))

  • sql84 (9/25/2014)


    How can I insert the result of below dynamic openquery into an existing table

    DECLARE @TSQL varchar(8000), @VAR char(2)

    SELECT @VAR = 'CA'

    SELECT @TSQL = 'SELECT * FROM OPENQUERY(MyLinkedServer,''SELECT * FROM pubs.dbo.authors WHERE state = ''''' + @VAR + ''''''')'

    EXEC (@TSQL)

    I tried Insert into but didn't work.

    INSERT INTO TableA

    SELECT *

    FROM (EXEC (@TSQL))

    Insert Into Table

    Execute(@Tsql)

  • Awesome.. Thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply