Unix Technical Forum

Delete Trigger

This is a discussion on Delete Trigger within the SQL Server forums, part of the Microsoft SQL Server category; --> I have set up a trigger that updates a table in another part of may database whenever a row ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-28-2008, 06:39 PM
Andy Baker
 
Posts: n/a
Default Delete Trigger

I have set up a trigger that updates a table in another part of may database
whenever a row is deleted - of the form
CREATE TRIGGER t_Delete ON tblOrderDetails AFTER DELETE
AS
SELECT @Quantity = Quantity FROM DELETED
etc etc
This works fine if I delete only one row from tblOrderDetails at a time, but
if I run a DELETE query that deletes more than one row, the correct rows are
deleted but my trigger only gets executed once, where I want it to be
executed for each deleted row. Is this normal behaviour, or am I doing
somthing wrong? Thanks in advance.

Andy Baker


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-28-2008, 06:39 PM
Plamen Ratchev
 
Posts: n/a
Default Re: Delete Trigger

The trigger fires once for the delete statement but you have access to all
deleted rows in the Deleted logical table. You have to write your code to
handle multiple rows. For example, in your select statement you can use SUM
to get the summary quantity for all deleted rows:

SELECT @Quantity = SUM(Quantity)
FROM Deleted;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-28-2008, 06:39 PM
David Portas
 
Posts: n/a
Default Re: Delete Trigger

"Andy Baker" <abaker@NOSPAMvanputer.com> wrote in message
news:V4adnYrYUYXLCRXVnZ2dneKdnZydnZ2d@posted.plusn et...
>I have set up a trigger that updates a table in another part of may
>database whenever a row is deleted - of the form
> CREATE TRIGGER t_Delete ON tblOrderDetails AFTER DELETE
> AS
> SELECT @Quantity = Quantity FROM DELETED
> etc etc
> This works fine if I delete only one row from tblOrderDetails at a time,
> but if I run a DELETE query that deletes more than one row, the correct
> rows are deleted but my trigger only gets executed once, where I want it
> to be executed for each deleted row. Is this normal behaviour, or am I
> doing somthing wrong? Thanks in advance.
>
> Andy Baker
>
>


The only thing you were doing wrong was to assume that the trigger would be
executed for each row. Triggers execute once per statement, not per row. So
you need to change your code reflect that. Unfortunately we don't know what
your trigger does with the value @Quantity so it's hard to advise you.

--
David Portas


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-28-2008, 06:39 PM
Andy Baker
 
Posts: n/a
Default Re: Delete Trigger


"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message
news:bq2dne4ys_7PchXVnZ2dnUVZ8uOdnZ2d@giganews.com ...
> "Andy Baker" <abaker@NOSPAMvanputer.com> wrote in message
> news:V4adnYrYUYXLCRXVnZ2dneKdnZydnZ2d@posted.plusn et...
>>I have set up a trigger that updates a table in another part of may
>>database whenever a row is deleted - of the form
>> CREATE TRIGGER t_Delete ON tblOrderDetails AFTER DELETE
>> AS
>> SELECT @Quantity = Quantity FROM DELETED
>> etc etc
>> This works fine if I delete only one row from tblOrderDetails at a time,
>> but if I run a DELETE query that deletes more than one row, the correct
>> rows are deleted but my trigger only gets executed once, where I want it
>> to be executed for each deleted row. Is this normal behaviour, or am I
>> doing somthing wrong? Thanks in advance.
>>
>> Andy Baker
>>
>>

>
> The only thing you were doing wrong was to assume that the trigger would
> be executed for each row. Triggers execute once per statement, not per
> row. So you need to change your code reflect that. Unfortunately we don't
> know what your trigger does with the value @Quantity so it's hard to
> advise you.
>
> --
> David Portas
>


Thanks - that clears it up. I have changed my code to account for it.

Andy Baker


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:41 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com