Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2009
    Posts
    168

    Unanswered: Transaction problem

    Hi,

    I am having problems with my sproc where I have a number of insert statements. It goes like this:

    BEGIN TRAN
    INSERT ...
    INSERT ...
    etc
    IF /* here I have an IF statement that tests whether a row exists in a table
    /* if exists rollback

    If this row exists I dont want the other INSERT statements to commit. But They still being inserted. How to correct that? Thanks.
    Last edited by kpeeroo; 12-10-09 at 08:30.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    can't fix it if we can not see it. see the instructions in Pat's sticky at the top of this forum.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jul 2009
    Posts
    168
    Code:
    	IF (@debitAccountFullName is not null) BEGIN
    			/*checks if debit acc already exists*/
    			IF NOT EXISTS(SELECT * FROM DebitAccount WHERE name = @debitAccountFullName AND (bankID = (select bankID from bank where name like @bank)) and accountNumber = @debitOrderAccountNumber) BEGIN
    				IF NOT EXISTS((SELECT * FROM bank WHERE name like @bank AND branchID = (SELECT branchID from bankBranch WHERE name like @branch and code like @branchCode))) BEGIN
    					INSERT INTO bankBranch 
    								(name,code) 
    					VALUES (@branch,@branchCode)
    					set @newBankBranchID = scope_identity()
    					INSERT INTO bank 
    								(name,branchID)
    					VALUES (@bank,@newBankBranchID)	
    					set @newBankID = scope_identity()
    				END
    				ELSE BEGIN
    					declare @bankID int
    					declare @branchID int
    					set @branchID = (SELECT branchID from bankBranch WHERE name like @branch AND code LIKE @branchCode)
    					set @bankID = (SELECT bankID from bank WHERE name like @bank and branchID like @branchID)
    				END
    				INSERT INTO DebitAccount
    								(name,bankID,accountNumber,date)
    				VALUES (@debitAccountFullName,@bankID,@debitOrderAccountNumber,@debitAccountDate)
    			END
    			ELSE BEGIN
    				set @intErrorCode = -1
    				GOTO PROBLEM
    				set @errorMessage = 'Debit Order Account already exists'
    				INSERT INTO @errorTable (error) values (@errorMessage)
    				select * from @errorTable
    			END
    		END
    				
    		SELECT @intErrorCode = @@ERROR
    		IF (@intErrorCode <> 0) GOTO PROBLEM				
    		COMMIT TRAN
    
    		PROBLEM:
    			IF (@intErrorCode <> 0) BEGIN
    			PRINT 'Unexpected error occurred!'
    			ROLLBACK TRAN
    	END -- end of transaction problem clause

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Why not put add a WHERE EXISTS to your insert statements?
    George
    Home | Blog

  5. #5
    Join Date
    Jul 2009
    Posts
    168
    Add the 'WHERE EXISTS' where in my code? This will test for duplicates you are implying right? the transaction problem isn't resolved though. It is still committing the previous inserts although PROBLEM routine was selected when a duplicate exists.
    Last edited by kpeeroo; 12-10-09 at 11:15.

  6. #6
    Join Date
    Jul 2009
    Posts
    168
    Very strange. I tried to test the transaction by just using 2-3 inserts and then right after issuing a rollback but still the insert goes on. I'm using Visual Web Developer Express 2008. Does it support transactions?
    Last edited by kpeeroo; 12-10-09 at 14:55.

  7. #7
    Join Date
    Jul 2009
    Posts
    168
    All this fuss for nothing. Thanks guys but the stupid problem lied in the fact that I was using the GUI for testing and each time I run the test for duplicates, the console was writing the newly incremented row and all the affected rows. However they were not being committed. So silly why show it at all then because it was saying '10 rows affected' when actually none were affected. After so much searching I tried to list my tables' items only to see that all this time it was working and nothing was committed!

  8. #8
    Join Date
    Jul 2009
    Posts
    168
    I was curious as to how to use the 'EXISTS WHERE' though as I'm new to TSQL in my code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •