?
Solved

Subquery returning more than 1 value in a trigger

Posted on 2008-06-24
3
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

771 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