SQL分支语句与循环语句

分支语句

if then

elsif then

else

end if

举例:

set serveroutput on
declare
num number;
begin
  num:=3;
  if num = 5
    then
      dbms_output.put_line('这个数字是5');
    elsif num >7
    then 
      dbms_output.put_line('这个数字大于7');
    else
      dbms_output.put_line('其他数字');
  end if;
end;

循环语句

loop

exit when

end loop

例子:

set serveroutput on
declare
num number;
begin
  num := 1;
  select * into stemp from student s where s.sno='109'
  loop
    exit when num > 10;
    dbms_output.put_line(num);
    num := num + 1;
  end loop;
end;

while

loop

end loop

set serveroutput on
declare
num number;
begin
  num := 1;
  select * into stemp from student s where s.sno='109'
while num < 10 
    loop
      dbms_output.put_line(num);
      num := num + 1;
    end loop;
end;

for li in()

loop

end loop

例子:

set serveroutput on
declare
num number;
begin
  num := 1;
  select * into stemp from student s where s.sno='109'
for ii in (select sname from student)
    loop
      dbms_output.put_line(ii.sname);
    end loop;
end;

 

posted @ 2017-06-14 13:54  暗杠小发  阅读(2468)  评论(0编辑  收藏  举报