PL/SQL结构学习

PL/SQL结构分为三块:declare、begin、exception。其中begin必不可少。

块一:

declare的作用是申明变量,包括变量的名称及其相关属性和是否定义初始值。

其相关属性包括数据类型,长度。 其初始值定义时可以用:=也可以用default,当用constant对数据类型进行修饰之后,必须赋予初始值。

其次,对于变量的类型也可以定义为记录型:%rowtype ;也可以定义成与某个字段类型相同,如%type。

实例:

1.%type

declare
V_ename emp.ename%type ;
begin
  select ename into V_ename from emp where empno = 7788 ;
  dbms_output.put_line('员工姓名为:'||V_ename) ;
end ;
/

2.%rowtype

declare
V_empRecord emp%rowtype ;
begin
  select * into V_empRecord from emp where empno = 7788 ;
  dbms_output.put_line('员工姓名:'||V_empRecord.ename) ;
  dbms_output.put_line('员工职位:'||V_empRecord.job) ;
  dbms_output.put_line('部门编号:'||V_empRecord.deptno) ;
  dbms_output.put_line('薪水:'||V_empRecord.sal) ;
  end ;
  /

块二:

begin的作用是程序控制,它的内容包含SQL语句和逻辑语句。如,条件分支结构,循环结构。

条件分支结构:

结构,

1)if 条件 then

    statement1 ;

  end if ;

 2)if 条件 then

    statement1 ;

  else

    statement2 ;

  end if ;

3)if 条件 then

    statement1 ;

  elsif

    statement2 ;

  else

    statement3 ;

  end if ;

实例:

1. 
declare
V_empno emp.empno%type:=7788 ;
V_sal emp.sal%type ;
begin
 select sal into V_sal from emp where V_empno = empno ;
 if V_sal < 1500 then
 update emp set sal = sal + 100 where empno = V_empno ;
 end if ;
 end ;
  / 

循环结构:

结构,

1)基本循环,

LOOP

  语句序列

  EXIT WHEN 布尔型表达式(为真) ;

END LOOP ;

实例:

1.
--向部门表中插入10条数据,部门编号从41到50
declare
    V_counter binary_integer:=41 ; --pl/sql整型数据类型 binary_integer                            
    begin
      LOOP
      insert into dept(deptno,dname) values(V_counter,'loop') ;--语句序列
      V_counter:=V_counter + 1 ;
      EXIT WHEN V_counter > 50 ;--布尔表达式(为真)
      END LOOP ;
      commit ;
      end ;
      /

2)WHILE_LOOP循环,

WHILE 条件 LOOP

  语句序列

END LOOP ;

实例:

1.
--向部门表中插入10条数据,部门编号从51到60
declare
V_counter binary_integer:=51 ;
begin
  WHILE V_counter <= 60 LOOP  --条件
  insert into dept(deptno,dname) values (V_counter,'while loop') ; --语句序列
  V_counter := V_counter + 1 ;
END LOOP ;
commit ;
end;
/

3)数值for_LOOP循环,

FOR 计数器 IN 低界...高界 LOOP

  语句序列

END LOOP ;

 实例:

--向部门表中插入5条数据,部门编号从61到65
declare
  V_counter binary_integer ;
begin
  FOR V_counter IN 61..65  LOOP  --FOR 计数器 IN [REVERES]低界..高界  LOOP
  insert into dept(deptno,dname) values (V_counter,'for loop') ;--语句序列
END LOOP ;
commit ;
end ;
/

块三:异常处理exception

结构,

exception

  WHEN first_exception THEN <code to handle first exception>

  WHEN second_exception THEN <code to handle second exception>

  WHEN others THEN <code to handle others exception>

实例:

--EXCEPTION 实例
declare
V_job emp.job%type := 'ANALYST' ;
V_sal emp.sal%type ;
begin
  select sal into V_sal from emp where job = V_job ;
  if V_sal <= 1500 then  --condition
 update emp set sal = sal + 100 WHERE job = V_job ;  --statement1
  end if ;
exception
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.put_line('数据库中没有编码为'||V_job||'的员工') ;
  WHEN TOO_MANY_ROWS THEN
    DBMS_OUTPUT.put_line('程序运行错误!请使用游标') ; 
  WHEN OTHERS THEN
    DBMS_OUTPUT.put_line('发生其它错误!') ;
end ;
/ --由于有多个记录的job是‘ANALYST’,因此在执行程序的过程中无法给未指明的记录进行更新因而报错。如果没有异常处理,程序将无法执行而停止。而异常处理使得程序执行完毕,不影响之后的程序。

posted @ 2015-06-05 11:05  zzwxf9h  阅读(162)  评论(0编辑  收藏  举报