I have a query:

SET @str = 'SET QUOTED_IDENTIFIER ON' + CHAR(13)+ CHAR(10) + 'GO' + CHAR(13) + CHAR(10) + 'Select 1 from MyTable'
PRINT @str
EXEC (@str)

It returns an error:

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near 'GO'.

But if I exec the printed query in a new query editor it works fine.

I am using SQL Server 2014.

  • If you get rid of the GO, it will work. – Queue Mann Jun 22 '15 at 11:49
  • I realy need a "GO" in my dynamic SQL statement. How can I solve this issue? – Artashes Khachatryan Jun 22 '15 at 11:51
  • Why don't you simply prepare two separate strings and execute them one by one? Have you tried to add a semicolon after ON? – dezso Jun 22 '15 at 12:06
  • 2
    @ArtashesKhachatryan, why do you need a GO? It is not required in your example. GO implies separate batches and therefore requires dynamic SQL statements to be executed separately. Maybe we can help if you provide your use-case. – Dan Guzman Jun 22 '15 at 12:08
  • 2
    GO is not part of T-SQL, it is just the statement separator in SSMS and can be even changed in the settings – James Z Jun 22 '15 at 13:02