Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread How to debug a trigger?
Tue, Sep 18 2012 12:47 PMPermanent Link

Barry

I have added several

Set Log Message to "test"; -- etc.

inside of my before Update/Insert trigger for a table.

But when using EDBMgr to update a row from a dbgrid, none of the messages show up in the Log Messages window.

Why? If "Log Message" doesn't work in a trigger, how does one debug a trigger?

TIA
Barry
Tue, Sep 18 2012 2:41 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate

Barry,

<< I have added several Set Log Message to "test"; -- etc.
inside of my before Update/Insert trigger for a table.
But when using EDBMgr to update a row from a dbgrid, none of the messages show up in the Log Messages window. Why? >>

When a PSM routine executes a SET LOG MESSAGE statement, a log messages, that can be retrieved by the calling application, is generated when a TEDBLogMessageEvent event fires.

TEDBSession, TEDBDatabase, TEDBQuery, TEDBScript and TEDBStoredProcedure types have an OnLogMessage Event of that type, that fires whenever the PSM routine they are calling generate a log message by executing a SET LOG MESSAGE statement. Thats how EDB Manager, that is an EDB application like any other, displays the log messages.

Triggers are automatically called by the engine as appropriate, there is no calling application and it's execution doesn't have an associated calling component that can retrieve the log messages, thats why SET LOG MESSAGE has no effect inside triggers.

<< If "Log Message" doesn't work in a trigger, how does one debug a trigger? >>

The simplest way is to create a script with the same code and debug it with EDB Manager, but that method doesn't allow you to reproduce the firing sequence or timing.
What I do to debug triggers is to add my own "MyLog" table to the database, and a stored procedure "AddLogMessage(IN "Msg" VARCHAR)" that I call from the triggers, and adds a row to the "MyLog" table.


--
Fernando Dias
[Team Elevate]
Tue, Sep 18 2012 2:50 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate

Reading my own post, I found it confusing Smiley
Let me rephrase the first sentence; what i mean is:

When a PSM routine executes a SET LOG MESSAGE statement, a log messages is generated.
That log message can be retrieved by the calling application, when a TEDBLogMessageEvent event fires.

--
Fernando Dias
[Team Elevate]
Tue, Sep 18 2012 3:49 PMPermanent Link

Barry

Fernando,

Thanks for the suggestions. After I posted the original message, I started using "Execute Immediate Insert into LogMsg (Msg) values('...'); but it cluttered up the trigger code. I decided to use your LogMsg() SP instead. It works well.  At least now I have some idea of what the trigger is doing.

It would be ideal if there was some way to debug a trigger, but I guess that is quite a ways off.

I find I can do some debugging with Scripts in EDBMgr if I declare variables as NewRow_InvoiceAmt etc and assign default values for them. Same with OldRow. Then when the script is working properly, I just rename "NewRow_"  to "NewRow." and copy the script back to the trigger. I also comment out the new declarations ("NewRow_*") with /* ... */ so if I need to debug it again, I don't have to redeclare them all over again. It is a bit of a pain to set up, but at least it is one solution for complicated triggers.

Barry
Image