Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Transaction commit

Author  Topic 

dano
Starting Member

3 Posts

Posted - 2008-07-21 : 08:14:19
I have two simple insert statements in a transaction as follows:

begin tran
// insert into table1
// insert into table2
commit

The second insert has a foreign key constraint defined on a field in
the first table. If the second insert fails due to it not satisfying this constraint, then the first statement in the transation is not rolled back, so get first insert even though second failed. If I remove the constraint, and then the second insert fails for some other reason, then the transaction *is* rolled back, and neither rows appear in the tables.

Can anyone explain this why having a FK constraint should affect the behaviour of the transation.

Cheers,
DO

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-21 : 08:40:23
[code]declar @err int

begin transaction mytrannamehere

insert into table1

set @err = @@error
if @err = 0
begin
insert into table2
set @err = @error
end

if @err = 0
commit transaction mytrannamehere
else
rollback transaction mytrannamehere[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

dano
Starting Member

3 Posts

Posted - 2008-07-21 : 09:17:12
Thanks but doesn't really help me. I've just been lumbered with maintaining lots and lots of bits of SQL that rely on a simple:

begin tran

// do stuff

commit

People I'm doing this for insist that this has always worked, so I need to know why it doesn't if the failure is due to the foreign key constraint. It won't be easy to change all the SQL to explitly check the error code and do the rollback manually.

Ta.

DanO
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-21 : 09:24:00
Not all errors makes the transaction rollback.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-21 : 09:26:05
This is copied from Books Online
quote:
Errors During Transaction Processing

If an error prevents the successful completion of a transaction, SQL Server automatically rolls back the transaction and frees all resources held by the transaction. If the client's network connection to an instance of the Database Engine is broken, any outstanding transactions for the connection are rolled back when the network notifies the instance of the break. If the client application fails or if the client computer goes down or is restarted, this also breaks the connection, and the instance of the Database Engine rolls back any outstanding connections when the network notifies it of the break. If the client logs off the application, any outstanding transactions are rolled back.

If a run-time statement error (such as a constraint violation) occurs in a batch, the default behavior in the Database Engine is to roll back only the statement that generated the error. You can change this behavior using the SET XACT_ABORT statement. After SET XACT_ABORT ON is executed, any run-time statement error causes an automatic rollback of the current transaction. Compile errors, such as syntax errors, are not affected by SET XACT_ABORT.

It is the responsibility of the programmer to code the application to specify the correct action (COMMIT or ROLLBACK) if a run-time or compile error occurs. One effective tool for handling errors, including those in transactions, is the Transact-SQL TRY…CATCH construct. For more information with examples that include transactions, see Using TRY...CATCH in Transact-SQL and TRY...CATCH (Transact-SQL).



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-21 : 09:28:21
quote:
Originally posted by dano

People I'm doing this for insist that this has always worked
Well, they are wrong.
See explanation above (text copied from Books Online).



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

dano
Starting Member

3 Posts

Posted - 2008-07-21 : 09:30:53
Thanks mate - I've been trying to find something that descibed the general strategy around this area and failing. I now how something that helps for which I am grateful.
Go to Top of Page
   

- Advertisement -