当前位置:网站首页>PL/SQL入门,非常详细的笔记
PL/SQL入门,非常详细的笔记
2022-07-25 11:00:00 【咻的一下飞过去】
PL/SQL入门,非常详细的笔记
-- PL/SQL简介:
1.PL/SQl是过程语言PL与结构化语言SQL结合而成的编程语言
2.PL/SQL引擎驻留在Oracle服务器中
3.该引擎接收PL/SQL块并对其进行编译
-- PL/SQL 块:
是构成PL/SQL的基本组成单元,将逻辑上相关的声明和语句组合在一起
PL/SQL分为三个部分,声明部分,可执行部分,和异常处理部分
-- PL/SQL 语法:
[declare
声明部分]
[begin
可执行部分
]
[exception
异常处理部分
end
]
/* PL/SQL是一种强类型的编程语言,所有的变量都必须在声明之后才可以使用, 变量都要求在declare部分进行声明,对于变量名称有如下规定: 1.变量名可以是字母数字下划线$#等组成 2.所有的变量名称要求以字母开头,不能是Oracle中的关键字 3.变量的长度最多只能为30个字符 -- 为了提高PL/SQL的可读性,所有变量采用"v_变量名"进行定义 */
declare
v_name varchar2(20) :='我是谁?';
begin
-- 输出操作
dbms_output.put_line('pl/sql的输出操作' || v_name); -- PL/SQL 中字符串使用||连接
end;
-- 使用into为变量赋值
declare
v_name varchar2(20);
begin
-- 通过SQL语句和into关键字为变量赋值
select uname into v_name from user_book where userid=9999;
dbms_output.put_line(v_name); -- 打印
end;
-- %type指定的字段的类型
declare
v_name user_book.uname%type;
v_id user_book.userid%type;
begin
-- 为字段赋值
select user_book.uname,user_book.userid into v_name,v_id
from user_book
where user_book.userid=9999;
-- 输出看结果
dbms_output.put_line(v_id || v_name);
end;
-- %rowtype 代表表中的整行的数据
declare
v_row user_book%rowtype;
begin
-- 赋值
select * into v_row from user_book where user_book.userid=9999;
-- 输出行中的数据: 注意此时必须手动将行内的所有字段写入输出语句,否则会报错
dbms_output.put_line(v_row.uname||v_row.userid||v_row.urole );
end;
-- 条件判断语句
/* 1. if else 语句: if 条件1 then 满足条件1时执行的语句 else 条件2 then 满足条件2时执行的语句 */
/* 2. case when 语句: case when 条件1 then 满足条件1时执行的代码 when 条件2 then 满足条件2时执行的代码 when 条件3 then 满足条件3时执行的代码 else 以上条件都不满足时执行的代码 end case; */
-- 循环
/* -- 1.exit when 退出循环: loop 循环执行的语句块; exit when循环结束条件; 步进语句; end loop; */
declare
v_i int := 0; -- 声明变量
begin
loop
dbms_output.put_line(v_i); -- 循环块: 打印v_i
exit when v_i >= 10; -- 结束条件
v_i := v_i+1; -- 步进语句
end loop;
end;
/* -- 2.while循环: while 循环结束条件 loop 循环体; 步进语句; end loop; */
declare
v_i int := 0; -- 声明变量
begin
while exit when v_i >= 10; -- 结束条件
loop
dbms_output.put_line(v_i); -- 循环块: 打印v_i
v_i := v_i+1; -- 步进语句
end loop;
end;
/* -- 3.for in 循环: for 循环索引变量 in [recerse] -- reverse: 反向 循环区域下限..循环区域上线 loop 循环语句块; end loop ; */
declare
begin
for i in 1..10 -- 从1到10 正序
loop
dbms_output.put_line(i); -- 打印i
end loop;
end;
-- 异常处理
/* 1.定义:在运行异常时出现的错误叫做异常 2.特点: 发生异常后,语句将停止执行,控制权转移到PL/SQL块的异常处理部分 3.异常的三种类型: 预定义异常:当PL/SQL程序违反Oracle规则或超越系统限制时隐式引发; 用户不需要在程序中定义; 非预定义异常:当PL/SQL程序违反Oracle规则或超越系统限制时引发; 用户需要在程序中定义; 用户定义异常:需要用户在程序中定义,显式的在程序中引发; */
-- 异常处理程序的语法:
begin
常规代码块;
exception
when 异常条件1满足 then 异常条件1满足时执行的代码
when 异常条件2满足 then 异常条件2满足时执行的代码
when others then 都不满足时执行的代码
end;
-- 常见的系统异常:
-- 数据过多: to_many_rows
-- 查询的数据不存在: no_data_found
declare
v_error exception;
v_name user_book.uname%type;
begin
select uname into v_name from user_book where user_book.userid=100000; -- 此时的userid是不存在的
end;
-- 用户自定义异常
-- -----------------------------------
declare
v_error exception;
v_name user_book.uname%type;
begin
select uname into v_name from user_book where user_book.userid=100000;
if v_name = '张三' then -- 如果满足条件则返回v_error
raise v_error;
end if;
exception
when v_error then -- 当出现此异常的时候触发
dbms_output.put_line('原来是张三啊,抛个异常给你吧');
end;
边栏推荐
- Functions in JS
- Review in the middle of 2022 | understand the latest progress of pre training model
- 贪心问题01_活动安排问题
- Hardware peripherals =maixpy3
- Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
- 【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
- SQL injection less23 (filter comment)
- JS作用域以及预解析
- Getting started with tensorflow
- SQL language (4)
猜你喜欢

Breadth first traversal (problems related to sequence traversal of graphs and binary trees)

Common web attacks and defense

论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》

SQL language (4)

JaveScript循环

toString()与new String()用法区别

第4章线性方程组

How to judge the performance of static code quality analysis tools? These five factors must be considered

微星主板前面板耳机插孔无声音输出问题【已解决】

Small and micro enterprise smart business card management applet
随机推荐
flinksql client 连接kafka select * from table没有数据报错,如何解决?
Txt to CSV file, blank lines appear every other line
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States
Database integrity -- six constraints learning
Detailed explanation of lvs-nat and lvs-dr modes of LVS load balancing
SQL language (6)
Eigenvalues and eigenvectors of matrices
The first C language program (starting from Hello World)
Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
Getting started with tensorflow
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
擎创科技加入龙蜥社区,共建智能运维平台新生态
JS常用内置对象 数据类型的分类 传参 堆栈
如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题
Web APIs(获取元素 事件基础 操作元素)
W5500 upload temperature and humidity to onenet platform
W5500通过上位机控制实现调节LED灯带的亮度
Fillet big killer, use filter to build fillet and wave effect!
贪心问题01_活动安排问题
Similarity matrix, diagonalization condition