Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a command string with GO at the end

Tags:

sql

sql-server

I need to execute a command string with using GO command at the end like

exec('SELECT * FROM tblTmp where Id = 1 GO')

After executing I have

Incorrect syntax near 'GO'.

If I execute exec('SELECT * FROM tblTmp GO') everything is OK

What's the issue here?

Thank you.

like image 775
Vyacheslav Avatar asked Mar 28 '11 16:03

Vyacheslav


1 Answers

GO is not an SQL command, it is a batch separator understood by clients like osql, sqlcmd, and SSMS but not the engine itself.

If you write something like this in SSMS:

SELECT  1
GO
SELECT  2
GO

, the engine sends two batches with one statement in each instead of one batch with two statements.

In your second query, GO is treated as an alias to the table tblTmp.

Just remove GO from your query.

like image 151
Quassnoi Avatar answered Nov 04 '22 08:11

Quassnoi