SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
ngustafson-1056093
ngustafson-1056093
SSC-Enthusiastic
SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)

Group: General Forum Members
Points: 144 Visits: 44
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
mister.magoo
mister.magoo
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10747 Visits: 7891
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
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • ngustafson-1056093
    ngustafson-1056093
    SSC-Enthusiastic
    SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)

    Group: General Forum Members
    Points: 144 Visits: 44
    The stored procedure is a standard proc used for different circumstances. I prefer to have a trigger execute the insert
    ChrisM@Work
    ChrisM@Work
    SSC-Forever
    SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

    Group: General Forum Members
    Points: 42077 Visits: 20008
    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
    Exploring Recursive CTEs by Example Dwain Camps
    ngustafson-1056093
    ngustafson-1056093
    SSC-Enthusiastic
    SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)

    Group: General Forum Members
    Points: 144 Visits: 44
    I probably shouldn't, seeing that is one supplied by our erp provider. I may create a sproc that the user runs instead.
    Sergiy
    Sergiy
    One Orange Chip
    One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

    Group: General Forum Members
    Points: 25915 Visits: 12473
    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


    ngustafson-1056093
    ngustafson-1056093
    SSC-Enthusiastic
    SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)

    Group: General Forum Members
    Points: 144 Visits: 44
    Thanks so much! I will check this out.
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search