Devin Dixon Devin Dixon - 7 months ago 13
SQL Question

Keyword GO throws errors with PHP

I've come across a problem where I am trying to restore MSSQL dumps in php on a linux server. I'm am reading in a file and then passing the file in as a query.

This works:

CREATE TABLE [dbo].[recipes](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Desc] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Directions] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CompAuthor] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CompName] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Servings] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_recipes] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)


BUT as soon as I add n a word like GO

WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO


It throws an error like

Warning: mssql_query(): message: Incorrect syntax near 'GO'


My question is what causes this error and how is it fixed?

Answer

GO is not a Transact-SQL keyword. It is a batch separator for tools like Management Studio and SQLCMD.

The answer is to not inject GO commands into the batches you are sending from your application. Just send them as separate batches (if necessary).

Source: GO (Transact-SQL)