Incorrect Syntax Near 'Go' in SQL Server Management Studio

SQL Incorrect Syntax Near 'GO'

GO is not allowed in stored procedures. It separates batches and a procedure itself is one batch which cannot be separated.

You could use one procedure to create the database, then a second procedure to create the table.

Edit

Actually you could do it in one procedure:

CREATE PROCEDURE [dbo].[Sproc]
AS
BEGIN

EXEC ('USE [Master]; CREATE DATABASE [name]')
EXEC ('USE [Name]; CREATE TABLE [name].dbo.[Account] (id int)')

END

Incorrect syntax near 'go' in SQL Server Management Studio

SQL Server Management Studio can't handle some non printable characters.

Check the newline characters, probably you have Linux (LF) or Mac style (CR) instead of Windows style (CR and LF). You can check with any advanced text editor, for example Notepad++·

incorrect syntax near 'go' - can t have ' into double comment

This must be a bug in SQL Server Management Studio.

The GO statement is not a real statement that SQL Server knows how to handle but a convention that editors, such as Management Studio and the command line client, uses to delimit big queries into smaller pieces.

These smaller pieces are then executed one by one in order.

As such, if the GO command is actually sent to SQL Server to execute, it won't know how to handle it and thus gives you the error you got.

In Management Studio 2014, the syntax coloring is fine with the nested comments, but the presence of the apostrophe inside trips up the code that tries to delimit the query into smaller pieces.

As such I think the bug here is that the code that tries to split on the GO statement does not in fact support nested comments and thus is tripped up by the presence of them. Basically it seems to think that the comment ends after the inner */, which is wrong, and then the apostrophe is considered the start of a string that has no end that then encapsulates everything that follows, including the GO command.

Thus everything after the apostrophe is sent to SQL Server. SQL Server does support nested comments so it will see the GO command as a statement, which it doesn't support, and thus the error.


I have reported this using Microsoft Connect here: SQL Server 2014 Management Studio, when delimiting on GO command, doesn't handle nested comments.

GO causes error when used in EXEC: Incorrect syntax near 'GO'.

1) EXEC[UTE] can execute only T-SQL statements.

GO is not T-SQL statement.

GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor. SQL Server utilities interpret GO as a signal that they should
send the current batch of Transact-SQL statements to an instance of
SQL Server.

2) You could replace

     SET @SQL = '
--Drop Trigger
BEGIN TRY
DROP TRIGGER [dbo].[TR_' + @TableName + '_Audit]
END TRY
BEGIN CATCH
END CATCH
GO

--Create Trigger
CREATE TRIGGER [dbo].[TR_' + @TableName + '_Audit]

with

DECLARE @TriggerName SYSNAME;
SET @TriggerName = 'TR_' + @TableName + '_Audit';

IF EXISTS (
SELECT *
FROM sys.triggers
WHERE parent_id = OBJECT_ID(@TableName)
AND name = @TriggerName
)
BEGIN
SET @SQL = N'DROP TRIGGER [dbo].' + QUOTENAME(@TriggerName);
EXEC(@SQL);
END

SET @SQL = '
--Create Trigger
CREATE TRIGGER [dbo].[TR_' + @TableName + '_Audit]

or (better)

with

DECLARE @TriggerName SYSNAME;
SET @TriggerName = 'TR_' + @TableName + '_Audit';

IF NOT EXISTS (
SELECT *
FROM sys.triggers
WHERE parent_id = OBJECT_ID(@TableName)
AND name = @TriggerName
)
BEGIN
SET @SQL = N'CREATE TRIGGER [dbo].' + QUOTENAME(@TriggerName) + 'ON ' + @TableName + ' AFTER INSERT, UPDATE, DELETE AS BEGIN SELECT NULL END';
EXEC(@SQL);
END

SET @SQL = '
--Alter Trigger
ALTER TRIGGER [dbo].[TR_' + @TableName + '_Audit]

Note: The object's name should be NVARCHAR(128) or SYSNAME.

Incorrect syntax near 'GO' using SqlCommand

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go?view=sql-server-ver15#remarks

SQL ExecuteReader throws Incorrect syntax near GO

GO is NOT a T-SQL command - therefore you cannot have it in T-SQL statements being executed from PowerShell.

GO is a batch separator used by SQL Server Management Studio.

You need to break up that statement into several individual statements yourself and execute them one by one.

PHP PDO Incorrect syntax near GO, MSSQL & sql_srv

Have found an answer here by Jon Galloway

GO isnt valid T-SQL, its a command used by SQLCMD and other utilities, and parsed before execution.

It looks like there are a few options.

1) Execute the script using OSQL / command line

2) Split the script at each GO separator, then run them in sequence

3) If using .NET you can look at using SQL Server Management Objects:
Server.ConnectionContext.ExecuteNonQuery()

This parsers T-SQL statements and "gets" the GO statement as a batch separator.



Related Topics



Leave a reply



Submit