Solved

Subquery returning more than 1 value in a trigger

Posted on 2008-06-24
3
222 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

895 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

12 Experts available now in Live!

Get 1:1 Help Now