Bloggin in the UK RSS 2.0
 Saturday, February 11, 2006

I was compiling a release script the other day, and needed to check if all data items in a column were unique before adding a Unique constraint to a table. I wanted a way to stop Query Analyzer (QA) from executing the batch if the check revealed there were duplicate items in the column. I tried a few obvious things like Return and raiserror but these didn't work. I then realised that the issue lay with the GO command. I found this in BOL "GO is not a Transact-SQL statement; it is a command recognized by the osql and isql utilities and SQL Query Analyzer." unfortunately there isn't a corresponding STOP command so you cannot easily perform a test in T-SQL and then tell Query Analyzer (QA) to stop processing the rest of the batch.

I did however find a dirty way around this problem. Using a While loop and the Print command I was able to repeatedly display a message to the user explaining the problem and instructing them to cancel the execution. Not very elegant but it did work.

BOL Search Terms: Batches, Batch Processing

Saturday, February 11, 2006 1:34:33 PM (GMT Standard Time, UTC+00:00)  #    Comments [3] -
SQL Server
Friday, March 16, 2007 11:54:45 AM (GMT Standard Time, UTC+00:00)
Hi Charlie,
Could you please explain how user will see that message before execution stops ?
Friday, March 16, 2007 3:13:15 PM (GMT Standard Time, UTC+00:00)
Hi ykogun,
It will be displayed in the results pane in Sql Query Analyzer.
Charlie
Tuesday, August 14, 2007 4:02:55 PM (GMT Standard Time, UTC+00:00)
One of the guys I work with told me about using a RETURN statement to stop batch execution. I'm using MS SQL Server 2000 and it works great.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Charlie Barker
Sign In
Statistics
Total Posts: 144
This Year: 25
This Month: 1
This Week: 0
Comments: 53
Themes
Pick a theme:
All Content © 2008, Charlie Barker
DasBlog theme 'Business' created by Christoph De Baene (delarou)