Monday, March 19, 2012

How to tell if a function is called within a trigger

How can I tell whether a function has been called, along with the values that were passed into it? My SQL is generated by Visual Basic, and on an insert, a trigger fires, which then *might* call the function depending on a condition that's difficult to monitor. Ideally, I'd be looking for a simple function that would write to a log, or print a message somewhere. "Print" does not work within functions. I'm running SQL Server 2000.You could create a stored procedure that writes to a log, and then call that stored procedure from the trigger. Kinda messy, but will work.|||

1. Profiler

2. Put code in the trigger that inserts relevant values (funtion input parameters, record IDs) into a table at the point the function would be called. Comment out or remove this code after testing. I routinely do this in Try Catch blocks (or 2000 error handling) of sprocs during dev.

|||You also cannot perform an INSERT to a permanent table in a function. If this is important you might consider upgrading to SQL Server 2005 and using a SET CONTEXT_INFO to save some information. I guess you could call a procedure from the trigger but I am really not sure how much this will buy you -- you are sort-of already in a psedudo procedure since you are in a trigger.

No comments:

Post a Comment