exec commands stored in a variable

  • When executing the query which is stored in a variable like that :

    exec (@test_SQL)

    This is alright for simple query, but this method is not work for a query with “go”. Syntax error will come out.

    declare @test_SQL varchar (max)

    set @test_SQL = 'print(''aaa'') ' + char(10)+ 'go print(''bbb'')'

    print @test_SQL

    exec (@test_SQL)

    Any idea to deal with this?

    Thanks in advance.

  • "GO" isn't actually a part of T-SQL and it can't be used the way you want in Dynamic SQL...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • GO is a transaction seperator, but it is a query window (and osql/sqlcmd) specific code.

  • What are you trying to accomplish? Why does it need the GO statement separator?

    If you need the GO statement separator then you could do two exec calls rather than one. However, I'd wager that you don't need it - try to remove the GO and see if it goes. 😀

  • o... thanks a lot !!!

    exec 2 query separately is a good idea, thanks to knock me up !!!

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

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