Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello ,
I have created a stored Procedure and have used Transaction Inside Try Catch block. I have put some lines of code before transaction to. if an error occurs it will be caught by the catch block. Now if i put rollback tran inside catch and error occurs before tran , then it is a mistake. how will i know if the error occurred inside tran ?

For example here is a sample procedure
Create Procedure Test
@Input varchar(10)
As
Begin
 Begin Try
    Declare @Value int;
    Set @Value = @Input

     Begin Tran
      Insert into abc value (@Value);
     
     Commit Tran
 End Try

 Begin Catch
      Rollback Tran
 End Catch     

End

<pre>

Here an error will occur because @Value is of type int and i am assigning it a string value.
SO Execution will pass on to the catch block. There i have put rollback tran, but the transaction nerver started so how can it rollback ??
Posted
Updated 16-Sep-15 20:00pm
v2

1 solution

It's because a Transaction is intended to work with more than one command: inserting related data into to tables for example.
If either one of the inserts fails, you don't want any of the data from either in the DB because it wouldn't have it's matching data. Think about an invoice: you have one table containing the invoice header (customer, invoice number, date) and a separate table containing a row for each of the individual products the customer bought. If the header fails (because the date is 31st Feb 2111111 perhaps) then you don't want the product rows in your DB because they don't have a header to join them together. Equally, if one of the products doesn't exist, you don't want the header to generate an invoice either, because some of the information is missing.

So you use a Transaction around the whole set of INSERT statements:
SQL
Begin Try
    Begin Tran
     INSERT INTO Invoices (InvoiceNumber, CustomerID, InvoiceDate) VALUES (@IN, @CID, @IDT);
     INSERT INTO InvoiceLines (InvoiceNumber, ProductID) VALUES (@IN, 1234)
     INSERT INTO InvoiceLines (InvoiceNumber, ProductID) VALUES (@IN, 1235)
    Commit Tran
End Try

Begin Catch
     Rollback Tran
End Catch
And when anything fails, the ROLLBACK undoes them all.
 
Share this answer
 
Comments
Riya-Pandey 17-Sep-15 2:41am    
Thanks for being so brief. But i already knew the purpose of the transaction. Actually My procedure is some 1000 lines of code so couldn't paste it here. I read in an article in code project yesterday that we should only use transaction when required i mean when inserting , updating data Using Procedure, Rest of the things should be outside the transaction. for instance say if i am calculating some value that should be outside transaction . once i have the value use open transaction do your work and immediately close it. i can always put the set value part from the example in the question inside tran and then there will be no problem coz tran will rollback. Just wanted to know is it the right practice or i should follow what i read in the article. if later then how will i know where the error occured ??
OriginalGriff 17-Sep-15 3:04am    
The Transaction should be around the "related" bits: the bits where it matters because if one of them fails all of them fail. And yes, you should keep a transaction open for as short a time as possible - but that doesn't mean "immediately". If the related updates are widely separated and can't be got closer, then they should still be inside the same transaction (in fact, you can nest transactions within transactions, again ensuring data integrity).

You can't just say "Open...insert...close" in all cases, because sometimes you need to process info in complex ways, and the integrity of the data can depend on the outcome of that processing. To go back to the Invoices example, you may not know the invoice is invalid until you have totalled up the cost of all items, and found that the result means the customer is above his credit limit and the order can't be processed.

Which article was it?
Riya-Pandey 18-Sep-15 3:40am    
Don't remember the article or the author name...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900