设置字段的更新有条件地确定不起作用?

时间:2013-11-21 11:41:50

标签: mysql tsql

我在表上有一个后插入触发器,用于根据新插入的行更新另一个表上的字段。 (它在一个地方扣除库存数量并添加到另一个地点。每列都是一个不同的库存地点)

要更新的确切字段由选择的参数确定。基本上

UPDATE stock SET FIELD1=FIELD1+NEW.stquantity, FIELD2=FIELD2-NEW.stquantity 

WHERE FIELD1和FIELD2由案例陈述

决定

我现在的触发器现在看起来像这样:

CREATE TRIGGER `changestockqty` AFTER INSERT ON `stocktransfer_items` FOR EACH ROW

BEGIN
DECLARE fromstocklocationvar INT DEFAULT -1;
DECLARE tostocklocationvar INT DEFAULT -1;


SET fromstocklocationvar = (SELECT stsource_location FROM stocktransfers WHERE stocktransfer.idstocktransfers=NEW.idstocktransfers);
SET tostocklocationvar = (SELECT stdest_location FROM stocktransfers WHERE stocktransfer.idstocktransfers=NEW.idstocktransfers);

UPDATE stock SET
CASE
WHEN fromstocklocationvar=1 THEN sl1
WHEN fromstocklocationvar=2 THEN sl2
WHEN fromstocklocationvar=3 THEN sl3
WHEN fromstocklocationvar=4 THEN sl4
WHEN fromstocklocationvar=5 THEN sl5
END
=
CASE    
WHEN fromstocklocationvar=1 THEN sl1
WHEN fromstocklocationvar=2 THEN sl2
WHEN fromstocklocationvar=3 THEN sl3
WHEN fromstocklocationvar=4 THEN sl4
WHEN fromstocklocationvar=5 THEN sl5
END
-NEW.stquantity,
CASE
WHEN tostocklocationvar=1 THEN sl1
WHEN tostocklocationvar=2 THEN sl2
WHEN tostocklocationvar=3 THEN sl3
WHEN tostocklocationvar=4 THEN sl4
WHEN tostocklocationvar=5 THEN sl5
END
=
CASE    
WHEN tostocklocationvar=1 THEN sl1
WHEN tostocklocationvar=2 THEN sl2
WHEN tostocklocationvar=3 THEN sl3
WHEN tostocklocationvar=4 THEN sl4
WHEN tostocklocationvar=5 THEN sl5
END
+NEW.stquantity,

 WHERE stock.idstockpcode=NEW.stpcode;

END

在第一种情况后显示错误。我做错了什么?

2 个答案:

答案 0 :(得分:1)

可能存在语法错误(您尝试使用CASE语句创建另一个语句)..

尝试这个SQL(做同样的事情):

CREATE TRIGGER `changestockqty` AFTER INSERT ON `stocktransfer_items` FOR EACH ROW

BEGIN
DECLARE fromstocklocationvar INT DEFAULT -1;
DECLARE tostocklocationvar INT DEFAULT -1;


SET fromstocklocationvar = (SELECT stsource_location FROM stocktransfers WHERE stocktransfer.idstocktransfers=NEW.idstocktransfers);
SET tostocklocationvar = (SELECT stdest_location FROM stocktransfers WHERE stocktransfer.idstocktransfers=NEW.idstocktransfers);

UPDATE stock SET
sl1 = CASE WHEN fromstocklocationvar=1 THEN sl1-NEW.stquantity ELSE sl1 END,
sl2 = CASE WHEN fromstocklocationvar=2 THEN sl2-NEW.stquantity ELSE sl2 END,
sl3 = CASE WHEN fromstocklocationvar=3 THEN sl3-NEW.stquantity ELSE sl3 END,
sl4 = CASE WHEN fromstocklocationvar=4 THEN sl4-NEW.stquantity ELSE sl4 END,
sl5 = CASE WHEN fromstocklocationvar=5 THEN sl5-NEW.stquantity ELSE sl5 END
,
sl1 = CASE WHEN tostocklocationvar=1 THEN sl1+NEW.stquantity ELSE sl1 END,
sl2 = CASE WHEN tostocklocationvar=2 THEN sl2+NEW.stquantity ELSE sl2 END,
sl3 = CASE WHEN tostocklocationvar=3 THEN sl3+NEW.stquantity ELSE sl3 END,
sl4 = CASE WHEN tostocklocationvar=4 THEN sl4+NEW.stquantity ELSE sl4 END,
sl5 = CASE WHEN tostocklocationvar=5 THEN sl5+NEW.stquantity ELSE sl5 END

 WHERE stock.idstockpcode=NEW.stpcode;

END

答案 1 :(得分:1)

你不能这样做。 Update的左侧必须修复。 这是一个例子:

UPDATE stock SET
sl1=sl1 - CASE WHEN fromstocklocationvar=1 THEN NEW.stquantity ELSE 0 END
        + CASE WHEN tostocklocationvar=1 THEN NEW.stquantity ELSE 0 END, 
sl2=sl2 - CASE WHEN fromstocklocationvar=2 THEN NEW.stquantity ELSE 0 END
       + CASE WHEN tostocklocationvar=2 THEN NEW.stquantity ELSE 0 END, 
sl3=sl3 - CASE WHEN fromstocklocationvar=3 THEN NEW.stquantity ELSE 0 END
       + CASE WHEN tostocklocationvar=3 THEN NEW.stquantity ELSE 0 END, 
sl4=sl4 - CASE WHEN fromstocklocationvar=4 THEN NEW.stquantity ELSE 0 END
       + CASE WHEN tostocklocationvar=4 THEN NEW.stquantity ELSE 0 END, 
sl5=sl5 - CASE WHEN fromstocklocationvar=5 THEN NEW.stquantity ELSE 0 END
       + CASE WHEN tostocklocationvar=5 THEN NEW.stquantity ELSE 0 END 

 WHERE stock.idstockpcode=NEW.stpcode;