当前位置:网站首页>pl/sql之神奇的嵌套与变量生命周期
pl/sql之神奇的嵌套与变量生命周期
2022-08-02 05:13:00 【赟文武】
pl/sql代码块的嵌套
pl/sql代码块为顺序执行,在嵌套了一个代码块后仍顺序执行,遇到return后,return下的语句不再执行直接结束外层子句
举个栗子吧:
--示例1
declare
a number:=1;
begin
declare
b number:=1;
begin
dbms_output.put_line('b的值:'||b);
end;
dbms_output.put_line('a的值:'||a);
end;
--示例2
declare
a number:=1;
begin
declare
b number:=1;
begin
return;
dbms_output.put_line('b的值:'||b);
end;
dbms_output.put_line('a的值:'||a);
end;

循环的嵌套
- for 循环
for循环套for循环大致执行思路如下:首先外部循环从第一个值开始,进行相关操作进入内部循环,内部循环走完一轮再到外部循环,以此类推外部循环再到第二个值…
--示例1
begin
for i in 1..3 loop
dbms_output.put('Hello ');
for s in 1..3 loop
dbms_output.put('World ');
end loop;
dbms_output.put_line(' ');
end loop;
end;
--示例二(遇到return后内外循环均停止)
begin
for i in 1..3 loop
dbms_output.put('Hello ');
for s in 1..3 loop
dbms_output.put('World ');
dbms_output.put_line(' ');
return;
end loop;
dbms_output.put_line(' ');
end loop;
end;

各个变量生命周期
生命周期用户可以理解为一个变量从产生到灭亡的过程
- declare处声明的变量生命周期
declare处变量在整个代码块结束(end后)释放
declare
a number:=50;
begin
dbms_output.put_line('外层a的值(嵌套子句执行前):'||a);
declare
a number:=10;
begin
dbms_output.put_line('内层a的值:'||a);
end;
dbms_output.put_line('外层a的值(嵌套子句执行后):'||a);
end;

- for…in之间的变量的生命周期
for…in 之间的变量在for循环结束后释放

declare处变量名与for循环中变量名可一致且互不干预
declare
i number;
begin
i:=9;
for i in 1..2 loop
dbms_output.put_line('局部变量(for)中i的值:'||i);
end loop;
dbms_output.put_line('全局变量(declare处)i的值:'||i);
end;

边栏推荐
- APP Bluetooth connection test of test technology
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- 聪明人的游戏提高篇:第三章第二课:“桐桐数”(number)
- kubernetes affinity, anti-affinity, taint, tolerance
- 配合蓝牙打印的encoding-indexes.js文件内容:
- 卸载redis
- 51 MCU Peripherals: Infrared Communication
- 51单片机外设篇:点阵式LCD
- MySql copies data from one table to another table
- 驱动页面性能优化的3个有效策略
猜你喜欢

线程基础(一)

Features and installation of non-relational database MongoDB

OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?

Say good woman programmers do testing have an advantage?More than a dozen interview, abuse of cry ~ ~ by the interviewer

复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
![[PSQL] 窗口函数、GROUPING运算符](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] 窗口函数、GROUPING运算符

A list of 300+ learning resources compiled by senior engineers of the Tao Department (the latest version in 2021)

网安学习-内网渗透4

18 years of programmer career, read more than 200 programming books, pick out some essence to share with you

Introduction to coredns
随机推荐
Timing task library in the language use Cron, rounding
如何进行并发数计算(稳定性测试和压力测试)?
Cyber Security Learning - Intranet Penetration 4
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
在腾讯做外包测试的那些日子.....
APP Bluetooth connection test of test technology
Shuttle + Alluxio 加速内存Shuffle起飞
Difference and analysis of CPU usage and load
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
VMTK环境配置记录
线程基础(一)
Redis-----非关系数据库
51单片机外设篇:ADC
Shell 脚本不同玩法
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
About the directory structure of the web application
nacos注册中心
JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
Introduction to coredns