3

我有两张桌子。它们是

CARD(cardid, credit, usertype,charge)

PAYMENTDEVICE(paydevid, paydevip,paydevdate, paydevtime, chargedcardid, mealtype). 

膳食类型可以是“客人”或“标准”。当在支付设备中插入新行时,我想更新卡表中的信用。费用取决于用户类型。但是如果用餐类型是客人,每个人都必须支付5美元。我尝试使用以下代码

CREATE OR REPLACE TRIGGER  "TRG_PAYMONEY" 
AFTER INSERT
ON PAYMENTDEVICE FOR EACH ROW

BEGIN
UPDATE CARD
WHERE CARDID = :NEW.CHARGEDCARDID
SET CREDIT = 
(CASE MEALTYPE

WHEN "STANDARD" THEN CREDIT - CHARGE
WHEN "GUEST" THEN CREDIT - 5
END);
END;

但我得到这个错误: PL/SQL: ORA-00971: missing SET keyword, PL/SQL: SQL Statement ignored。请问你能帮帮我吗?

4

4 回答 4

5

按照此语法进行更新,

update table_name set field1='value' where field2='value'

(IE)

UPDATE CARD
SET CREDIT = 
(CASE MEALTYPE
WHEN "STANDARD" THEN CREDIT - CHARGE
WHEN "GUEST" THEN CREDIT - 5
END)
 WHERE CARDID = :NEW.CHARGEDCARDID;

有关更多信息,'update'声明的工作方式请参阅此文档,

http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/update_statement.htm

对于你的第二个错误,试试这个,

UPDATE CARD
SET CREDIT = 
(CASE 
WHEN MEALTYPE="STANDARD" THEN CREDIT - CHARGE
WHEN MEALTYPE="GUEST" THEN CREDIT - 5
END MEALTYPE) 
 WHERE CARDID = :NEW.CHARGEDCARDID;

像这样的东西,

update card c
set c.credit=(select case when p.mealtype='STANDARD' then c.credit-c.charge
                 when p.mealtype='GUEST' then c.credit-5
            end credit from PAYMENTDEVICE p
           where c.cardid=p.chargedcardid)

小提琴演示

于 2012-12-25T14:47:42.317 回答
1

您的更新声明应该是(WHERE应该是之后SET

UPDATE CARD
SET CREDIT = 
(CASE MEALTYPE

WHEN "STANDARD" THEN CREDIT - CHARGE
WHEN "GUEST" THEN CREDIT - 5
WHERE CARDID = :NEW.CHARGEDCARDID
于 2012-12-25T14:47:29.687 回答
0

请运行此代码..

CREATE OR REPLACE TRIGGER TRG_PAYMONEY1
AFTER INSERT ON PAYMENTDEVICE 
FOR EACH ROW
BEGIN
UPDATE CARD c
SET c.credit=(select case when p.mealtype='STANDARD' then c.credit-c.charge
                 when p.mealtype='GUEST' then c.credit-5
            end result from PAYMENTDEVICE p,card c
           where c.cardid=p.chargedcardid)
WHERE CARDID = :NEW.CHARGEDCARDID;
END;
于 2012-12-26T13:10:54.270 回答
0

好吧,经过一些研究并感谢 Oracle 文档,我们通过这个实现解决了我们的问题:

CREATE OR REPLACE TRIGGER  "TRG_PAYMONEY" 
AFTER INSERT
ON PAYMENTDEVICE FOR EACH ROW

BEGIN
IF :NEW.MEALTYPE='GUEST' THEN
UPDATE CARD
SET CREDIT = CREDIT - 5
WHERE CARDID = :NEW.CHARGEDCARDID;
ELSE
UPDATE CARD
SET CREDIT = CREDIT - CHARGE
WHERE CARDID = :NEW.CHARGEDCARDID;
END IF;
END;

并感谢您的帮助和建议。

于 2012-12-26T21:11:04.703 回答