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 12»»

Trigger Expand / Collapse
Author
Message
Posted Tuesday, March 23, 2010 4:05 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, September 07, 2010 2:04 AM
Points: 72, Visits: 92
Hi, I have created the Trigger for insertion, Deletion, and Updation.

But the thing is, Suppose If I have multiple insertion, trigger fires that much times.

Do there any to restrict this, we can't do any thing.

Ex:

Insert into table values(' ')
Insert into table values(' ')
Insert into table values(' ')

If I insert three values, trigger fires three time. I agree that this is the nature. But any way to control, if i give multiple insert, it should do at the end.... Probably this might be a stupid question. But please answer this?
Post #887941
Posted Tuesday, March 23, 2010 4:10 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 8:01 AM
Points: 1,651, Visits: 5,201
It is not clear what you want to do if there are multiple rows being updated.

If you want the trigger not to fire for multiple row updates then you can do this

create trigger.....
AS
IF (SELECT COUNT(*) FROM inserted)>1 RETURN
IF (SELECT COUNT(*) FROM deleted)>1 RETURN

If you want to handle multiple updates/inserts then you need to make sure everything in the trigger is set based.

e.g.
create trigger....
as
insert mytable(<column-list>) select <column-list> from inserted where <selections>



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 #887943
    Posted Tuesday, March 23, 2010 4:18 AM
    Valued Member

    Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

    Group: General Forum Members
    Last Login: Tuesday, September 07, 2010 2:04 AM
    Points: 72, Visits: 92
    Now I'll tell you clearly...

    I am having employee table.

    Suppose If today 10 new joinees Joined. So I'll add through Insert statement.

    SO I will give 10 insert statement. right?

    But here I have a trigger for Insert, Update,Delete.

    My requirement is at the insertion end, trigger should work.

    But here 10 times trigger is triggered. Since i am using 10 insert.

    So Is there anything to control, if i use multiple insert trigger should work at the end of the last insert.
    Post #887950
    Posted Tuesday, March 23, 2010 4:18 AM
    Valued Member

    Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

    Group: General Forum Members
    Last Login: Tuesday, September 07, 2010 2:04 AM
    Points: 72, Visits: 92
    Now I'll tell you clearly...

    I am having employee table.

    Suppose If today 10 new joinees Joined. So I'll add through Insert statement.

    SO I will give 10 insert statement. right?

    But here I have a trigger for Insert, Update,Delete.

    My requirement is at the insertion end, trigger should work.

    But here 10 times trigger is triggered. Since i am using 10 insert.

    So Is there anything to control, if i use multiple insert trigger should work at the end of the last insert.

    Please let me know for further info.
    Post #887951
    Posted Tuesday, March 23, 2010 4:33 AM
    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: Wednesday, January 22, 2014 6:44 PM
    Points: 891, Visits: 234
    in SQL Server its statement level trigger. For each statement trigger is fired. There is not any solution for the scenario you are thinking of


    Post #887961
    Posted Tuesday, March 23, 2010 4:41 AM
    Valued Member

    Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

    Group: General Forum Members
    Last Login: Tuesday, September 07, 2010 2:04 AM
    Points: 72, Visits: 92
    Do I need to change the trigger function also, I can. This is my req.

    Please help on this....
    Post #887966
    Posted Tuesday, March 23, 2010 4:47 AM


    SSCrazy

    SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

    Group: General Forum Members
    Last Login: Thursday, April 10, 2014 5:09 AM
    Points: 2,567, Visits: 4,654
    Why such a strange requirement? What is the problem if the trigger gets called 10 times? But still if you want the trigger to be called only once try converting your 10 statements into one.

    One way to do this would be to insert all the records in a temporary table without the trigger and then insert in the permanent table with the trigger at one go with an INSERT...SELECT method.

    But i wonder what benefit you will derive out of this?



    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/
    Post #887968
    Posted Tuesday, March 23, 2010 4:52 AM
    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: Wednesday, January 22, 2014 6:44 PM
    Points: 891, Visits: 234
    I wanted to say that you can not have trigger called only once for many insert statements.

    I think u could have done this for the below statement-

    insert into Table1 (col1, col2) Select col1,col2 from Table2

    Here trigger is fired only once.



    Post #887970
    Posted Tuesday, March 23, 2010 4:54 AM
    Valued Member

    Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

    Group: General Forum Members
    Last Login: Tuesday, September 07, 2010 2:04 AM
    Points: 72, Visits: 92
    First of all, Thanks to kingston....

    Really rocked. This is really help ful.

    Post #887971
    Posted Tuesday, March 23, 2010 4:58 AM
    Valued Member

    Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

    Group: General Forum Members
    Last Login: Tuesday, September 07, 2010 2:04 AM
    Points: 72, Visits: 92
    Thanks vidya..I'll use kingston's suggestion.. That is the best one.... Thanks a lot
    Post #887975
    « Prev Topic | Next Topic »

    Add to briefcase 12»»

    Permissions Expand / Collapse