Solved

Subquery returning more than 1 value in a trigger

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 64
how to make geography query faster?  SQL 7 45
T-SQL Query - Group By Year 3 32
Oracle perfomance issue. 4 23
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
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.

733 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