Oracle——08PL/SQL简介,基本程序结构和语句
发布日期:2025-04-29 23:58:33
浏览次数:11
分类:精选文章
本文共 3530 字,大约阅读时间需要 11 分钟。
PL/SQL变量与常量及程序结构
PL/SQL(Procedural Language/SQL)是一种扩展的SQL语言,能够在Oracle数据库服务器和Oracle开发工具中运行。它通过PL/SQL引擎接收和执行PL/SQL块和子程序,从而将SQL语句提交给Oracle服务器执行。
1. PL/SQL变量
(1)变量的声明
在PL/SQL中,变量通常在PL/SQL块的声明部分定义,每个变量都有特定的数据类型。变量的声明格式如下:
<变量名> <数据类型> [(宽度):= <初始值> ] 初始值> 数据类型> 变量名>
例如,定义名为countNum的number类型变量,初始值为1,可以写为:
countNum number := 1;
(2)变量的属性
PL/SQL提供了以下属性,可以简化变量的定义和管理:
%type属性:用于获取数据库列或变量的数据类型。例如,如果表
A中字段B的数据类型未知,可以定义变量C为B%type,这样C将与B具有相同的数据类型。%rowtype属性:用于获取表中一行记录的数据类型。例如,可以定义游标
cursorName,并将其行记录存储在变量cursorRecord中:declare cursor moduleCursor is select * from t_module; moduleRecord moduleCursor%rowtype;begin open moduleCursor; fetch moduleCursor into moduleRecord; while moduleCursor%found loop dbms_output.put_line(moduleRecord.name || '**********************'); fetch moduleCursor into moduleRecord; end loop; close moduleCursor;end;
(3)自定义数据类型
PL/SQL支持自定义数据类型。例如,可以定义一个PersonRecord数据类型:
declare type PersonRecord is record ( name varchar(20), id number(3) ); person PersonRecord;begin select name, id into person from t_employee where username='admin'; dbms_output.put_line('**********' || person.name);end; 2. PL/SQL常量
常量是在程序运行过程中值不变的量。常量的声明格式如下:
<常量名> constant <数据类型> := <值>值> 数据类型> 常量名>
例如,定义一个整型常量num,值为5,可以写为:
num constant integer := 5;
3. PL/SQL基本程序结构和语句
(1)条件结构
a. if-then结构
语法格式:
if booleanExpression then runExpressionend if;
示例代码:
declare totalCount number;begin select count(*) into totalCount from t_module; if totalCount > 0 then dbms_output.put_line('共有' || totalCount || '条记录'); end if;end; b. if-then-else结构
语法格式:
if booleanExpression then runExpressionelse runExpression2end if;
示例代码:
declare a number := 1; b number := 2; c number := 3;begin if a = 1 then c := c + a; else c := c + b; end if; dbms_output.put_line(c); dbms_output.put_line(c);end;
c. if-then-elsif-then-else结构
语法格式:
if booleanExpression then runExpression1elsif booleanExpression2 then runExpression2else runExpression3end if;
(2)循环结构
在编写循环结构时,必须确保有退出条件满足。
a. loop-exit-end循环
语法格式:
loop runExpression; if booleanExpression then exit; end if;end loop;
示例代码,利用loop-exit-end循环求10的阶乘:
declare n number := 10; results number := 1;begin loop results := results * n; n := n - 1; if n = 0 then exit; end if; end loop; dbms_output.put_line('10的阶乘是:' || results);end; b. loop-exit-when-end循环
语法格式:
loop runExpression; exit when booleanExpression;end loop;
示例代码,利用此结构求10的阶乘:
declare a number := 1; b number := 1;begin loop b := a * b; a := a + 1; exit when a > 10; end loop; dbms_output.put_line('the result is: ' || b);end; c. while-loop-end循环
语法格式:
while booleanExpressionloop runExpression;end loop;
示例代码:
declare a number := 1; b number := 1;begin while a <= 10 loop b := a * b; a := a + 1; end loop; dbms_output.put_line('the result is: ' || b);end; d. for-in-loop-end循环
语法格式:
for countVar in count1..countnloop runExpression;end loop;
示例代码:
declare b number := 1;begin for c in 1..10 loop b := b * c; end loop; dbms_output.put_line('the result is: ' || b);end; (3)选择语句
可以使用case语句,对数值列表做出选择。
语法格式:
case inputNamewhen expression1 then result1when expression2 then result2when expressionn then resultnelse resultend case;
示例代码:
declare num1 number;begin num1 := 6; case num1 when 1 then dbms_output.put_line('the result is no.1'); when 2 then dbms_output.put_line('the result is no.2'); when 3 then dbms_output.put_line('the result is no.3'); else dbms_output.put_line('the result is not in top 3'); end case;end; 发表评论
最新留言
很好
[***.229.124.182]2026年06月09日 23时19分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php筛选js,php如何多条件筛选js代码
2023-03-01
R730服务器做了raid的硬盘,插在R720上面可以用吗?
2023-03-01
PHP类数组式访问(ArrayAccess接口)
2023-03-01
PHP系列:浅谈PHP中isset()和empty() 函数的区别
2023-03-01
PHP索引数组unset的坑-array_values解决方案
2023-03-01
PHP索引数组排序方法整理(冒泡、选择、插入、快速)
2023-03-01
PHP线程安全和非线程安全
2023-03-01
R3LIVE开源项目常见问题解决方案
2023-03-01
php缃戠珯,www.wfzwz.com
2023-03-01
php缓存查询函数
2023-03-01
php编写TCP服务端和客户端程序
2023-03-01
php编码规范
2023-03-01
PHP编码规范-PSR1、psr2 /psr3 psr4
2023-03-01
PHP编程效率的20个要点
2023-03-01
PHP网页缓存技术优点及代码
2023-03-01
PHP自动化测试(一)make test 和 phpt
2023-03-01
php自定义函数: 文件大小转换成智能形式
2023-03-01
php英语单词,php常用英语单词,快速学习php编程英语(6)
2023-03-01
PHP获取curl传输进度
2023-03-01