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


delete trigger is not working as i want


delete trigger is not working as i want

Author
Message
delta dirac
delta dirac
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 54
Hi everyone
i 'm using sql server express 2005 and i have tree triggers for insert delete and update

İ am making my inserts ,deletes or updates through ADO.NET (so multiple rows in an update or delete or insert)

i have a sales table and a sales details table and i am making a plus operation on stock when it's an insert and the opposite for deletes(putting back the stocks quantity),
The insert works fine (when i make an insert all the godds that sold are on the plus values and taht's right), the update is also fine but when it comes to delete ,it updates only the fist stocks that sold and the other rows are untouched
here's my delete trigger

ALTER TRIGGER [dbo].[faturagdelete] on [dbo].[fatura]

for delete
AS
declare @stokkod int,@quantity decimal(8,2)
select @stokkod=stokkod,@quantity=quantity from deleted
UPDATE stok
SET stokquantity =stokquantity-@quantity
FROM stok
WHERE stok.stokkod =@stokkod

why did this trigger updated only the First items quantity and leave the rest(the same methot is working on insert trigger bu this ones not)
i'll be very appreciate for any ideas thanks.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)SSC Guru (117K reputation)

Group: General Forum Members
Points: 117951 Visits: 45532
Triggers fire once per insert/update/delete, not once per row. Your line here

select @stokkod=stokkod,@quantity=quantity from deleted



Will fetch only one of the values from the deleted table, while there are as many as rows that you delete.

Maybe something more like this... (rough, untested)

UPDATE stok
SET stokquantity =stokquantity-deleted.quantity
FROM stok inner join deleted on
stok.stokkod = deleted.stokkod



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


delta dirac
delta dirac
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 54
Dear friend it worked exactly as i want ,and by the way thanks to your explanation i think i got the logic thank you very very much, it was a lifesaver answer for me Wink
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