oracle游标语法举例,PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标)
1、PL/SQL语句块
PL/SQL语句块只适用于Oracle数据库,使用时临时保存在客户端,而不是保存在数据库。
基本语法:
declare
变量声明、初始化
begin
业务处理、逻辑代码
exception
异常捕获
end;
变量声明:
[:=]
例:v_name
varchar2(20):=’张三’;
例:见第3节
2、循环语句
loop循环语法:
loop
exit
when表达式
end
loop;
while循环语法:
while 表达式
loop
end
loop;
for循环语法:
for
in
loop
end
loop;
for循环的变量可不做声明及初始化。
例:见第3节
3、if判断语句
基本语法:
if
then
…
else
if
then
…
else
…
end
if;
end
if;
例:
declare
v_identity number(4):=0;
begin
loop
if
v_identity=1 then
dbms_output.put_line('v_identity=1');
else
if v_identity=3 then
dbms_output.put_line('v_identity=3');
else
if v_identity=6 then
exit;
else
dbms_output.put_line('v_identity
is not 1 or 3');
end
if;
end
if;
end
if; --
注意,有多少个if就要有多少个end
if结束标志。
v_identity:=v_identity+1;
end
loop;
exception
when
others
then dbms_output.put_line('error!');
end;
/
4、分支case
基本语法:
case
when常量
then
…
when常量
then
…
else
…
end
case;
例:
declare
v_number number(4):=3;
v_string varchar(20):='abc';
begin
case
v_number
when
1 then
dbms_output.put_line('v_number
is '||1);
when
2 then
dbms_output.put_line('v_number
is '||2);
when
3 then
dbms_output.put_line('v_number
is '||3);
end
case;
case
v_string
when
'ab' then
dbms_output.put_line('v_string
is '||'ab');
when
'bc' then
dbms_output.put_line('v_string
is '||'bc');
else
--
缺省匹配
dbms_output.put_line('v_string
is other value');
end
case;
exception
when
others
then dbms_output.put_line('error!');
end;
/
5、异常(exception)
声明异常语法:
exception;
抛出异常语法:raise
;
捕获异常语法:when
then异常处理语句;
例:
declare
v_input varchar2(1):='&throw';--
动态输入
v_exception_1 exception; --
自定义异常
v_exception_2 exception;
others exception; --
系统异常
begin
if
v_input='1' then
raise
v_exception_1;
--
抛出异常
else
if v_input='2' then
raise
v_exception_2;
else
raise
others;
end
if;
end
if;
exception
--
捕获异常
when
v_exception_1
then dbms_output.put_line('throw
exception: v_exception_1');
when
v_exception_2
then dbms_output.put_line('throw
exception: v_exception_2');
when
others
then dbms_output.put_line('throw
exception: others');
end;
/
6、游标(cursor)
声明游标语法:cursor
is
select语句;
声明ref游标语法:
is ref cursor;
打开游标语法:open
;
移动游标并获取数据语法:fetch
into
;
关闭游标语法:close
;
游标属性(游标的属性必须在关闭游标之前):
%isopen:
判断游标是否打开
%notfound:
找不到数据时
%found:
%rowcount:
返回当前游标已扫描的数据行数量
游标分类:1、显示游标(自定义游标);2、隐示游标(系统游标);3、REF游标
例:
declare
v_row
t_test%rowtype; --
匹配t_test表中一行所有的数据类型
cursor v_cur is select * from t_test;--
声明游标
begin
open
v_cur;--
打开游标
loop
fetch
v_cur
into v_row;--
将游标所在行的数据转存到v_row中
exit
when v_cur%notfound; --
当游标到最后一行时跳出
dbms_output.put_line('id
= '||v_row.t_id||' name = '||v_row.t_name||' msg = '||v_row.t_msg);
end
loop;
close
v_cur;--
关闭游标
exception
when
others
then dbms_output.put_line('throw
exception: others');
end;
/
--
REF游标 --
create or replace package upk_select_test
as
type uc_test is ref cursor; --
声明ref游标
end
upk_select_test;
/
--
存储过程中调用ref游标,并将查询结果以游标的方式返回
create or replace procedure up_select_test_2
(uc_result out upk_select_test.uc_test)
is
begin
open
uc_result
for select * from t_test;
end
up_select_test_2;
/
7、通配类型操作符
%type:
通配某行某列数据类型,如v_name
t_test.t_name%type;通配表t_test中的t_name。
%rowtype:
通配一行所有列的数据类型,如 v_row
t_test%rowtype;匹配t_test表中一行
所有的数据类型。
8、存储过程(procedure)
基本语法:
create procedure
()
as|is
变量声明、初始化
begin
业务处理、逻辑代码
exception
异常捕获、容错处理
end
;
参数:
in|out|in out
,如:v_name
varchar2
in:入参
out:出参
in
out:出入参
注:as|is表示as或is
调用语法:
1)、exec
;
2)、execute
;
3)、在PL/SQL语句块中直接调用。
例:
create or replace procedure up_wap(v_param1 in out varchar2,v_param2 in out varchar2)
is
v_temp varchar2(20);
begin
dbms_output.put_line('交换前参数1:'||v_param1||'参数2:'||v_param2);
v_temp:=v_param1;
v_param1:=v_param2;
v_param2:=v_temp;
dbms_output.put_line('交换后参数1:'||v_param1||'参数2:'||v_param2);
exception
when
others
then dbms_output.put_line('There
is a error when the procedure up_wap executing!');
end
up_wap;
/
--
调用存储过程
declare
v_param1 varchar2(20):='param1';
v_param2 varchar2(20):='param2';
begin
up_wap(v_param1 =>
v_param1,v_param2 => v_param2);
end;
/
9、自定义函数(function)
基本语法:
create function
()
return
as|is
变量声明、初始化
begin
业务处理、逻辑代码
return
;
exception
异常捕获、容错处理
end
;
参数:in入参
注:只有入参的类型。
在存储过程和自定义函数中的参数的传递(入参和出参)不能使用%type或%rowtype匹配,不能使用空值null,但是存储过程可以返回空值。
例:
create function uf_select_name_by_id_test(v_id
in number)
return varchar2
is
v_name
t_test.t_name%type;
begin
select t_name into v_name from t_test where t_id=v_id;
return v_name;
exception
when
others
then dbms_output.put_line('error');
end
uf_select_name_by_id_test;
/
select uf_select_name_by_id_test(1)
姓名 from dual;--
select调用
declare --pl/sql语句块调用
v_name varchar2(20);
begin
v_name:=uf_select_name_by_id_test(1);
dbms_output.put_line('name
= '||v_name);
end;
/
10、包(package)
封装,可以封装过程(procedure)、函数(function)和变量。
注意,在包(package)中声明的过程(procedure)和函数(function)必须在包的实现体
(package
body)中定义实现。
基本语法:
create
package
as|is
变量声明
存储过程声明
自定义函数声明
end
;
/
create
package
as|is
存储过程的代码实现
自定义函数的代码实现
end
;
/
例:
--
创建包upk_hello
create or replace package upk_hello
is
v_hello_world varchar2(20):='hello world'; --
声明变量
procedure up_hello_world(v_name
in varchar2);--
声明过程
function uf_hello_world(v_name
in varchar2) return varchar2;--
声明函数
end
upk_hello;
/
--
实现包(upk_hello)里声明的方法
create or replace package body upk_hello
is
procedure up_hello_world(v_name
in varchar2)
is
v_string varchar2(100);
begin
v_string:=v_name||'
say hello world!';
dbms_output.put_line(v_string);
exception
when
others
then dbms_output.put_line('error');
end
up_hello_world;
function uf_hello_world(v_name
in varchar2) return varchar2
is
v_string varchar2(100);
begin
v_string:=v_name||'
say hello world!';
return v_string;
exception
when
others
then dbms_output.put_line('error');
end
uf_hello_world;
end
upk_hello;
/
--
包的调用
declare
v_msg
varchar2(100);
begin
upk_hello.up_hello_world('bing');
v_msg:=upk_hello.uf_hello_world('admin');
dbms_output.put_line(v_msg);
dbms_output.put_line(upk_hello.v_hello_world);
end;
/
oracle游标语法举例,PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标)相关推荐
- oracle语句优化pl sql语句,求oracle插入初始数据pl/sql语句优化,该怎么处理(2)
SQL codeCREATE OR REPLACE PROCEDURE thi_pro AS randomnum NUMBER; randomnum2 NUMBER; randomnum3 NUMBE ...
- oracle执行计划的概念,SQL语句性能调整之ORACLE的执行计划
对于CBO优化器: CBO根据统计信息选择驱动表,假如没有统计信息,则在from 子句中从左到右的顺序选择驱动表.这与RBO选择的顺序正好相反.这是英文原文(CBO determines join o ...
- oracle数据库匿名快,pl/sql分匿名块和命名块
命名块:存储过程,函数,触发器,包等 pl/sql语句块分3部分: (1)声明部分 (2)可执行部分 (3)异常处理部分 其中可执行部分是语句块中唯一要求必须存在的部分,声明部分和异常处理部分是可选的 ...
- 结束下面sql块_oracle: PL/SQL基本结构,语法,变量
PL/SQL是基于块结构,不论是命名块还是匿名块都由3个部分组成 定义部分:定义常量,变量,游标,基础及复杂数据类型 执行部分:包含要执行的PL/SQL语句,sql语句,实现应用模块功能 异常处理部分 ...
- Oracle学习笔记_PL/SQL语句块
推荐一个学习视频:https://www.bilibili.com/video/BV1AX4y1T7KG?spm_id_from=333.1007.top_right_bar_window_custo ...
- Oracle数据库学习:PL/SQL(详解)
Oracle数据库学习:PL/SQL 什么是PL/SQL PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的扩展语言; 使用PL/SQL 可以编写具 ...
- oracle sq语句查询时间,Oracle实现查询时间段的Sql语句两法
Oracle实现查询时间段的Sql语句两法,相比ORacle要查询时间段的Sql语句还是与Sql Server的Sql语句有区别的,下面举两种方法来说明在ORacle是如何查询时间段的: 第一种方法: ...
- Oracle PL/SQL语句基础学习笔记(上)
PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL ...
- Oracle PL/SQL语句基础学习笔记(下)
游标 游标: 游标(cursor)可以被看作指向结果集(a set of rows)中一行的指针(pointer).在oracle数据库中可以使用显示或隐式两种游标. 1.隐式游标 在执行一个sql语 ...
最新文章
- 真相!30K拿到互联网大厂offer,网友:我服了!
- ES6中的异步对象Promise
- Spring5 - 核心原理
- 如何利用jquery实现一句话全选/取消全选的实例代码
- springboot超详细教程_CG原画插画教程:超详细线稿教程
- NLP《词汇表示方法(七)BERT》
- 【OJ8462】大盗阿福
- JS事件、对象基础篇
- 爬虫python能做什么-python爬虫能干什么
- python通过MySQLdb操作mysql
- samba、nginx服务
- 男人婚后为何会对婚姻心生倦意?
- 单片机引脚模式的配置
- Java环境安装(Linux版)
- C语言 | 求圆周长 面积 圆球表面积 体积
- wangeditor 粘贴word内容带样式解决方法
- 【天空盒】资源预览下载
- 牙膏不只可刷牙——32不寻常用途!!
- Cmaker 是什么
- InSAR学习(五)高级的InSAR技术:PS技术和SBAS技术
热门文章
- 用Visual Studio 2019 开发stm32,cortex-m3, arm
- 支持php,让Apache支持PHP语言
- python的super用法_关于Python的super用法研究
- 晶振,数字电路的心脏~
- 电子徽章:融创意、疯狂与电子设计中
- 手环是如何测试人体健康数据?
- go mysql recover_golang recover后怎么返回
- linux禁止修 5在线阅读,linux – 如何在不重新编译内核的情况下禁用CentOS 5.3中的nf_conntrack内核模块...
- java三目运算符嵌套_替代JS中的嵌套三元运算符
- linux如何切换到光盘,怎么刻录cd光盘-Linux切换目录之cd命令详解