Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Nested Update in a Select statement?

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-11-10 : 08:25:29
Ryan writes "I’m trying to write a select statement that will show me all my current orders and order lines. I want it to show in detail the qty ordered, qty already shipped, and qty on hand. I also want it to calculate the total qty to make based on ((qty ordered – qty shipped) – qty on hand).

I’m able to get the total to make for each order but I need to update my qty on hand as I use it up, so other orders for the same item do not calculate qty to make based on inventory already being used.

My question is can I write in a select statement a nested update to update my current inventory levels. I have put all my inventories in a temp table so I’m not updating actual inventories levels. This is used as a production reference.

INSERT INTO ## ProdToMake
SELECT co.co_num, coitem.co_line, coitem.due_date, coitem.item, coitem.qty_ordered, coitem.qty_shipped,
(SELECT 'qty_on_make' = CASE
WHEN ((coitem.qty_ordered - coitem.qty_shipped) - ##ItemLoc.qty_on_hand) = 0 THEN 0
WHEN ((coitem.qty_ordered - coitem.qty_shipped) - ##ItemLoc.qty_on_hand) > 0 THEN ((coitem.qty_ordered - coitem.qty_shipped) - ##ItemLoc.qty_on_hand)
WHEN ((coitem.qty_ordered - coitem.qty_shipped) - ##ItemLoc.qty_on_hand) < 0 THEN 0
END
FROM ## ItemLoc WHERE ##ItemLoc.item = coitem.item) AS qty_on_make
FROM co INNER JOIN custaddr
ON co.cust_num = custaddr.cust_num
INNER JOIN coitem
ON co.co_num = coitem.co_num
INNER JOIN job
ON item.job = job.job AND item.suffix = job.suffix
WHERE co.stat = 'O' AND custaddr.cust_seq = 0 AND (coitem.stat = 'O' OR coitem.stat = 'P') "

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-11-10 : 08:30:58
This might help:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=10177
Go to Top of Page
   

- Advertisement -