| Author |
Topic |
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-03 : 15:24:32
|
Hi all,the following is just a part of a large .sql file that I execute from Cmd with the utility ISQL in multiple machine (win2000) and SQL 2000.I run to an error every time I add Go keyword.use DBgoset nocount ongoIF @@VERSION LIKE '%6.5%' BEGINprint 'Im 6.5 system'ENDELSEBEGINIF EXISTS (select * from dbo.sysobjects where name='table1')begin DROP table dbo.table1endcreate table table1 (value1 varchar(255) null)goEND ErrorServer: Msg 170, Level 15, State 1, Line 14Line 14: Incorrect syntax near ')'.Server: Msg 156, Level 15, State 1, Line 95Incorrect syntax near the keyword 'END'. when I remove the Go keyword it will be parsed successfully.Any idea please? --------------------------Get rich or die trying-------------------------- |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-03 : 15:28:11
|
GO is a batch separatorso if you add it before END it starts a new batch with just an orphaned END statement which is cause of the error.if you want to add it, it should be likeuse DBgoset nocount ongoIF @@VERSION LIKE '%6.5%' BEGINprint 'Im 6.5 system'ENDELSEBEGINIF EXISTS (select * from dbo.sysobjects where name='table1')begin DROP table dbo.table1endcreate table table1 (value1 varchar(255) null)ENDgo ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-03 : 15:52:31
|
Thanks visakh16 a lot, how about this sample:BEGINIF EXISTS (select * from dbo.sysobjects where name='import')begin DROP table dbo.importendGOcreate table import (value1 varchar(255) null)IF EXISTS (select * from dbo.sysobjects where name='audit')begin DROP table auditendENDGO I have placed the the GO after end and still cause an error!I guess I'm missing something I didn't understand !! I just want to know where to put them EXACTLY to optimize the script because these Goes are causing so many errors.--------------------------Get rich or die trying-------------------------- |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-03 : 15:56:36
|
here you've just opposite. you've an orphaned BEGIN without end.it should beBEGINIF EXISTS (select * from dbo.sysobjects where name='import')begin DROP table dbo.importendEND GOBEGIN create table import (value1 varchar(255) null)IF EXISTS (select * from dbo.sysobjects where name='audit')begin DROP table auditendENDGO and if they all have to be in same block then there's no need of GO in between------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-08-03 : 16:01:59
|
If you take everything between two adjacent GO statements (or between the beginning of the script and the first GO statement, or the last GO statement and the end of the script) and paste it on a new query window, it should be able to stand on its own. BEGINIF EXISTS (select * from dbo.sysobjects where name='import')begin DROP table dbo.importendGO create table import (value1 varchar(255) null)IF EXISTS (select * from dbo.sysobjects where name='audit')begin DROP table auditendENDGO
Since in your code one BEGIN/END pair spans across a GO, the parser does not like that. So remove the first begin and the last end, or add additional END and BEGIN to match. BEGINIF EXISTS (select * from dbo.sysobjects where name='import')begin DROP table dbo.importendENDGO BEGINcreate table import (value1 varchar(255) null)IF EXISTS (select * from dbo.sysobjects where name='audit')begin DROP table auditendENDGO
Edit: Or what Visakh said :) |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-08-03 : 16:05:06
|
| Just out of curiosity, have you tested this without any GO separators? I did and it runs fine. Don't use GO unless you absolutely need it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-03 : 16:12:51
|
quote: Originally posted by robvolk Just out of curiosity, have you tested this without any GO separators? I did and it runs fine. Don't use GO unless you absolutely need it.
yep that's the pointI think the GO in posted code came from copy paste of CREATE TABLE script from some other code------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-03 : 16:19:21
|
That's clear, thank you all --------------------------Get rich or die trying-------------------------- |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-03 : 16:22:23
|
| @robvolkIt is running without GO, however, when I run it from the cmd line. I think it causes no results.Somehow...the script isn't acting properly when you call a .SQL file without GO.But Thanks,;)--------------------------Get rich or die trying-------------------------- |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-08-03 : 16:46:54
|
| Your script isn't SELECTing any data, it has nothing to do with the GO separator. The only output to be expected is the PRINT statement if the version is 6.5. Otherwise that script will have no output at all. |
 |
|
|
|