oracle 10g 学习之PL/SQL简介和简单使用(10)

PL /SQL是一种高级数据库程序设计语言,该语言专门用于在各种环境下对ORACLE数据库进行访问。由于该语言集成于数据库服务器中所以PL/SQL代码可以对数据进行快速高效的处理。PL/SQL是 Procedure Language & Structured Query Language 的缩写。ORACLE的SQL是支持ANSI(American national Standards Institute)和ISO92 (International Standards Organization)标准的产品。PL/SQL是对SQL语言存储过程语言的扩展。从ORACLE6以后,ORACLE的RDBMS附带了PL/SQL。它现在已经成为一种过程处理语言,简称PL/SQL。

PL/SQLORACLE系统的核心语言,现在ORACLE的许多部件都是由PL/SQL写成。在PL/SQL中可以使用的SQL语句有:

INSERT,UPDATE,DELETE,SELECT INTO,COMMIT,ROLLBACK,SAVEPOINT。

提示:在 PL/SQL中只能用 SQL语句中的 DML 部分,不能用 DDL 部分,如果要在PL/SQL中使用DDL(CREATE  table  )的话,只能以动态的方式来使用。

l  ORACLE 的 PL/SQL 组件在对 PL/SQL 程序进行解释时,同时对在其所使用的表名、列名及数据类型进行检查。

l  PL/SQL 可以在SQL*PLUS 中使用。

l  PL/SQL 可以在高级语言中使用。

l  PL/SQL可以 在ORACLE的 开发工具中使用。

l  其它开发工具也可以调用PL/SQL编写的过程和函数,如Power Builder 等都可以调用服务器端的PL/SQL过程。

PL/SQL程序由三个块组成,即声明部分、执行部分、异常处理部分

PL/SQL块的结构如下:

 

DECLARE 

/* 声明部分: 在此声明PL/SQL用到的变量,类型及游标,以及局部的存储过程和函数 */

BEGIN

    /*  执行部分过程及SQL 语句  , 即程序的主要部分  */

EXCEPTION

   /* 执行异常部分: 错误处理  */

END;

其中 执行部分是必须的。

标识符

PL/SQL程序设计中的标识符定义与SQL 的标识符定义的要求相同。要求和限制有:

l  标识符名不能超过30字符;

第一个字符必须为字母;

不分大小写;

l  不能用’-‘(减号);

l  不能是SQL保留字。

提示一般不要把变量名声明与表中字段名完全一样,如果这样可能得到不正确的结果.

例如:下面的例子将会删除所有的纪录,而不是KING 的记录;

DECLARE

   Ename varchar2(20) :=’KING’;

BEGIN

       DELETE FROM emp WHERE ename=ename;

END;

变量命名在PL/SQL中有特别的讲究,建议在系统的设计阶段就要求所有编程人员共同遵守一定的要求,使得整个系统的文档在规范上达到要求。下面是建议的命名方法

标识符

命名规则

例子

程序变量

V_name

V_name

程序常量

C_Name

C_company_name

游标变量

Name_cursor

Emp_cursor

异常标识

E_name

E_too_many

表类型

Name_table_type

Emp_record_type

Name_table

Emp

记录类型

Name_record

Emp_record

SQL*Plus 替代变量

P_name

P_sal

绑定变量

G_name

G_year_sal

记录类型

记录类型是把逻辑相关的数据作为一个单元存储起来,称作PL/SQL RECORD 的域(FIELD),其作用是存放互不相同但逻辑相关的信息

定义记录类型语法如下:

 

TYPE record_type IS RECORD(

   Field1 type1  [NOT NULL]  [:= exp1 ],

   Field2 type2  [NOT NULL]  [:= exp2 ],

   . . .   . . .

   Fieldn typen  [NOT NULL]  [:= expn ] ) ;

例如

declare

         type test_rec is record(

         name varchar2(30),

         id number(4)

);

v_emp test_rec;

begin

         v_emp.name:=’Tom’;

         v_emp.id:=1234;

         dbms_output.put_line(v_emp.name||’,’||v_emp.id);

end;

使用select…into 对变量进行赋值

declare

         type test_rec is record(

         name varchar2(30),

         id number(4)

);

v_emp test_rec;

begin

         select last_name,department_id into v_emp from employees where employee_id = 200;

         dbms_output.put_line(v_emp.name||’,’||v_emp.id);

end;

提示: 1) DBMS_OUTPUT.PUT_LINE 过程的功能类似于 Java 中的 System.out.println() 直接将输出结果送到标准输出中.

        2) 在使用上述过程之前必须将 SQL * PLUS 的环境参数 SERVEROUTPUT 设置为 ON, 否则将看不到输出结果:  set serveroutput on

0. 准备工作:

set serveroutput on—这个是sql*plus语言,让sql*plus支持输出

hellowrold 程序

begin

dbms_output.put_line('hello world');

end;

/

1. 使用一个变量

declare

  --声明一个变量

  v_name varchar2(25);

begin

  --通过 select ... into ... 语句为变量赋值

 select last_name into v_name

 from employees

 where employee_id = 186;

 -- 打印变量的值

 dbms_output.put_line(v_name);

end;

2. 使用多个变量

declare

  --声明变量

  v_name varchar2(25);

  v_email varchar2(25);

  v_salary number(8, 2);

  v_job_id varchar2(10);

begin

  --通过 select ... into ... 语句为变量赋值

 select last_name, email, salary, job_id into v_name, v_email, v_salary, v_job_id

 from employees

 where employee_id = 186;

 -- 打印变量的值

 dbms_output.put_line(v_name || ', ' || v_email || ', ' ||  v_salary || ', ' ||  v_job_id);

end;

3. 自定义记录类型

declare

  --定义一个记录类型

  type emp_record is record(

    v_name varchar2(25),

    v_email varchar2(25),

    v_salary number(8, 2),

    v_job_id varchar2(10));

   

  --声明自定义记录类型的变量

  v_emp_record emp_record;

begin

  --通过 select ... into ... 语句为变量赋值

 select last_name, email, salary, job_id into v_emp_record

 from employees

 where employee_id = 186;

 -- 打印变量的值

 dbms_output.put_line(v_emp_record.v_name || ', ' || v_emp_record.v_email || ', ' || 

                                        v_emp_record.v_salary || ', ' ||  v_emp_record.v_job_id);

end;

4. 使用 %type 定义变量

declare

  --定义一个记录类型

  type emp_record is record(

    v_name employees.last_name%type,

    v_email employees.email%type,

    v_salary employees.salary%type,

    v_job_id employees.job_id%type);

   

  --声明自定义记录类型的变量

  v_emp_record emp_record;

begin

  --通过 select ... into ... 语句为变量赋值

 select last_name, email, salary, job_id into v_emp_record

 from employees

 where employee_id = 186;

 -- 打印变量的值

 dbms_output.put_line(v_emp_record.v_name || ', ' || v_emp_record.v_email || ', ' || 

                                        v_emp_record.v_salary || ', ' ||  v_emp_record.v_job_id);

end;

5. 使用 %rowtype

declare

  v_emp_record employees%rowtype;

begin

  --通过 select ... into ... 语句为变量赋值

 select * into v_emp_record

 from employees

 where employee_id = 186;

 -- 打印变量的值

 dbms_output.put_line(v_emp_record.last_name || ', ' || v_emp_record.email || ', ' || 

                                        v_emp_record.salary || ', ' ||  v_emp_record.job_id  || ', ' || 

                                        v_emp_record.hire_date);

end;

6. 赋值符号

declare

  v_emp_record employees%rowtype;

  v_employee_id employees.employee_id%type;

begin

  --使用赋值符号位变量进行赋值

  v_employee_id := 186;

  --通过 select ... into ... 语句为变量赋值

 select * into v_emp_record

 from employees

 where employee_id = v_employee_id;

 -- 打印变量的值

 dbms_output.put_line(v_emp_record.last_name || ', ' || v_emp_record.email || ', ' || 

                                        v_emp_record.salary || ', ' ||  v_emp_record.job_id  || ', ' || 

                                        v_emp_record.hire_date);

end;

7. 使用 IF ... THEN ... ELSIF ... THEN ... END IF;

要求: 查询出 150 员工的工资, 若其工资大于 10000 则打印 'salary > 10000'; 若在 5000 到 10000 之间, 则打印 '5000< salary <= 10000'; 否则打印 'salary <= 5000'

declare

  v_salary employees.salary%type;

begin

  --通过 select ... into ... 语句为变量赋值

 select salary into v_salary

 from employees

 where employee_id = 139;

 dbms_output.put_line('salary: ' || v_salary);

 -- 打印变量的值

 if v_salary > 10000 then

    dbms_output.put_line('salary > 10000');

 elsif v_salary > 5000 then

    dbms_output.put_line('5000 < salary <= 10000');

 else

    dbms_output.put_line('salary <= 5000');

 end if;

end

8. 使用 case ... when 完成上面的任务

declare

       v_sal employees.salary%type;

       v_msg varchar2(50);

begin

      

       select salary into v_sal

       from employees

       where employee_id = 100;

      

       --case 不能向下面这样用

       /*

       case v_sal when salary > 10000 then v_msg := '>10000'

                  when salary > 5000 then v_msg := '5000< salary <= 10000'

                  else v_msg := 'salary <= 5000'

       end;

       */

      

       v_msg :=

             case trunc(v_sal / 5000)

                  when 0 then 'salary <= 5000'

                  when 1 then '5000< salary <= 10000'

                  else 'salary > 10000'

             end;

      

       dbms_output.put_line(v_msg);

end;

9. 使用循环语句打印 1 - 100.

1).

declare

       v_i number(3) := 1;

begin

       loop

        dbms_output.put_line(v_i);

        exit when v_i = 100;

        v_i := v_i + 1;

       end loop;

end;

2).

declare

       v_i number(3) := 1;

begin

       while v_i <= 100 loop

             dbms_output.put_line(v_i);

             v_i := v_i + 1;

       end loop;

end;

3).

begin

       for i in 1 .. 100 loop

             dbms_output.put_line(i);

       end loop;

end;

10. 使用 goto

PL/SQL中GOTO语句是无条件跳转到指定的标号去的意思。语法如下:

 

GOTO   label;

 . . .  . . .

<<label>> /*标号是用<< >>括起来的标识符 */

declare

  --标记值, 若为 1 则是素数, 否则不是

  v_flag number(1) := 0;

begin

   for i in 2 .. 100 loop

       v_flag := 1;    

        

       for j in 2 .. i - 1 loop

           if i mod j = 0 then

              v_flag := 0;

              goto label;

           end if;       

       end loop;

      

       <<label>>

       if v_flag = 1 then

           dbms_output.put_line(i);

       end if;

      

   end loop;

end;

原文地址:https://www.cnblogs.com/yxlblogs/p/3464257.html