Solved

Question on NON EXISTS/RETURN and GO

Posted on 2013-02-04
2
213 Views
Last Modified: 2013-02-04
I have a block of script like this:

IF NON EXISTS (select 1 from client where clientid = 1)  <------- LINE1
RETURN;   <-------- LINE2

IF column 1 does not exists alter table ......
go

IF table_A not exists  create table_A
go

The problem is that after LINE2 is exec, it continue to EXEC the rest of the scripts. The intention is to exit the script if LINE1 returns true.
BTW, I cannot use EXIT, because it is called by other script, which will handle the exception differently.

Because the rest of script is so large, I do wish to keep the GO between the script lines.

Is there anyone can help me figure out what goes on here?

Thanks in advance.
0
Comment
Question by:chuang4630
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 38852038
The problem is that after LINE2 is exec, it continue to EXEC the rest of the scripts. The intention is to exit the script if LINE1 returns true.
...
Because the rest of script is so large, I do wish to keep the GO between the script lines.

If you cannot remove the GO, then there is not much you can do about it.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 500 total points
ID: 38852059
From SQL Server's BOL (my emphasis):
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. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now