Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger to insert from one (new row) in table to another table Expand / Collapse
Author
Message
Posted Friday, September 27, 2013 12:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 9:15 AM
Points: 10, Visits: 25
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
Post #1499543
Posted Friday, September 27, 2013 4:52 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:38 PM
Points: 1,805, Visits: 5,870
My advice would be not to use a trigger for this.

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



MM


  • MMGrid Addin
  • MMNose Addin


  • 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

  • Post #1499618
    Posted Tuesday, October 1, 2013 5:57 AM
    Grasshopper

    GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

    Group: General Forum Members
    Last Login: Today @ 9:15 AM
    Points: 10, Visits: 25
    The stored procedure is a standard proc used for different circumstances. I prefer to have a trigger execute the insert
    Post #1500354
    Posted Tuesday, October 1, 2013 6:01 AM


    SSCertifiable

    SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

    Group: General Forum Members
    Last Login: Today @ 9:43 AM
    Points: 6,869, Visits: 14,178
    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
    Post #1500356
    Posted Tuesday, October 1, 2013 6:51 AM
    Grasshopper

    GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

    Group: General Forum Members
    Last Login: Today @ 9:15 AM
    Points: 10, Visits: 25
    I probably shouldn't, seeing that is one supplied by our erp provider. I may create a sproc that the user runs instead.
    Post #1500381
    Posted Tuesday, October 15, 2013 10:41 PM
    SSCarpal Tunnel

    SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

    Group: General Forum Members
    Last Login: Monday, November 3, 2014 4:30 PM
    Points: 4,574, Visits: 8,366
    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

    Post #1505065
    Posted Wednesday, October 16, 2013 5:20 AM
    Grasshopper

    GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

    Group: General Forum Members
    Last Login: Today @ 9:15 AM
    Points: 10, Visits: 25
    Thanks so much! I will check this out.
    Post #1505143
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse