Trigger to insert from one (new row) in table to another table

  • I have a table where rows are created via a stored procedure.

    I need a trigger that will then insert those rows into another table.

    The problem I am having is that it seems to be trying to insert the rows before the row is established in the first table. When the trigger below executes I get an error stating that the table does not except nulls into num_of_copies_num (which is correct)

    This makes it seems to be that it is trying to do the insert before the row has been created by the sproc. Any advice?

    create trigger [dbo].[insert_transcript_request]

    on [dbo].[cosc_TRANSCRIPT_REQUESTs]

    AFTER INSERT, UPDATE

    AS if UPDATE(id_num)

    begin

    insert into TRANSCRIPT_REQUEST(ID_NUM,

    SEQ_NUM_2,

    TRANSCRIPT_PRT_DTE,

    DIV_GRP_CDE,

    NUM_OF_COPIES_NUM ,

    ADDR_BLOCK_LINE_1,

    ADDR_BLOCK_LINE_2,

    ADDR_BLOCK_LINE_3,

    ADDR_BLOCK_LINE_4,

    JOB_NAME, JOB_TIME,

    USER_NAME)

    select i.id_num,

    isnull(tr_seq.old_seq,0) + 1,

    GETDATE(),

    DIV_GRP_CDE,

    i.NUM_OF_COPIES_NUM ,

    i.ADDR_BLOCK_LINE_1,

    i.ADDR_BLOCK_LINE_2,

    i.ADDR_BLOCK_LINE_3,

    i.ADDR_BLOCK_LINE_4,

    'request from trigger', GETDATE(), 'SA_nag'

    FROM inserted i , (select MAX(SEQ_NUM_2) as old_seq

    from TRANSCRIPT_REQUEST t, inserted i

    where t.ID_NUM = i.id_num) as tr_seq

    where ID_NUM is not NULL

    end

  • My advice would be not to use a trigger for this.

    Change your stored procedure or your application to perform both inserts.

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • The stored procedure is a standard proc used for different circumstances. I prefer to have a trigger execute the insert

  • ngustafson-1056093 (10/1/2013)


    The stored procedure is a standard proc used for different circumstances. I prefer to have a trigger execute the insert

    Can you post the code for the sproc?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I probably shouldn't, seeing that is one supplied by our erp provider. I may create a sproc that the user runs instead.

  • That procedure might have multiple "reiterating" update while a row is added to the table.

    Extremely bad but at the same time very popular approach.

    Trigger is launched during every one of those updates, not the only "final" one.

    Without seeing the procedure it's hard to suggest a proper workaround.

    But for that specific error you posted above this should work:

    create trigger [dbo].[insert_transcript_request]

    on [dbo].[cosc_TRANSCRIPT_REQUESTs]

    AFTER INSERT, UPDATE

    AS

    IF UPDATE(id_num)

    begin

    insert into TRANSCRIPT_REQUEST(ID_NUM,

    SEQ_NUM_2,

    TRANSCRIPT_PRT_DTE,

    DIV_GRP_CDE,

    NUM_OF_COPIES_NUM ,

    ADDR_BLOCK_LINE_1,

    ADDR_BLOCK_LINE_2,

    ADDR_BLOCK_LINE_3,

    ADDR_BLOCK_LINE_4,

    JOB_NAME, JOB_TIME,

    USER_NAME)

    select i.id_num,

    isnull(tr_seq.old_seq,0) + 1,

    GETDATE(),

    DIV_GRP_CDE,

    i.NUM_OF_COPIES_NUM ,

    i.ADDR_BLOCK_LINE_1,

    i.ADDR_BLOCK_LINE_2,

    i.ADDR_BLOCK_LINE_3,

    i.ADDR_BLOCK_LINE_4,

    'request from trigger', GETDATE(), 'SA_nag'

    FROM inserted i

    LEFT JOIN (

    SELECT t.ID_NUM, MAX(SEQ_NUM_2) as old_seq

    from TRANSCRIPT_REQUEST t

    GROUP BY t.ID_NUM ) as tr_seq ON tr_seq.ID_NUM = i.id_num

    where i.ID_NUM is not NULL

    AND i.NUM_OF_COPIES_NUM IS NOT NULL

    end

    _____________
    Code for TallyGenerator

  • Thanks so much! I will check this out.

  • Viewing 7 posts - 1 through 6 (of 6 total)

    You must be logged in to reply to this topic. Login to reply