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.
| Author |
Topic |
|
harshal_in
Aged Yak Warrior
633 Posts |
Posted - 2004-02-18 : 01:26:53
|
| I have the following codebegin transaction testsome insertsset @Error=@@errorif @error=0beginsome updatesset @error=@@errorendif @error =0 begincommit transaction testendelsebeginrollback transaction testendnow if there is some error in the execution likebegin transaction testsome insertsset @Error=@@errorif @error=0beginsome updatesset @error=@@errorendselect * from not_existing_tableif @error =0 begincommit transaction testendelsebeginrollback transaction testendnow the select query will give an error since the table does not existand the process is terminated with an error saying "Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 0, current count = 1."how to avoid this?any help highly appreciated.regards,harshalHe is a fool for five minutes who asks , but who does not ask remains a fool for life! |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
|
|
harshal_in
Aged Yak Warrior
633 Posts |
Posted - 2004-02-18 : 02:07:15
|
quote: Originally posted by mohdowais Search and you shall findOS
u r right, I tried searching for it but could not find a solution, my situaltion is not regarding nested transactions its just a simple transactionwhat if there is an error in the transaction before the transaction is comitted or rolled back, and this error is not trappedlike selecting from an invalid table which throws the control out.In this case though the @error variable will contain non zero value but,set @error =@@error statement is not executed at all.He is a fool for five minutes who asks , but who does not ask remains a fool for life! |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2004-02-18 : 07:00:53
|
Hmmm... you are right, attempting to select from a non-existent table seems to be one of those uncatchable errors that aborts the procedure, leaving transactions dangling in mid-air. I am sure someone else here has a solution to this, but I would highly recommend checking for the existance of the table before doing a select, etc on it. The usual check should work:if exists (select * from dbo.sysobjects where id = object_id(N'[MyTableName]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)There is no cure like prevention OS |
 |
|
|
harshal_in
Aged Yak Warrior
633 Posts |
Posted - 2004-02-18 : 23:37:36
|
quote: Originally posted by mohdowais Hmmm... you are right, attempting to select from a non-existent table seems to be one of those uncatchable errors that aborts the procedure, leaving transactions dangling in mid-air. I am sure someone else here has a solution to this, but I would highly recommend checking for the existance of the table before doing a select, etc on it. The usual check should work:if exists (select * from dbo.sysobjects where id = object_id(N'[MyTableName]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)There is no cure like prevention OS
I think I have got a way to handle it, the above solution that you provided checks only for the existence of the table in the database, but what if the where clause gives an error if the data in a few rows is invalid giving an error during the comparision? this was the situation I was facing, and found that, such a situation can be handled by using:exec('select columns from table_name where condition')this seems to trap the error and the transaction is not left in mid air.Regards,harshal.He is a fool for five minutes who asks , but who does not ask remains a fool for life! |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2004-02-18 : 23:55:24
|
| Have you looked at SET XACT_ABORT ON?DavidM"SQL-3 is an abomination.." |
 |
|
|
|
|
|
|
|