How to debug a T-SQL trigger?

前端 未结 4 1546
清歌不尽
清歌不尽 2021-02-03 23:15

I have a table t, which has an \"after insert\" trigger called trgInsAfter. Exactly how do i debug it? i\'m no expert on this, so the question and steps performed might look sil

4条回答
  •  星月不相逢
    2021-02-03 23:41

    You're actually over-thinking this.

    I first run this query in one window (to set things up):

    create table X(ID int not null)
    create table Y(ID int not null)
    go
    create trigger T_X on X
    after insert
    as
        insert into Y(ID) select inserted.ID
    go
    

    I can then discard that window. I open a new query window, write:

    insert into X(ID) values (1),(2)
    

    And set a breakpoint on that line. I then start the debugger (Debug from menu or toolbar or Alt-F5) and wait (for a while, the debugger's never been too quick) for it to hit that breakpoint. And then, having hit there, I choose to Step Into (F11). And lo (after another little wait) a new window is opened which is my trigger, and the next line of code where the debugger stops is the insert into Y... line in the trigger. I can now set any further breakpoints I want to within the trigger.

提交回复
热议问题