欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

PL/SQL 基础( 上 )

发布时间:2025/3/19 数据库 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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

variablev_namev_sal
constantc_namec_company_name
cursorname_cursoremp_cursor
exceptione_namee_too_many
table typename_table_typeamount_table_type
tablename_tablecountry_table
record typename_recordcustomer_record
recordname_recordcustomer_record
substitutionp_namep_sal
host or bind variableg_nameg_year_sal
3. 输出

DBMS_OUTPUT.PUT_LINE()

4. 注释

-- 单行注释

/**/ 多行注释

5. scope

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

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 区分

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

9. 特殊的操作符号

** 幂运算

~= 约等于

<>, != , ^= 不等于

转载于:https://www.cnblogs.com/moveofgod/archive/2012/11/16/2773279.html

总结

以上是生活随笔为你收集整理的PL/SQL 基础( 上 )的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。