Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Transactions


Transactions

Author
Message
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47189 Visits: 44356
Dave Hall (1/25/2013)
I'm calling the 1st Proc like before and the transaction errors are gone (yay) BUT the Delete statement still executes and is not rolled back (boo).

I thought that if the call to the Proc was within a transaction then it would Rollback everything within (including actions carried out by a called SP)?


Sure it will, but you have nothing that checks to see if the Insert throws an error or not. There's no checks for @@Error after the insert, so no one notices that it threw an error. Hence it looks, to your code, that the inner proc ran fine and hence gets committed.

@@Error - the error code of the previous statement that ran.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Dave Hall
Dave Hall
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 14
Thank you, both for your help. I think perhaps I was trying to do too much for my first attempt at this!

Here is how I've changed the code and it now works:

SP1

ALTER PROCEDURE [dbo].[usp_Permissions_Update_Denied_UserPermissions]
@ColleagueID int,
@PermissionID int
AS
BEGIN
   -- SET NOCOUNT ON added to prevent extra result sets from
   -- interfering with SELECT statements.
   SET NOCOUNT ON;

BEGIN TRANSACTION

Declare @RetVal int

--if the permission exists in the permissions table (means its been granted)
IF EXISTS(select int_ID from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionID = @PermissionID )
Declare @oldpermStatus int, @Reason int
--get the current status of the permission to be denied
Set @oldpermStatus = (select int_PermissionsStatus from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionID = @PermissionID)
-- if the permstatus is 0 (means requested) then set the deny reason to 1 which is refused by admin
If (@oldpermStatus = 0)
Set @Reason = 1
else
-- set the deny reason to 3 which is request removal by authoriser
Set @Reason = 3


         -- set the denied permmision to a status of 6 (which is ready to be deleted)
      begin
       Update dbo.tbl_Permissions_UserPermissions
       Set int_PermissionsStatus = 6
       where int_ColleagueID = @ColleagueID and int_PermissionID = @PermissionID   
         IF @@ERROR <> 0 GOTO ErrBlock
      end
    -- find the ID of the permission and check it has a status of 6. if it does then move it to the denied table
    IF exists(select int_ID from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionsStatus = 6)

      Exec @RetVal = dbo.usp_Permissions_Admin_Move_Denied_UserPermissions @ColleagueID, @Reason
       IF @RetVal <> 0 GOTO ErrBlock


COMMIT TRANSACTION
Return

ErrBlock:
ROLLBACK TRANSACTION
Return

End



SP2
ALTER PROCEDURE [dbo].[usp_Permissions_Admin_Move_Denied_UserPermissions]
   -- Add the parameters for the stored procedure here
@ColleagueID int,
@Reason int
AS
BEGIN
   -- SET NOCOUNT ON added to prevent extra result sets from
   -- interfering with SELECT statements.
   SET NOCOUNT ON;

-- Insert statements for procedure here

Declare @RetVal int, @interror int
Set @interror = 0

Insert into dbo.tbl_Permissions_UserPermissionsDenied (int_ColleagueID,int_PermissionID,int_DenyStatus,dte_DeniedOn)
    values (@ColleagueID,(Select int_PermissionID from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionsStatus in (5)),@reason,getdate())
      IF (@@ERROR <> 0) BEGIN
         Set @interror = 1
      END
      
      If(@interror = 0) BEGIN
         Delete from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionsStatus in (6)
      END

If (@interror = 0)      
RETURN 0      
else
RETURN 1
End



I'm sure there is a much nicer way and a better way of doing it but I don't know it!! :-P
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47189 Visits: 44356
Yup, that's about it for SQL 2000's error handling. Fun, isn't it?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search