Solved

Subquery returning more than 1 value in a trigger

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

728 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