PLSQL中的循环和IF语句

时间:2014-12-01 09:13:35

标签: plsql

我一直在尝试在我的PLSQL分配中使用IF ELSE

这是我当前代码的一部分

FETCH c2 into Mark, credit_hour;
    IF Mark = 'A+' THEN
        Points := Points + (4.0 * credit_hour);
    ELSIF Mark = 'A ' THEN
        Points := Points + (4.0 * credit_hour);
    ELSIF Mark = 'A-' THEN
        Points := Points + (3.7 * credit_hour);
    ELSIF Mark = 'B+' THEN
        Points := Points + (3.3 * credit_hour);
    ELSIF Mark = 'B ' THEN
        Points := Points + (3.0 * credit_hour);
    ELSIF Mark = 'B-' THEN
        Points := Points + (2.7 * credit_hour);
    ELSIF Mark = 'C+' THEN
        Points := Points + (2.3 * credit_hour);
    ELSIF Mark = 'C ' THEN
        Points := Points + (2.0 * credit_hour);
    ELSIF Mark = 'C-' THEN
        Points := Points + (1.7 * credit_hour);
    ELSIF Mark = 'D+' THEN
        Points := Points + (1.3 * credit_hour);
    ELSIF Mark = 'D ' THEN
        Points := Points + (1.0 * credit_hour);
    ELSEIF Mark = 'D-' THEN
        Points := Points + (0.7 * credit_hour);
    ELSE Mark = 'F' THEN
        Points := Points + (0.0 * credit_hour);
    END IF;

然后我认为我可以通过使用像

这样的双参数循环来缩短过程
FETCH c2 into Mark, credit_hour; 
for (i,k) in (A+,A,A-,B+,B,B-,C+,C,C-,D+,D,D-,F)        (4.0,4.0,3.7,3.3,3.0,2.7,2.3,2.0,1.7,1.3,1.0,0.7,0.0)

IF Mark = i THEN 
Points := Points + (k * credit_hour);

但我不知道如何在PLSQL中执行此操作

1 个答案:

答案 0 :(得分:1)

对于您当前的代码,我建议使用case when,因为这样更容易理解:

Points := Points
          + case Mark
            when 'A+' then 4.0
            when 'A ' then 4.0
            ...
            end
            * credit_hour;

或者,更好的是,创建一个(临时)表来存储值并创建一个select语句,可选择与原始表连接,然后你就不需要PL / SQL了所有