Solved

Stored Procedure Commit.

Posted on 2001-07-24
6
829 Views
Last Modified: 2008-03-17
Does a SQL 7.0 stored proc rollback if part of the procedure fails or do you have to use Begin Trans..., Commit Trans... and Rollback Trans...

I am just curious if I don't include any Transaction statements if my stored procedure has 4 different DELETE calls and say the third one fails will the first two be reversed?

A reference for the answer would also be appreciated.

Thanks!
0
Comment
Question by:MTroutwine
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
deestuar earned 50 total points
ID: 6313436
not unless you wrap it in transactions
sample included. You can use a label with goto lable when the error occurs and have the rollback code at the end


CREATE PROCEDURE sp_delCompOccasion
@p_nIdCompany int,
@p_nIdOccasion int
 AS

DECLARE @error int,
         @lcMessage nvarchar(255),
         @lcProcedure nvarchar(255),
         @strCompany nvarchar(255)

SELECT @error=0,
     @lcMessage='',
     @lcProcedure=OBJECT_NAME(@@procid)

IF EXISTS(SELECT *
            FROM compOccasion
           WHERE idCompany=@p_nIdCompany
          AND idOccasion=@p_nIdOccasion)
BEGIN

BEGIN TRANSACTION delCompOcc

     DELETE FROM  compOccasion
     WHERE idCompany=@p_nidCompany
      AND  idOccasion=@p_nIdOccasion

     SELECT @error=@@error

     IF @error<>0
     BEGIN

          ROLLBACK TRANSACTION delCompOcc
          SET @strCompany=convert(nvarchar,@p_nIdCompany)

          EXEC sp_writeto_errorlog @strCompany,
                         @error,
                         'Unable to delete data in compOccasion',
                         @lcProcedure

          RETURN @error          
     END

COMMIT TRANSACTION delCompOcc
END
RETURN @error



0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6315213
depends how your connection is set up.
If you set it to use implicit transactions then you need to explicitly commit at the end. If not then you need to start a transaction and commit it or rollback. In both cases you need to check the error status after each statement. The error status will be reset at the next statement so you can not leave it until the end
not that
delete tbl1
if @@rowcount = 0 return
if @@error <> 0 ...
will not work because the if @@error will lose the error status.
To do this put @@error and @@rowcount into variables after the delete.


begin tran
   delete tbl1
   if @@error <> 0
   begin
     raiserror('failed delete tbl1',16,-1)
     rollback tran
     return
   end
   delete tbl2
   if @@error <> 0
   begin
     raiserror('failed delete tbl2',16,-1)
     rollback tran
     return
   end
   delete tbl3
   if @@error <> 0
   begin
     raiserror('failed delete tbl3',16,-1)
     rollback tran
     return
   end
   delete tbl4
   if @@error <> 0
   begin
     raiserror('failed delete tbl4',16,-1)
     rollback tran
     return
   end
commit tran
0
 
LVL 3

Author Comment

by:MTroutwine
ID: 6315309
The problem I am having is I have a stored proc that deletes records from several different tables, one delete after another, etc.  Using the Begin Transaction statement apparently locks the entire table as I am not deleting any rows that are related.  What is occuring is if two users delete at the same time than I get a deadlock and one of the two is the victom (yuck).  

If I remove the Begin Transaction it works fine, but I want to confirm that it worked fine and if not do a rollback....

So is there a way to do this without using Begin Transaction or setting a different isolation level.

Thanks.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 18

Expert Comment

by:nigelrivett
ID: 6315327
You can use the with rowlock hint to stop page/table locks being taken. Will still take locks on the indexes though.

delete tbl (rowlock)

You can reduce deadlocks by making sure that good indexes are available for all statements to reduce the number of pages accessed and making sure that deletes, inserts, updates are actioned in the same order so that if two spids are getting table locks then the other will wait for the first rather than getting a deadlock.
Another way of stopping deadlocks at the expense of slowing updates is to have a resource table and every SP tha wishes to update contentious tables takes an exclusive lock (inside a transaction) to make sure that all other spids wait.

begin tran
select * from resourcetbl (tablockx, holdlock) where 1 = 0
delete
...
0
 
LVL 3

Author Comment

by:MTroutwine
ID: 6315350
I tried the rowlock, but let me take a look at the indexes tomorrow and give you a shout.

I really appreciate your help on this!

:>)
0
 
LVL 3

Author Comment

by:MTroutwine
ID: 6356974
I worked around this by setting the Isolation level in the stored procedure.  Thanks for your help...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

864 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

25 Experts available now in Live!

Get 1:1 Help Now