简体   繁体   中英

SQL Server - WHERE inside CASE?

I'm fairly new to SQL Server and I'm having trouble implementing a WHERE statement inside a CASE, specifically for a trigger I'm working on.

I have 2 tables with the following fields:

Receipt(ItemID,Quantity)

and

Warehouses(WarehouseID,Stock)

|WarehouseID|Stock|
-------------------
|ARM01      |100  |
|ARM02      |100  |

The trigger checks whether a row with a specific quantity of any given item was inserted, altered or removed to Receipt and then subtracts or adds to the Stock of the Warehouses table.

So if I add a row with 200 Quantity in Receipt, it will subtract 100 from the Stock of WarehouseID 'ARM01', and if there is no Stock left in 'ARM01' (which there isn't because there is only 100), it will subtract the other 100 from 'ARM02'. Of course if 'ARM02' has a Stock of 0 it will print an error but I'll worry about that later.

The thing is, SQL Server doesn't accept a WHERE inside a CASE, because, of course, the entire CASE is already part of the SET statement and WHEREs only come after a SET. It's weird but I couldn't find an answer online yet for such a simple thing.

My goal:

CASE 1
UPDATE 'Stock of Table' where 'WarehouseID of Same Table' = 'ARM01'
CASE 2
UPDATE 'Stock of Table' where 'WarehouseID of Same Table' = 'ARM02'

I also realize the calculus for Stock -/+ Quantity and vice-versa might be wrong right now and I also have 2 statements in parenthesis after a THEN, separated by a AND because I was trying to distribute the first 100 to ARM01 and the rest to ARM02. That's probably very, very misguided and apologies for the incorrect code but for now I just want to figure out where to use a WHERE statement.

CREATE TRIGGER SubtractStock ON Receipt
FOR INSERT, UPDATE, DELETE
AS
[snipped UPDATE for deleted because it only exchanges '+' for '-' in the operation]
UPDATE Warehouses
SET Stock =
    (CASE
        WHEN inserted.Quantity  <= Stock THEN Stock - Quantity WHERE WarehouseID = 'ARM01'
        WHEN inserted.Quantity > Stock THEN (Stock - Quantity WHERE WarehouseID = 'ARM01')
        AND (Quantity - Stock WHERE WarehouseID = 'ARM02')
        END)
    FROM inserted JOIN Warehouses ON inserted.ItemID = Warehouses.ItemID

Thank you!

I hate triggers, and this is a bad idea for one. With that out of the way.

For ARM01, move the WHERE to the WHEN with an AND:

SET Stock =
    (CASE
        WHEN inserted.Quantity <= Stock AND WarehouseID = 'ARM01'
            THEN Stock - Quantity
...

The next problem is you need to see two rows to set the stock for ARM02, what the stock is in ARM02 to see if you need to subtract from ARM02, and the stock in ARM02 to subtract from. So moving the condition in the where somewhere else won't help. Probably two update statements, update ARM01, with an output clause to temp table or table valued variable to give before stock values for ARM01, then use that to update the values for ARM02.

Again, this sort of complicated logic rarely is appropriate inside a trigger.

Please try like this...Use proper aliases..

CREATE TRIGGER SubtractStock ON Receipt
FOR INSERT, UPDATE, DELETE
AS
UPDATE W 
SET Stock = CASE 
            WHEN WarehouseID = 'ARM01' AND I.Quantity <= Stock THEN Stock - Quantity 
            WHEN WarehouseID = 'ARM01' AND I.Quantity > Stock THEN Stock - Quantity
            WHEN WarehouseID = 'ARM02' THEN I.Quantity - Stock
        END
FROM inserted I JOIN Warehouses W ON I.ItemID = W.ItemID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM