我的PL / SQL块出了什么问题?

时间:2014-10-10 14:18:13

标签: sql database stored-procedures plsql relational-database

我写了一个程序来计算学生的平均分数:

DECLARE 
    rno   NUMBER(10); 
    wt    NUMBER(10); 
    dbms  NUMBER(10); 
    se    NUMBER(10); 
    toc   NUMBER(10); 
    per   FLOAT(10); 
    total NUMBER(10); 
BEGIN 
    rno := &rno; 
    wt := &wt; 
    dbms := &dbms; 
    se := &se; 
    toc := &toc; 

    IF( wt < 40 OR se < 40 OR toc < 40 OR dbms < 40 ) THEN 

        dbms_output.Put_line('Fail'); 
        total := ( wt + se + toc + dbms ); 
        per := ( total / 400 ) * 100; 

        IF( per > 75 ) THEN 
            dbms_output.Put_line('grade A'); 
        ELSIF( per > 65 AND per < 75 ) THEN 
            dbms_output.Put_line('grade B'); 
        ELSIF( per > 40 AND per < 65 ) THEN 
            dbms_output.Put_line('grade c'); 
        ELSE 
            dbms_output.Put_line('Invalid Input'); 
        END IF; 

        dbms_output.Put_line('percentage is' ||per); 
    END IF; 
END; 
/ 

程序中没有错误。但该计划的产出如下:

Enter value for rno: 1
old  10:  rno:=&rno;
new  10:  rno:=1;
Enter value for wt: 23
old  11:  wt:=&wt;
new  11:  wt:=23;
Enter value for dbms: 56
old  12:  dbms:=&dbms;
new  12:  dbms:=56;
Enter value for se: 74
old  13:  se:=&se;
new  13:  se:=74;
Enter value for toc: 84
old  14:  toc:=&toc;
new  14:  toc:=84;

PL/SQL procedure successfully completed.

SQL> /
Enter value for rno: 2
old  10:  rno:=&rno;
new  10:  rno:=2;
Enter value for wt: 45
old  11:  wt:=&wt;
new  11:  wt:=45;
Enter value for dbms: 25
old  12:  dbms:=&dbms;
new  12:  dbms:=25;
Enter value for se: 73
old  13:  se:=&se;
new  13:  se:=73;
Enter value for toc: 22
old  14:  toc:=&toc;
new  14:  toc:=22;

PL/SQL procedure successfully completed.

该程序未达到'if'语句并且进一步。请帮忙。

1 个答案:

答案 0 :(得分:1)

就像它在这里Printing the value of a variable in SQL Developer所说的那样,如果你使用的是SQL Developer,你必须打开VIEW _&gt; DBMS_OUTPUT,然后单击绿色加号以启用输出。这可以在我的计算机上运行,​​它之前没有工作,但现在正在做你现在正在做的事情。现在,您所做的代码在我的计算机上工作正常,我得到输出:

Fail
Invalid Input
percentage is10

对于SQL_Plus

上设置serveroutput

做两件事:

1)告诉SQLPLUS在每个语句之后转储输出 2)它使sqlplus发出dbms_output.enable

编辑:你问题的第二个输入实际上很好

Fail
grade c
percentage is41.25