Execute statement error

  • Hi folks,

    Can anyone help me in this one??

    declare @strAux as varchar (2000),

    @RESULT AS VARCHAR(200)

    SET @strAux = 'select TOP 100 name + '','' from database1.dbo.syscolumns where id = object_id(''dabase2.DBO.table'') order by colid'

    PRINT @strAux

    SET @RESULT = EXEC(@strAux)

    ERROR:

    Incorrect syntax near the keyword 'EXEC'.

    The final intend of this query is return an string removing last , and first column name (identity column name).

    Can this be donne 🙂

    Thanks forward, for the help

  • The result will be a resultset, and you cannot assign this to a variable directly. However, you can dump the result into a temporary table, and get the result out of there, like:

    CREATE TABLE #foo ( a INT )

    DECLARE @strAux AS VARCHAR(2000)

    , @RESULT AS VARCHAR(200)

    SET @strAux = 'select 1 as xx'

    PRINT @strAux

    INSERT INTO #foo

    EXEC ( @strAux )

    -- select from the table #foo

    drop table #foo

    or a table variable:

    DECLARE @Foo TABLE ( a INT )

    DECLARE @strAux AS VARCHAR(2000)

    , @RESULT AS VARCHAR(200)

    SET @strAux = 'select 1 as xx'

    PRINT @strAux

    INSERT INTO @Foo

    EXEC ( @strAux )

    -- select from the table @Foo

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • your sqlstatement is returing a resultset !

    What are you trying to do ?

    Why are you using dynamic sql to fulfill this need ?

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

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

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