PL/SQL 基础( 上 )

预备 ( PL/SQL 好处 )

Integration : 集成度高 ( server 和 application 中都有 , 例如 : Oracle Developer )

Improved performance : ( 必须系统中计算工时 , 如果使用 procedure , 则只需要传几个参数过去 , 可以很少的使用 network ,

而如果没有使用 procedure , 则每条记录的情况都要传给SERVER, 假如公司有10000人 , 则就需要使用 network很多 )

Modularized program development : 使用模块 ( begin ... end ) , Place reusable PL/SQL code in library to be shared between Oracle Form and Oracle Reports.

Portable , you can declare varibales . ( 可以复制到别的 SERVER 中执行 )

You can program with control structure . ( include handle errors )

匿名块不保存在数据库内部. ( block ) ( 执行完就没有了 )

1.变量定义

变量种类 : PL/SQL变量,系统绑定变量

PL/SQL变量 : scalar , composite , reference , LOB ( large objects ) ( 在此 只讨论 scalar , composite )

系统绑定变量 : 定义在外部环境 ( i*SQL ) 中,可以用做参数传递的变量。

identifier [CONSTANT] datatype [NOT NULL] [:= | default exp];

例如 :

declare

v_depno number(3) NOT NULL := 100; ( 有 NOT NULL 必须给值 )

c_comn CONSTANT number(4) := 1400; ( 有 CONSTANT 必须初始化 )

v_location varchar(20) := “Hello'’world”( 如果字符串中有’,那么必须两次使用,成对出现 )

%TYPE ( 前面定义过的变量,或者是TABLEZ中的列 )

%ROWTYPE

v_name employees.last_name%TYPE

v_balance number(10,2) ;

v_min_balance v_balance%TYPE := 10;

v_ename := LOWER( v_ename ) ;  可以使用函数

字符串问题,例如 tom’s home

v_home = q’!tom’s home!’ 或者 q’[! tom’s home ]’其中主要格式为 q’分隔符,例如! [] 等等

当然,因为限定成对出现,所以如果你写 “tom'’s home” 也是可以的。

declare 声明部分的变量在执行语句中不需要加 : 冒号 例如:

   1:  DECLARE
   2:    v_bonus  NUMBER(6);
   3:  BEGIN
   4:    SELECT salary * 0.01
   5:    INTO v_bonus      -- 注意此处没有冒号
   6:    FROM employees
   7:    WHERE EMP_ID = '2008491';
   8:  END;

Bind variables : 变量是在 host environment中定义,主要是提供参数给 PL/SQL BLOCK

系统绑定变量,可以通过 print 显示内容, 例如 PRINT g_n ,

VARIABLE return_code NUMBER ( 可以直接在PL/SQL中使用,不用再 declare , 此种变量必须要冒号 :

   1:  VARIABLE g_salary NUMBER
   2:  BEGIN
   3:    SELECT salary
   4:    INTO :g_salary        -- 注意此处有冒号
   5:    FROM EMPLOYEES
   6:    WHERE EMPLOYEE_ID = '20080504';
   7:  END;

PLSQL variables assignments always use :=

SQL column assignments always use =

不可以在 PL/SQL 中使用的函数 :

- DECODE

- GROUP functions ( AVG, MIN, MAX, COUNT, SUM, STDDEV, VARIANCE )

  group functions apply to groups of rows in a table and therefore are availiable only in SQL statements in a PL/SQL block.

  也就是说,在PL/SQL中的SQL中可以使用以上函数。

2. 命名规则

identifier

Naming Convention

Example

variable v_name v_sal
constant c_name c_company_name
cursor name_cursor emp_cursor
exception e_name e_too_many
table type name_table_type amount_table_type
table name_table country_table
record type name_record customer_record
record name_record customer_record
substitution p_name p_sal
host or bind variable g_name g_year_sal
3. 输出

DBMS_OUTPUT.PUT_LINE()

4. 注释

-- 单行注释

/**/ 多行注释

5. scope

同 C 一样, PL/SQL 中的变量也是有 SCOPE 的

image

6. 运行

/ A slash( / ) runs the PL/SQL block in a script file or in some tools such as isql*plus.

7. 在 PL/SQL 中使用函数

v_ename := LOWER( v_ename) ;

Most of the SQL function can bu used in PL/SQL ,

PL /SQL has its own error handling functions which are : ( SQLCODE, SQLERRM )

8. Qualify an identifier

标识符可以使用 label 区分

image

注意 : 上边的 outer.birthdate ( 这样在内部的 begin end , 可以访问外部变量 )

9. 特殊的操作符号

** 幂运算

~= 约等于

<>, != , ^= 不等于

原文地址:https://www.cnblogs.com/moveofgod/p/2773279.html