32
第 18 第 PL/SQL

第 18 课 PL/SQL

Embed Size (px)

DESCRIPTION

第 18 课 PL/SQL. PL/SQL 的概念 BLOCK FUNCTION PROCEDURE. 教学目标. PL/SQL 是 Oracle 对 SQL 规范的扩展,是一种块结构语言 . 构成一个 PL/SQL 程序的基本单位(过程、函数和无名块)是逻辑块 . 可包含任何数目的嵌套子块。 PL/SQL 与一般结构化语言一样,有变量和常量 ,控制语句 ,循环语句等。 PL/SQL 与 sql 一样,不区分大小写。 看 :fun_proc.txt 中的各程序段。. PL/SQL 的概念. DECLARE --- 说明 BEGIN --- 语句序列 - PowerPoint PPT Presentation

Citation preview

Page 1: 第 18 课 PL/SQL

第 18 课PL/SQL

Page 2: 第 18 课 PL/SQL

• PL/SQL 的概念

• BLOCK

• FUNCTION

• PROCEDURE

教学目标

Page 3: 第 18 课 PL/SQL

• PL/SQL 是 Oracle 对 SQL 规范的扩展,是一种块结构语言 .• 构成一个 PL/SQL 程序的基本单位(过程、函数和无名块)是逻辑块 . 可包含任何数目的嵌套子块。• PL/SQL 与一般结构化语言一样,有变量和常量 ,控制语句 ,循环语句等。• PL/SQL 与 sql 一样,不区分大小写。• 看 :fun_proc.txt 中的各程序段。

PL/SQL 的概念

Page 4: 第 18 课 PL/SQL

• DECLARE• --- 说明• BEGIN• --- 语句序列• EXCEPTION• --- 例外处理程序• END ;

Block Structure

Page 5: 第 18 课 PL/SQL

Block Structure

– DECLARE (optional)Define PL/SQL objects to be used within this b

lock– BEGIN ( 强制 )

Executable statements– EXCEPTION (optional)

What to do if the executable action causes an error condition– END; ( 强制 )

Page 6: 第 18 课 PL/SQL

Development Environments

– SQL*Plus使用 PL/SQL engine in the Oracle Server

– Procedure Builder使用 the PL/SQL engine in the client tool or in

the Oracle Server

Page 7: 第 18 课 PL/SQL

• 第一个例子: fun_proc.txt• DECLARE• num_a NUMBER := 6;• num_b NUMBER;• BEGIN• num_b := 0;• num_a := num_a / num_b;• num_b := 7;• dbms_output.put_line(' Value of num_b ' || n

um_b);• EXCEPTION• WHEN ZERO_DIVIDE• THEN• dbms_output.put_line('Trying to divide b

y zero');• dbms_output.put_line(' Value of num_a '

|| num_a);• dbms_output.put_line(' Value of num_b '

|| num_b);• END;

Page 8: 第 18 课 PL/SQL

• 注: dbms_output.put_line (…)是一个 oracle内置包中的函数,可以在 sqlplus 中输出()内的值。• PL/SQL 使用两种注释:1. 单行注释: -- 注释文字2. 多行注释: /*……..• …………………………*/• EXCAMPLE:fun_proc.txt 中的匿名 block whith

comment

PL/SQL 的注释

Page 9: 第 18 课 PL/SQL

• 支持 SQL: 几乎所有 SQL 可以在 PL/SQL使用 ;

• 生产率高 ;

• 性能好 ;

• 可移植性 :在不同平台代码相同 ;• 与 ORACLE 集成 .

PL/SQL 的优点

Page 10: 第 18 课 PL/SQL

• 可重用性: PL/SQL blocks can be named and stored in the Oracle server and reused as required in another PL/SQL program or from the SQL com-mand line. • The small unit of code is more manageable and reusable.• 安全性高: Security on PL/SQL structures stored in the server can be managed using the Oracle database objects syntax.• You can grant and revoke privileges on these stored PL/SQL programs to and from other users in the database.

PL/SQL 的优点

Page 11: 第 18 课 PL/SQL

• 减少网络压力:• With SQL, Oracle must process each SQL statement one at a time.• In a networked environment, this means that a separate call must be made to the Oracle server each time a SQL statement is issued, • With PL/SQL, the entire block of statements is sent to the Oracle server at one time, reducing network traffic.

PL/SQL 的优点

Page 12: 第 18 课 PL/SQL

• PL/SQL 运行系统是一种技术,不是一种独立产品,• 它是 PL/SQL 块和子程序的一种解释器,它可接收任何有效的 PL/SQL 块或子程序。最终,变为 SQL.• PL/SQL 机可执行过程性语句,将对应 SQL 语句发送到 ORACLE 服务器上的 SQL 语句执行器。 • 见图: plsql_Engine.pdf

PLSQL的运行原理

Page 13: 第 18 课 PL/SQL

Developing Procedures and Functions Using SQL*Plus

Page 14: 第 18 课 PL/SQL

PL/SQL Program Constructs

匿名匿名 blockblock

ApplicationApplicationtriggertrigger

Stored Stored procedure/procedure/

functionfunction

DatabaseDatabasetriggertrigger

ApplicationApplicationprocedure/procedure/

functionfunction

PackagePackage

DECLAREDECLARE

BEGINBEGIN

EXCEPTIONEXCEPTION

END;END;

Object typeObject type

Page 15: 第 18 课 PL/SQL

1. A PL/SQL block can be a named block or an anonymous (匿名匿名) block.2. 前面的例子就是一个匿名 block. 匿名 blocks can be used in the server side (在ORACLE SERVER 中) or client side( DEVELOPER2000 系列) .3. There are two types of named blocks:Function,Procedure

PL/SQL

Page 16: 第 18 课 PL/SQL

1. 函数:2. Function Takes zero or more parameter values and returns one value3. 过程:4. Procedure Takes zero or more parameter values and may return value through parameters.

PL/SQL

Page 17: 第 18 课 PL/SQL

• FUNCTION name [(parameter [,parameter,…])] RETURN datatype• IS // 或 AS• [local declarations ]• BEGIN• executable statements• [EXCEPTION• exception handlers ]• END [name ];

PL/SQL

Page 18: 第 18 课 PL/SQL

1. A function accepts zero or more input parameters and returns one value.2. Function 通过其返回值,返回结果。3. The datatype of the return value is defined when the function is created. 注意:并不定义长度。4. User-defined functions can be used in the same way as Oracle built-in single-row functions.• EXCAMPLE:fun_proc.txt 中的 find_area

PL/SQL

Page 19: 第 18 课 PL/SQL

• PROCEDURE name [(parameter [,parameter,…])]

• IS // 或 AS• [local declarations ]• BEGIN• executable statements• [EXCEPTION• exception handlers ]• END [name ];

PL/SQL

Page 20: 第 18 课 PL/SQL

1. A procedure is a PL/SQL block that accepts zero or more parameters as input (IN), output (OUT), or both (INOUT).

2. Unlike functions, procedures do not return a value;

3. the INOUT parameter or OUT parameter may be used instead to pass a value from the procedure.

PL/SQL

Page 21: 第 18 课 PL/SQL

1. Procedures cannot be used in SQL statements; 2. 在 SQLPLUS 中用 EXECUTE , CALL 语句调用• EXCAMPLE:fun_proc.txt 中的 get_area, my_first_proc1. 在 PL/SQL block 内直接调用 :

PL/SQL

Page 22: 第 18 课 PL/SQL

1. function 的参数可以使用 out 定义吗?create or replace function find_area

(vlength in number,vwidth in number)return number….create or replace function find_area

(vlength in number,vwidth in number,varea out number )return number…. 正确吗?EXAMPLE:fun_proc.txt

PL/SQL

Page 23: 第 18 课 PL/SQL

1. 存储于数据库中2. A function is created in the database using a CREATE OR REPLACE command.3. A procedure created in the database using a CREATE OR REPLACE command.4. 存储于数据库中的函数,过程是数据库对象。叫存储过程• EXAMPLE:fun_proc.txt

PL/SQL

Page 24: 第 18 课 PL/SQL

1. PL/SQL blocks may be compiled separately and stored in the database,this is called a 存储过程 .2. Function,procedure都是存储过程。3. 存储过程经编译和优化后存储在数据库服务器中,使用时只要调用即可 4. 在 ORACLE 中,若干个有联系的过程可以组合在一起构成程序包。

存储过程

Page 25: 第 18 课 PL/SQL

• 练习:• 建立一个求圆面积的函数• 建立一个圆面积的过程,测试

存储过程

Page 26: 第 18 课 PL/SQL

Developing Stored Procedures

TextTextfilefile

SourceSourcecodecode

p-codep-code

EditEdit

Store in database

Compile

Execute

1

2

SystemSystemeditoreditor

OracleOracleProcedure Procedure

BuilderBuilder

Page 27: 第 18 课 PL/SQL

• ORACLE 创建存储过程的语法为: • create [or replace] procedure 过程名 • ( 参数 1 [in|out|in out] 数据类型 • [, 参数 2 [in|out|in out] 数据类型 ]... )• {is|as} pl/sql 语句块• 注意:没有 declare, 变量定义在 is 之后• EXAMPLE:• sm_procedure.txt

存储过程

Page 28: 第 18 课 PL/SQL

• ORACLE 创建存储函数的语法为:• create [or replace] FUNCTION name [(parameter [,parameter,…])] RETURN datatype• IS/AS• pl/sql 语句块

• EXAMPLE:• sm_procedure.txt

存储过程

Page 29: 第 18 课 PL/SQL

• 练习:• 建立一个 log_user表,存 username,log

_time,• 建立一个存储过程,记录当前用户,及时间

存储过程

Page 30: 第 18 课 PL/SQL

• 存储过程有以下的优点 : • 存储过程的能力大大增强了 SQL 语言的功能和灵活• 性。存储过程可以用流控制语句编写,有很强的灵• 活性,可以完成复杂的判断和较复杂的运算。• • 可保证数据的安全性和完整性。

存储过程

Page 31: 第 18 课 PL/SQL

• 在运行存储过程前,数据库已对其进行了语法和句法分析,并给出了优化执行方案。这种已经编译好的过程可极大地改善 SQL 语句的性能。• 由于执行 SQL 语句的大部分工作已经完成,所以存储过程能以极快的速度执 行。 • 可以降低网络的通信量。

存储过程

Page 32: 第 18 课 PL/SQL

• PL/SQL 的概念

• BLOCK

• FUNCTION

• PROCEDURE

小结