如何在SAP HANA中的过程中分配变量

时间:2017-08-23 07:11:26

标签: sql stored-procedures hana hana-sql-script

这里我尝试根据使用程序订购的数量给出折扣。但我不知道如何在过程中为列中的变量赋值。

这里是我的代码:

create column table "KABIL_PRACTICE"."SALES_IF_ELSE"
(
"SALES_ID" integer,
"PRODUCT_ID" integer,
"QTY" integer,
"DISCOUNT" integer,
"SALES_AMOUNT" Double
);

insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (1,101,15,1500);
insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (2,102,25,2500);
insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (3,103,35,3500);
insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (4,104,40,4000);
insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (5,105,27,2700);
insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (6,106,32,3200);
insert into "KABIL_PRACTICE"."SALES_IF_ELSE" ("SALES_ID","PRODUCT_ID","QTY","SALES_AMOUNT") values (7,107,19,1900);

create procedure "KABIL_PRACTICE"."IF_ELSE_DISC"
language sqlscript
as begin
declare QTY integer ;
select "QTY" from "KABIL_PRACTICE"."SALES_IF_ELSE" := QTY;
if ( QTY > 25)
then
update "KABIL_PRACTICE"."SALES_IF_ELSE" set "DISCOUNT" = 5;
else if
update "KABIL_PRACTICE"."SALES_IF_ELSE" set "DISCOUNT" = 1;
end if;
end if;
end;
任何人都帮助我......

2 个答案:

答案 0 :(得分:1)

这是一个不经常使用SQL但是试图强制SQL数据库上的命令式编程风格的经典案例。 您要做的就是根据条件更新表格,不是吗?

在这种情况下,根本不需要程序逻辑(对于任何SQL数据库都是如此)。

UPDATE "KABIL_PRACTICE"."SALES_IF_ELSE" 
SET "DISCOUNT" = (CASE WHEN "QTY" >25 
                     THEN 2 
                     ELSE 1 
                  END) ;

这将在单个命令中将计算出的"DISCOUNT"值存储在表中而不进行循环。

答案 1 :(得分:0)

您可以通过在列中选择列来分配变量。例如:

SELECT QTY INTO QTY FROM KABIL_PRACTICE.SALES_IF_ELSE