plsql基础语法

来源:互联网 发布:东莞 体验 全套 知乎 编辑:程序博客网 时间:2024/05/16 04:55

--while循环
declare num1 number;
begin
num1:=1;
while num1=1 loop
dbms_output.put_line(to_char(num1));
num1:=2;
end loop;
end;

--if.....else
declare num2 number;
begin
num2:=3;
if num2=2 then
dbms_output.put_line('if success');

else
dbms_output.put_line('else success');
end if;
end;

--if....else if....else
declare num3 number;
begin
num3:=3;
if num3=2 then
dbms_output.put_line('if success');
elsif num3=3 then
dbms_output.put_line('if..else success');
else
dbms_output.put_line('else success');
end if;
end;

--case的用法
declare num4 number;
begin
num4:=4;
case num4
when 1 then dbms_output.put_line('num4=1');
when 2 then dbms_output.put_line('num4=2');
when 3 then dbms_output.put_line('num4=3');
when 4 then dbms_output.put_line('num4=4');
else dbms_output.put_line('num4=无限大');
end case;
end;

--loop end 循环
   declare
      x  number;
 begin
      x  := 0;
      loop
      x:=x+1;
      dbms_output.put_line(to_char(x));
      exit  when  x = 10;
end loop;

  end;
--for循环
begin
for i in reverse 1..10
loop
dbms_output.put_line('i='||i);
end loop;
end;

显式光标处理需四个 PL/SQL步骤:

 

cursor  光标名称  is  select 语句;

open   光标名称;

Fetch  光标名称  into  变量列表;

Close  光标名称;

declare
cursor get_well is select well_id,well_common_name from cd_well_source where rownum<5;
wellId varchar2(50);
wellCommonName varchar2(50);
begin
open get_well;
fetch get_well into wellId,wellCommonName;
while get_well%found
loop
dbms_output.put_line(wellCommonName);
fetch get_well into wellId,wellCommonName;
end loop;
close get_well;
end;

原创粉丝点击