Sybase: Incorrect syntax next to "go" in "IF EXISTS" block
This is my sql statement
IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE') drop table dbo.PNL_VALUE_ESTIMATE go isql gives an error message
Msg 102, Level 15, State 1: Server 'DB_SERVER', Line 3: Incorrect syntax near 'go'. But the sql expression looks correct for me. What's wrong?
Sybase version is 15
Try the following:
IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE') drop table dbo.PNL_VALUE_ESTIMATE go or that:
IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE') BEGIN drop table dbo.PNL_VALUE_ESTIMATE END go or that:
IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE') BEGIN select 1 END go Does any work work?
GO not a T-SQL keyword, but an editor.
SMSS (between others) uses it as a โseparationโ between batches of commands that it sends to the database server. Executing it inside a stored procedure or even a script file will not work.
edit: It may work with SyBase, but I think that in this case it should be uppercase.
From the documentation, the GO statement is the command of the editor used, not SQL :
GO is not a Transact-SQL statement; it is a team recognized by sqlcmd and the osql utility and SQL Server Management Studio code editor.
However, Sybase is also an editor that supports the GO statement.
I had the same problem but with SQL Management Studio. The problem is that the editor does not support mixed-line types around certain statements - GO is one of them. In Management Studio, for example, only Windows-style lines (CR + LF) are allowed, and if I used the Linux (LF) format, it would produce the same error as yours.
In text editors such as Notepad ++ (which I use), there is an option for what type of trailing characters you use by default (Windows, Linux, Mac (CR)).
Try checking which newlines are used in your instructions to determine if this can solve the problem.
Should an object reference have
dbo..PNL_VALUE_ESTIMATE
because you did not specify the database name, and if you included the obj owner, you need to ... skip the db name?
I would go:
EXEC ('DROP TABLE dbo..PNL_VALUE_ESTIMATE')
in the true part, since DROP TABLE is always compiled, and if the table does not exist, you will still fail.
Do you even need a dbo? If your sql always works like dbo, just leave it.