﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / Transactions / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 07:17:53 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>Yup, that's about it for SQL 2000's error handling. Fun, isn't it?</description><pubDate>Fri, 25 Jan 2013 05:31:34 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>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[code="sql"]ALTER PROCEDURE [dbo].[usp_Permissions_Update_Denied_UserPermissions]@ColleagueID int,@PermissionID intASBEGIN	-- SET NOCOUNT ON added to prevent extra result sets from	-- interfering with SELECT statements.	SET NOCOUNT ON;BEGIN TRANSACTIONDeclare @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 &amp;lt;&amp;gt; 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 &amp;lt;&amp;gt; 0 GOTO ErrBlock  COMMIT TRANSACTIONReturnErrBlock:ROLLBACK TRANSACTIONReturnEnd[/code]SP2[code="sql"]ALTER PROCEDURE [dbo].[usp_Permissions_Admin_Move_Denied_UserPermissions]	-- Add the parameters for the stored procedure here@ColleagueID int,@Reason intASBEGIN	-- SET NOCOUNT ON added to prevent extra result sets from	-- interfering with SELECT statements.	SET NOCOUNT ON;    -- Insert statements for procedure hereDeclare @RetVal int, @interror intSet @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 &amp;lt;&amp;gt; 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)		ENDIf (@interror = 0)		RETURN 0		elseRETURN 1End[/code]I'm sure there is a much nicer way and a better way of doing it but I don't know it!! :-P</description><pubDate>Fri, 25 Jan 2013 03:56:48 GMT</pubDate><dc:creator>Dave Hall</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>[quote][b]Dave Hall (1/25/2013)[/b][hr]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)?[/quote]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.</description><pubDate>Fri, 25 Jan 2013 03:37:32 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>[quote][b]Dave Hall (1/25/2013)[/b][hr]I'd love to Bhuvnesh but I'm using SQL 2000 :([/quote] oh my bad i just realized that i am in sql 2000 window :Whistling:</description><pubDate>Fri, 25 Jan 2013 03:30:06 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>I'd love to Bhuvnesh but I'm using SQL 2000 :(</description><pubDate>Fri, 25 Jan 2013 03:27:01 GMT</pubDate><dc:creator>Dave Hall</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>[quote][b]Dave Hall (1/25/2013)[/b][hr]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).[/quote] Then i will sugges to you use TRy-Catch block</description><pubDate>Fri, 25 Jan 2013 03:24:21 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>Thanks, Gail.  In a way it's a comfort to know its not just me being a pleb with this.  It sounded so simple at the start!!!I have just tested your suggestion with regards to have the transaction in 1 and not the other but hit a problem....SP with transaction:[code="sql"]Create PROCEDURE [dbo].[usp_Permissions_Update_Denied_UserPermissions]@ColleagueID int,@PermissionID intASBEGIN	-- SET NOCOUNT ON added to prevent extra result sets from	-- interfering with SELECT statements.	SET NOCOUNT ON;BEGIN TRANSACTION    --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)		   Update dbo.tbl_Permissions_UserPermissions 		   Set int_PermissionsStatus = 6		   where int_ColleagueID = @ColleagueID and int_PermissionID = @PermissionID			IF @@ERROR &amp;lt;&amp;gt; 0 GOTO ErrBlock				  -- 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 dbo.usp_Permissions_Admin_Move_Denied_UserPermissions @ColleagueID, @Reason		 IF @@ERROR &amp;lt;&amp;gt; 0 GOTO ErrBlock   COMMIT TRANSACTIONReturnErrBlock:ROLLBACK TRANSACTIONReturnEnd[/code]and the other:[code="sql"]Create PROCEDURE [dbo].[usp_Permissions_Admin_Move_Denied_UserPermissions]	-- Add the parameters for the stored procedure here@ColleagueID int,@Reason intASBEGIN	-- SET NOCOUNT ON added to prevent extra result sets from	-- interfering with SELECT statements.	SET NOCOUNT ON;    -- Insert statements for procedure here            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())			Delete from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionsStatus in (6)End[/code]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)?I really appreciate your insight and patience with this!@Bhuvnes - I could do that, so I've read, but am trying to learn a little bit more..... perhaps running before I can walk here!</description><pubDate>Fri, 25 Jan 2013 03:17:16 GMT</pubDate><dc:creator>Dave Hall</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>Also see this link too [url]http://rusanu.com/2009/06/11/exception-handling-and-nested-transactions/[/url]</description><pubDate>Fri, 25 Jan 2013 03:14:26 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>Instead , why cant you use SET XACT_ABORT ON in both the SP and remove all explicit transaction handling</description><pubDate>Fri, 25 Jan 2013 03:10:42 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>[quote][b]Dave Hall (1/25/2013)[/b][hr]Thanks, GilaMonster.  I have indeed learned that they are a pain!I understand the concept of how nested transactions work and have tried using @@Trancount to see if there are still open transactions that need to commit..... but all to no avail!  I was hoping someone might be able to say "ahhh you need to test for @@trancount here before you commit" ... [/quote]Well, if you want... Not going to solve half the problems but....[quote]The problem with only doing the Rollback in 1 or the other is that each procedure can and does get called on its own so I'd really need the transaction in both.[/quote]Then you're in for a world of hurt, odd bugs, annoying errors, etc. I would strongly recommend you take a long look at the design, see if you can make it so that transactions are only ever started and committed at one level, not nested.If you have to go this way, then you need either to check the transaction count before you *begin* a transaction and have different behaviours in the procedure depending on whether there's already a transaction open or not, or you need to add savepoints and rollbacks to those savepoints if there's already a transaction open. Which one depends on what needs to roll back in the case of an error.Neither option is clean and easy, both are going to result in lots of extra code for the transaction management.</description><pubDate>Fri, 25 Jan 2013 03:04:26 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>[quote][b]GilaMonster (1/25/2013)[/b][hr]Nested transactions are a lie (and a major pain)The rollback in the inner procedure rolls back all the way to the first begin tranHence you enter the inner proc with a transaction open (tran count 1), start a second one (tran count now 2) hit rollback, that rolls back all open transactions (tran count 0, different from when you entered the proc), return to the outer procedure and hit commit, but there's no open transaction any longer.Don't nest transactions (unless you really know what you're doing and how nested transactions work). Either do your transaction management in the outer proc or in the inner proc, not both.[/quote]Thanks, GilaMonster.  I have indeed learned that they are a pain!I understand the concept of how nested transactions work and have tried using @@Trancount to see if there are still open transactions that need to commit..... but all to no avail!  I was hoping someone might be able to say "ahhh you need to test for @@trancount here before you commit" ... The problem with only doing the Rollback in 1 or the other is that each procedure can and does get called on its own so I'd really need the transaction in both.Thanks,Dave</description><pubDate>Fri, 25 Jan 2013 02:37:43 GMT</pubDate><dc:creator>Dave Hall</dc:creator></item><item><title>RE: Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>Nested transactions are a lie (and a major pain)The rollback in the inner procedure rolls back all the way to the first begin tranHence you enter the inner proc with a transaction open (tran count 1), start a second one (tran count now 2) hit rollback, that rolls back all open transactions (tran count 0, different from when you entered the proc), return to the outer procedure and hit commit, but there's no open transaction any longer.Don't nest transactions (unless you really know what you're doing and how nested transactions work). Either do your transaction management in the outer proc or in the inner proc, not both.</description><pubDate>Fri, 25 Jan 2013 02:28:22 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>Transactions</title><link>http://www.sqlservercentral.com/Forums/Topic1411538-8-1.aspx</link><description>Hi Guys,I've been doing a lot of reading (Books Online and articles on here) and can't manage to fix my problem so hoping one of you guys may be able to help.  I have 2 Stored Procs, 1 calls the other.  Here is SP1[code="sql"]Create PROCEDURE [dbo].[usp_Permissions_Update_Denied_UserPermissions]@ColleagueID int,@PermissionID intASBEGIN	-- SET NOCOUNT ON added to prevent extra result sets from	-- interfering with SELECT statements.	SET NOCOUNT ON;BEGIN TRANSACTION    --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)		   Update dbo.tbl_Permissions_UserPermissions 		   Set int_PermissionsStatus = 6		   where int_ColleagueID = @ColleagueID and int_PermissionID = @PermissionID			IF @@ERROR &amp;lt;&amp;gt; 0 GOTO ErrBlock				  -- 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 dbo.usp_Permissions_Admin_Move_Denied_UserPermissions @ColleagueID, @Reason		 IF @@ERROR &amp;lt;&amp;gt; 0 GOTO ErrBlock   COMMIT TRANSACTIONReturnErrBlock:ROLLBACK TRANSACTIONReturnEnd[/code]and here is SP2[code="sql"]Create PROCEDURE [dbo].[usp_Permissions_Admin_Move_Denied_UserPermissions]	-- Add the parameters for the stored procedure here@ColleagueID int,@Reason intASBEGIN	-- SET NOCOUNT ON added to prevent extra result sets from	-- interfering with SELECT statements.	SET NOCOUNT ON;    -- Insert statements for procedure here    BEGIN TRANSACTION                  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 ([b]5[/b])),@reason,getdate())			IF @@ERROR &amp;lt;&amp;gt; 0 GOTO ErrBlock			Delete from dbo.tbl_Permissions_UserPermissions where int_ColleagueID = @ColleagueID and int_PermissionsStatus in (6)			IF @@ERROR &amp;lt;&amp;gt; 0 GOTO ErrBlock			COMMIT TRANSACTION 	Return	ErrBlock:ROLLBACKRETURN  End[/code]My calling code is this:  usp_Permissions_Update_Denied_UserPermissions 4,9I have deliberately changed a bit of the code so that it forces a fail (NULL Insert shown below) and has to rollback.  It does seem to do this but I get the following error every time:[code="plain"]Msg 515, Level 16, State 2, Procedure usp_Permissions_Admin_Move_Denied_UserPermissions, Line 21Cannot insert the value NULL into column 'int_PermissionID', table 'dbo.tbl_Permissions_UserPermissionsDenied'; column does not allow nulls. INSERT fails.The statement has been terminated.Msg 266, Level 16, State 2, Procedure usp_Permissions_Admin_Move_Denied_UserPermissions, Line 0Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 2, current count = 0.Msg 3902, Level 16, State 1, Procedure usp_Permissions_Update_Denied_UserPermissions, Line 46The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.Msg 3903, Level 16, State 1, Procedure usp_Permissions_Update_Denied_UserPermissions, Line 53The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.[/code]Could anyone please explain what it is I'm doing wrong as I've really got no clue!Many thanks in advance,Dave</description><pubDate>Fri, 25 Jan 2013 02:14:03 GMT</pubDate><dc:creator>Dave Hall</dc:creator></item></channel></rss>