PL / SQL这个if if语句会工作吗?

时间:2017-10-31 22:11:43

标签: sql if-statement plsql oracle10g

如果p_CreditHour介于0到30之间(包括),系统会在屏幕上显示“此学生是新生”;如果在31到60学分之间,打印“这个学生是大二学生”,在61-90学分之间,打印“这个学生是少年”;超过91学分,打印“这名学生是高年级学生。”

以下程序是否反映了上一个问题的逻辑?

IF credit <= 30 THEN
     dbms_output.putline ('This student is a Freshmen'.);
END IF;
IF credit <= 60 THEN
     dbms_output.putline ('This student is a
Sophomore.');
END IF;
IF credit <= 90 THEN
     dbms_output.putline ('This student is a Junior.');
END IF;
IF credit > 90 THEN
     dbms_output.putline ('This student is a Senior.');
END IF;

3 个答案:

答案 0 :(得分:1)

不会,因为15小于30,60和90,所以学生将成为Frechman,大二学生和少年。但是Oracle有一个你应该使用的ELSIF:

IF credit <= 30 THEN
     dbms_output.putline ('This student is a Freshmen'.);
ELSIF credit <= 60 THEN
     dbms_output.putline ('This student is a Sophomore.');
ELSIF credit <= 90 THEN
     dbms_output.putline ('This student is a Junior.');
ELSE
     dbms_output.putline ('This student is a Senior.');
END IF;

答案 1 :(得分:1)

或者,有CASE声明。

begin 
  for s in ( select 1 student, 25 credits from dual union all 
             select 2 student, 35 credits from dual union all 
             select 3 student, 65 credits from dual union all 
             select 4 student, 85 credits from dual union all 
             select 5 student, 95 credits from dual 
           ) 
    loop 
      dbms_output.put( 'Student ' || to_char(s.student) || ' is a ');
      case  when s.credits <= 30 then dbms_output.put_line(' Freshman.');
            when s.credits <= 60 then dbms_output.put_line(' Sophomore.');
            when s.credits <= 90 then dbms_output.put_line(' Junior.');
            else  dbms_output.put_line(' Senior.');
      end case;
    end loop; 
end;

答案 2 :(得分:0)

或者您可以使用ADD语句。

IF credit <= 30 THEN
 dbms_output.putline ('This student is a Freshmen'.);
ElSIF credit <= 60 AND credit > 30 THEN
 dbms_output.putline ('This student is a Sophomore.');
ELSIF credit <= 90 AND credit > 60 THEN
 dbms_output.putline ('This student is a Junior.');
ELSE
 dbms_output.putline ('This student is a Senior.');
END IF;