在IBM DB2中创建存储过程时出现语法错误

时间:2017-02-01 14:08:20

标签: stored-procedures db2

我无法执行该程序,错误说明:

  

在“ent.payment_id)* 0.50”之后发现意外的令牌“END-OF-STATEMENT”。预期的代币可能包括:“”。 LINE NUMBER = 6。 SQLSTATE = 42601。

此过程用于在代码有效时更新总价。我尝试了两种方法。有人可以帮忙吗?

create procedure Prc_Discount(in code char(3), payment_id integer)
begin
 if (code ='abc')then
update payment
set new.total_price =(select total_price from payment
where new.payment_id=payment.payment_id)*0.50;
else
 if (code ='bac')then
update payment
set new.total_price =(select total_price from payment
where new.payment_id=payment.payment_id)*0.75;
else
 if (code ='cba')then
update payment
set new.total_price =(select total_price from payment
where new.payment_id=payment.payment_id)*0.90;
end if;
end@

另一次尝试:

create procedure Prc_Discount(in code char(3), payment_id integer)
begin
case code
   when abc then                      
      update payment
      set new.total_price =(select total_price from payment
      where new.payment_id=payment.payment_id)*0.50;         
   when acd then                                                     
      update payment
      set new.total_price =(select total_price from payment
      where new.payment_id=payment.payment_id)*0.75; 
   else      
      update payment
      set new.total_price =(select total_price from payment
      where new.payment_id=payment.payment_id)*0.90;         
end case; 
end@

2 个答案:

答案 0 :(得分:0)

在你的程序中尝试这个

update payment f1
set f1.total_price =
        case 
        when code ='abc' then 0.50 * f1.total_price
        when code ='bac' then 0.75 * f1.total_price
        when code ='cba' then 0.90 * f1.total_price
        else f1.total_price
        end
where code in ('abc', 'bac', 'cba') 

答案 1 :(得分:0)

尝试将以上内容放入您的代码中:

“-”

将“ end @”替换为“ end;”