简体   繁体   中英

How to make sure the SQL trigger complete after the Transaction.commit?

Here is our scenario...

We have a transaction to update the SQL server database, we also have some sql triggers in database to do some corresponding works (update the table, add some data, etc).

After the transaction commit, we need read the data which updated from trigger.... The code looks like the following...

using (var tm = UpdateTransaction.Begin(...))
                            {
                                UpdateDatabase();
                                tm.Commit();
                            }
                            string val = GetData();

However we find that, sometimes GetData() cannot return the correct result. We doubt that the trigger has not been completed at that time...

So... How to handle this issue, can you give us some advices.

Thank you very much in advance...

You can check the status of the trigger, if it is enabled or disabled, in this way:

SELECT
OBJECT_NAME(PARENT_OBJ) TABLE_NAME,
NAME AS TRIGGER_NAME,
CASE OBJECTPROPERTY(ID, 'EXECISTRIGGERDISABLED')
WHEN 0 THEN 'ENABLED'
ELSE 'DISABLED'
END AS STATUS
FROM SYSOBJECTS
WHERE XTYPE = 'TR'

About how to check if the trigger completes, one way is to check the tables involved in the trigger, that means if the related tables/records are updated accordingly.

Another way, I think the best, is to check the execution of the trigger using SQL Server Profiler . Executing SQL Server Profiler, in the events selection section, enable the option "Show All Events" and make sure the stored procedures SP:StmtStarting and SP:StmtCompleted are checked. You will be able in this way to check the execution of your triggers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM