My company is using SQL Server 2008. I am facing a Audit Table with transaction problems.
I have a stored procedure.
create proc MySP
as begin
insert into MY_TABLE values('Value1');
begin transaction TX_MY_SP
-- write to audit table permanently
insert into AUDIT_TABLE values('Value1 is inserted.');
commit transaction TX_MY_SP
end
I have a block of VB.net code
Using tx = New TransactionScope()
Using conn = New SqlConnection(MY_CONN_STR)
Using cmd = New SqlCommand("MySP", conn)
conn.Open()
cmd.ExecuteNonQuery()
Throw New ApplicationException("Always throw exception.")
End Using
End Using
tx.Complete()
End Using
However there is no record inserted into AUDIT_TABLE. I found the reason in MSDN http://msdn.microsoft.com/en-us/library/ms189336.aspx
My question is how can I insert records AUDIT_TABLE with stored procedure.
Thanks!
When you use
TransactionScope, you need to call theCompletemethod before going out of its scope if you don't want the transaction to rollback: