Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Transactions - rollback on error and raise error
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

129 Posts

Posted - 03/01/2013 :  12:58:07  Show Profile  Reply with Quote
using .NET here and also SPROCS

if in a SPROC, an insert fails, I want to be able to rollback that transaction so none of the inserts are commited but also still raise an error back to .NET

how can I do this?



INSERT INTO [Table] (cols) VALUES (vals)
INSERT INTO [Table2] (COLS) VALUES (@vals) -- error raised

I want to rollback the data insert into the previous inserts but still raise an error back (same original error)


James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 03/01/2013 :  13:26:13  Show Profile  Reply with Quote
You should use a try/catch block in combination with begin tran/commit/rollback. Take a look at this page - it has examples of how to do that. (look for examples (b) and (c))

Now, if you want to rethrow the error, you can do a raiserror in the catch block - which will then be returned to .Net. If you are on SQL 2012, use throw instead of raiserror. This page has an example of raiserror within a catch block:
Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000