Oracle数据库—PL/SQL编程

PL/SQL块基本结构

declare

<declarations section> -- 声明部分

begin

<executable command(s)> -- 执行部分

exception

<exception handling> -- 异常处理部分

end;

声明部分:包含变量、常量定义,由 declare 关键字开始,如果不声明变量,可以省略这部分

执行部分:所有可执行 PL/SQL 语句放在这部分,由 begin 关键词开始, end 关键词结束,这部分不可省略,注意 end 后的分号

用 exception 关键词把可执行部分分成两个小部分,之前程序正常执行,一旦出现异常就跳转到异常部分执行

声明

变量声明

变量名 数据类型 [ not null ] [ := 初始值 ] | [ defacult 初始值 ]

常量声明

----常量声明后不可再次赋值

变量名 constant 数据类型 [ not null ] [ := 初始值 ] | [ defacult 初始值 ]

declare

  --声明变量并且初始化

  a number := 100;

  b number default 100;

  --声明属性变量

  c a%type := a; --声明c , c的类型是和a一样的类型

  myemp emp%rowtype;--声明mymep,myemp的类型是属性类型,记录类型,引用数据库表中的一行作为数据类型 , 访问里面属性用“ . ”

  myname emp.ename%type;--声明myid,myid的类型与emp表中的eid相同

begin

  dbms_output.put_line(a);

  dbms_output.put_line(c);

  --把emp表中eid='2018001'的ename赋值给myname

  select ename into myname from emp where eid='2018001';

  dbms_output.put_line(myname);

  --把emp表中eid='2018001'的那一条记录赋值给myemp

  select * into myemp from emp where eid='2018001';

  dbms_output.put_line(myemp.ename);

end;

PL/SQL IF-THEN语句

if 条件 then

----条件成立执行语句

end if;
declare

  score number default 60;

begin

  if score >= 60 then

    dbms_output.put_line('及格了!');

  end if;

end;

PL/SQL IF-THEN-ELSE语句

if 条件 then

----条件成立执行语句

else

----条件不成立执行语句

end if;


declare

  score number default 59;

begin

  if score >= 60 then

    dbms_output.put_line('及格了!');

  else

    dbms_output.put_line('不及格!');

  end if;

end;

PL/SQL IF-THEN-ELSE语句

第一种:

if 条件1 then

----条件1成立执行语句

elsif 条件2 then

----条件2成立执行语句

elsif 条件3 then

----条件3成立执行语句

else

----条件1、2、3都不成立执行语句

end if;
declare

  score number default 75;

begin

  if score>80 then

    dbms_output.put_line('优');

  elsif score between 60 and 80 then

    dbms_output.put_line('良');

  else

    dbms_output.put_line('差');

  end if;

end;
第二种:

if 条件1 then

----条件1成立执行语句

else if 条件2 then

----条件2成立执行语句

else if 条件3 then

----条件3成立执行语句

else

----条件1、2、3都不成立执行语句

end if;

end if;

end if;

注意:有几个if就有几个end if;
declare

  score number default 75;

begin

  if score>80 then

    dbms_output.put_line('优');

  else if score between 60 and 80 then

    dbms_output.put_line('良');

  else

    dbms_output.put_line('差');

  end if;

  end if;

end;

PL/SQL CASE语句

(如果选择器和表达式匹配就执行对应的执行语句,如果选择器与所有表达式都不匹配,就执行else后面执行语句)

case 选择器

when 表达式1 then 执行语句1;

when 表达式2 then 执行语句2;

when 表达式3 then 执行语句3;

when 表达式4 then 执行语句4;    

...

else 执行语句;

end case;

declare

  flag varchar2(50):=upper('&flag');

begin

  case flag

    when 'A' then dbms_output.put_line('优');

    when 'B' then dbms_output.put_line('良');

    when 'C' then dbms_output.put_line('差');

    else dbms_output.put_line('有问题');

  end case;

end;


declare

  flag varchar2(50):=upper('&flag');

  result0 varchar(50);

begin

  result0 :=

  case flag

    when 'A' then '优'

    when 'B' then '良'

    when 'C' then '差'

    else '有问题'

  end;

  dbms_output.put_line(result0);

end;
--用于搜索


declare

  flag varchar2(50):=upper('&flag');

  result0 varchar(50);

begin

  result0 :=

  case

    when flag='A' then '优'

    when flag='B' then '良'

    when flag='C' then '差'

    else '有问题'

  end;

  dbms_output.put_line(result0);

end;

PL/SQL LOOP语句

(无限循环语句,如果不添加 exit where 语句 或者 exit 语句就是无限循环,exit 类似于 break)

loop 

----循环体

----[ exit when 退出循环条件 ]

----[ if ... then 

           exit;

       end if ]

end loop;


declare

  i number:=1;

  sum0 number:=0;

begin

  loop

    sum0:=(sum0+i);

    i:=i+1;

    if i > 10 then

      exit;

    end if;

  end loop;

  dbms_output.put_line(i);

  dbms_output.put_line(sum0);

end;
declare

  i number:=1;

  sum0 number:=0;

begin

  loop

    sum0:=(sum0+i);

    i:=i+1;

    exit when i>10;

  end loop;

  dbms_output.put_line(i);

  dbms_output.put_line(sum0);

end;

PL/SQL WHILE语句

where 条件 loop

----循环体

end loop;
declare

  sum0 number:=0;

  i number:=1;

begin

  while i<=10 loop

    sum0:=(sum0+i);

    i:=i+1;

    exit when i>10;

  end loop;

  dbms_output.put_line(i);

  dbms_output.put_line(sum0);

end;

PL/SQL FOR语句

(reverse表示从循环上限到下限循环)

for 循环变量 in [ reverse ] 循环下限..循环上限 loop

----循环体

end loop;
declare

  sum0 number:=0;

begin

  for i in 1..10 loop

    sum0:=sum0+i;

  end loop;

  dbms_output.put_line(sum0);

end;

猜你喜欢

转载自yq.aliyun.com/articles/675842