Inserting dynamic sql resultset into table variable

  • Hi,

    I tried to insert the rows returned by executing a dynamic sql statement into a table variable, like this

    INSERT INTO @usertable EXEC ('SELECT * FROM users WHERE ... ')

    The error is very clear "EXECUTE cannot be used as a source when inserting into a table variable." Is there any work around, without having to create a table?

    Thanks.

  • Hello Senthil,

    Can you check this

    SELECT select_list INTO table_variable

    Thanks and have a nice day!!!

     


    Lucky

  • You mean like this

    EXEC ('SELECT * FROM users WHERE...some conditions...INTO @usertable')

    This is the error I get Incorrect syntax near the keyword 'into'. The select statement is built dynamically with some where conditions depending on parameters.

  • Why not

    INSERT INTO #usertable EXEC ('SELECT * FROM users WHERE ... ')

    _____________
    Code for TallyGenerator

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

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