Home >Database >Mysql Tutorial >Why Does My Dynamic SQL Query Cause a 'Incorrect syntax near 'go'' Error in SQL Server?
Challenge:
Executing a dynamic SQL query with a GO statement triggers the error "Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'go'".
Analysis:
GO is not a valid Transact-SQL statement but rather a command recognized by sqlcmd, osql, and SQL Server Management Studio Code Editor.
Solution:
To resolve this error, you must remove all instances of GO from your dynamic SQL. Running the query with GO removed should resolve the syntax error.
Code Sample:
DECLARE @script VARCHAR(MAX), @script1 VARCHAR(MAX); SET @script = ' create table ali(id decimal(10,0)); drop table ali; '; SET @script1 = ' create table ali(id decimal(10,0)); drop table ali; '; EXEC (@script); EXEC (@script1);
Note:
The example code provided in the question was for illustration purposes only. The dynamic SQL you execute may contain different queries but the solution remains the same: remove instances of GO.
The above is the detailed content of Why Does My Dynamic SQL Query Cause a 'Incorrect syntax near 'go'' Error in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!