SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Control flow in script with more then one batch
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Gold
Starting Member

Portugal
4 Posts

Posted - 07/14/2007 :  17:38:35  Show Profile  Reply with Quote
The situation is the following: I have a script that contains more then one batch of SQL DML statements and I want to interrupt script execution if any one of the SQL DML statements returns zero rows affected.

Thanks in advance to anyone that has some insight about this situation.

rmiao
Flowing Fount of Yak Knowledge

USA
7266 Posts

Posted - 07/14/2007 :  20:04:49  Show Profile  Reply with Quote
Check @@rowcount after every dml, exit the script if result is 0.
Go to Top of Page

Gold
Starting Member

Portugal
4 Posts

Posted - 07/15/2007 :  06:53:52  Show Profile  Reply with Quote
That's precisely what I'm looking for. A way to exit the entire script and not just the current executing batch, but I don't know the code to do that.

So if you could tell me the T-SQL code to do the exiting part, that would be great.
Go to Top of Page

rmiao
Flowing Fount of Yak Knowledge

USA
7266 Posts

Posted - 07/15/2007 :  20:41:29  Show Profile  Reply with Quote
Try goto statement to jump to the end.
Go to Top of Page

Gold
Starting Member

Portugal
4 Posts

Posted - 07/16/2007 :  04:46:33  Show Profile  Reply with Quote
The GOTO cannot jump to a label outside of the batch so does not accomplish what I'm trying to do, but thanks for the replies anyway.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 07/16/2007 :  10:23:13  Show Profile  Reply with Quote
Create a temp table at the beginning of the script.

When you reach your exit condition, set a flag in the table.

Check the table in the rest of your batches, and exit if the condition is set.



CODO ERGO SUM
Go to Top of Page

Gold
Starting Member

Portugal
4 Posts

Posted - 07/16/2007 :  13:23:48  Show Profile  Reply with Quote
Thanks, that did the trick.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000