Solved

Subquery returning more than 1 value in a trigger

Posted on 2008-06-24
3
221 Views
Last Modified: 2012-06-27
I am writing a clean up routine to go in the session end of my asp.net, it is to remove abandoned orders and replace the allocated stock. the problem is there will be multiple lines of products in the order and my trigger only works for ONE line how do I change it so it will handle multiple lines?

Mark
ALTER TRIGGER trig_delOrderLineEntry 

/*

This trigger will update the productlines qty when items are deleted from 

the orderline table mainly in the ordersummary and the session.end

*/

ON orderlines
 

FOR DELETE 
 

AS
 

DECLARE @qty int, @prodid int
 

SELECT @qty = (SELECT Qty FROM Deleted)
 

SELECT @prodid = (SELECT prodlineid FROM Deleted)
 
 

UPDATE    ProductLines

SET              StockLevel = StockLevel + @qty

WHERE     (id = @prodid)

Open in new window

0
Comment
Question by:markej
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21853695
change the trigger like this:
ALTER TRIGGER trig_delOrderLineEntry 
/*
This trigger will update the productlines qty when items are deleted from 
the orderline table mainly in the ordersummary and the session.end
*/
ON orderlines
 
FOR DELETE 
 
AS
 
UPDATE pl
   SET StockLevel = StockLevel + (SELECT SUM(d.qty) FROM Deleted d WHERE d.prodlineid = pl.id )
  FROM ProductLines pl
 WHERE pl.id IN ( SELECT prodlineid FROM Deleted )

Open in new window

0
 

Author Closing Comment

by:markej
ID: 31470074
Thanks especially for the quick response.

Mark
0
 

Expert Comment

by:monkeylugs
ID: 21853732
ALTER TRIGGER trig_delOrderLineEntry
/*
This trigger will update the productlines qty when items are deleted from
the orderline table mainly in the ordersummary and the session.end
*/
ON orderlines
 
FOR DELETE
 
AS
To avoid the subquery you could try this:;

 
UPDATE pl
SET
      StockLevel = StockLevel + d.Qty
from
      Deleted d
      inner join ProductLines pl on pl.id = d.prodlineid
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now