oracle PL/SL编程基础
.了解PL/SQL数据类型
.掌握PL/SQL控制结构
.了解PL/SQL中异常处理
.掌握游标的使用
Pl/SQL简介
提高应用程序的运行性能,
提供模块化的程序设计,
自定义标示符,
具有过程语言控制结构,
良好的兼容性,
处理运行错误.
Pl/SQL语言基础
sql是关系数据库的基本操作语言.
sql语言包括:数据查询,数据操纵,事物控制,数据定义和数据控制语言登5个部分.
Pl/SQL块
Pl/SQ的基本单元
1
2
3
4
5
6
7
|
DECLARE
/*定义部分-变量,常量,游标,列解*/
BEGIN
/*定义部分-PL/SQL语句*/
EXCEPTION
/*异常处理部分-处理运行错误*/
END; /*结束块标志*/
|
注意:"="表示判断,":="赋值,"||"连接.
标量类型
VARCHAR2(n),CHAR(n),NUMBER(p,s),DATE,TIMESTAMP,BOOLEAN
if语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-- 结构: if 条件 then = 表示判断
-- end if ;
select * from scott.emp;
declare
v_name varchar(30);
begin
select scott.emp.ename into v_name from scott.emp where scott.emp.empno = 7499;
if v_name = 'SMITH' then
begin
dbms_output.put_line( '没有该员工' );
end;
elsif xxx then
begin
end;
else
dbms_output.put_line( '员工姓名' || v_name);
end if ;
end;
|
CASE语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-- case
switch (xxx) {
case '' :
break ;
default :
}
case xxx
when xxx then
-- 处理语句
when xxx then
--
else
end case ;
select ename,
case deptno
when 10 then
'综合部门'
when 20 then '技术部门'
when 30 then '市场部门'
else '其他部门'
end case from scott.emp;
|
循环语句(loop,while,for)
条件:循环初始值,循环结束之,改变值.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
-- 循环 loop while for 循环起始值 循环结束条件 改变值
declare
i int := 0;
begin
loop
i := i + 1;
dbms_output.put_line(i);
exit when i = 10;
end loop;
end;
declare
i int := 0;
begin
while i < 101 loop
i := i + 1;
dbms_output.put_line(i);
end loop;
end;
declare
begin
for i in reverse 1..101 loop
dbms_output.put_line(i);
end loop;
end;
select emp.ename from emp;
-- 类型 存储更多的数据 %type %rowtype record(自定义类型) table varray
declare
v_name emp.ename%type;
begin
select emp.ename into v_name from emp where empno = 7369;
dbms_output.put_line(v_name);
end;
declare
v_name dept%rowtype;
begin
select * into v_name from dept where deptno = 10;
dbms_output.put_line(v_name.deptno || v_name.dname);
end;
declare
type r_name is record (
v_dname dept.dname%type,
v_loc dept.loc%type
);
rec_v r_name;
begin
select dname,loc into rec_v.v_dname,rec_v.v_loc from dept where deptno = 10;
dbms_output.put_line(rec_v.v_dname || rec_v.v_loc);
end;
select * from dept
|
异常处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
-- 异常处理
exception
when 异常 then
--
when 异常 then
when others then
-- 预定义异常
declare
v_name emp.ename%type;
begin
select to_number(ename) into v_name from emp where empno = &empno;
exception
when no_data_found then -- ora-01403
dbms_output.put_line( '没有数据' );
when invalid_number then -- ora-01403
dbms_output.put_line( '转换异常' );
end;
-- 非预定义异常 + 自定义异常
declare
myexception exception;
v_name emp.ename%type;
pragma exception_init(myexception,-30000);
begin
select ename into v_name from emp where empno = &empno;
if v_name = 'SMITH' then
raise myexception;
end if ;
exception
when myexception then -- ora-30000
dbms_output.put_line( 'SMITH不能更改' );
end;
select * from emp;
|
游标
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
-- 游标 如果你在pl/sql 操作多行数据 必须游标
-- 1定义游标 2.打开游标 3 提取数据 4关闭游标
declare
cursor cus_emp is select * from emp;
v_rowemp emp%rowtype;
v_count int ;
begin
select count(*) into v_count from emp;
open cus_emp;
for i in 1..v_count
loop
fetch cus_emp into v_rowemp;
dbms_output.put_line(v_rowemp.empno || ' ' || v_rowemp.ename);
end loop;
end;
declare
cursor cus_emp is select empno,ename from emp;
type record_type is record (
v_empno emp.empno%type,
v_ename emp.ename%type
);
res record_type;
v_count int ;
begin
select count(*) into v_count from emp;
open cus_emp;
for i in 1..v_count
loop
fetch cus_emp into res;
dbms_output.put_line(res.v_empno || ' ' || res.v_ename);
end loop;
close cus_emp;
end;
declare
cursor cus_emp(v_no emp.empno%type) is select empno,ename from emp where empno = v_no;
type record_type is record (
v_empno emp.empno%type,
v_ename emp.ename%type
);
res record_type;
v_count int ;
i int := 0;
begin
open cus_emp(7369);
loop
fetch cus_emp into res;
exit when cus_emp%notfound;
dbms_output.put_line(res.v_empno || ' loop ' || res.v_ename);
end loop;
close cus_emp;
end;
select count(*) into v_count from emp;
open cus_emp(7369);
while i<v_count
loop
fetch cus_emp into res;
dbms_output.put_line(res.v_empno || ' while ' || res.v_ename);
i := i + 1;
end loop;
close cus_emp;
|
Oracle Pl/SQL编程基础
Pl/SQL简介
提高应用程序的运行性能,
提供模块化的程序设计,
自定义标示符,
具有过程语言控制结构,
良好的兼容性,
处理运行错误.
Pl/SQL语言基础
sql是关系数据库的基本操作语言.
sql语言包括:数据查询,数据操纵,事物控制,数据定义和数据控制语言登5个部分.
Pl/SQL块
Pl/SQ的基本单元
1
2
3
4
5
6
7
|
DECLARE
/*定义部分-变量,常量,游标,列解*/
BEGIN
/*定义部分-PL/SQL语句*/
EXCEPTION
/*异常处理部分-处理运行错误*/
END; /*结束块标志*/
|
注意:"="表示判断,":="赋值,"||"连接.
标量类型
VARCHAR2(n),CHAR(n),NUMBER(p,s),DATE,TIMESTAMP,BOOLEAN
if语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-- 结构: if 条件 then = 表示判断
-- end if ;
select * from scott.emp;
declare
v_name varchar(30);
begin
select scott.emp.ename into v_name from scott.emp where scott.emp.empno = 7499;
if v_name = 'SMITH' then
begin
dbms_output.put_line( '没有该员工' );
end;
elsif xxx then
begin
end;
else
dbms_output.put_line( '员工姓名' || v_name);
end if ;
end;
|
CASE语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-- case
switch (xxx) {
case '' :
break ;
default :
}
case xxx
when xxx then
-- 处理语句
when xxx then
--
else
end case ;
select ename,
case deptno
when 10 then
'综合部门'
when 20 then '技术部门'
when 30 then '市场部门'
else '其他部门'
end case from scott.emp;
|
循环语句(loop,while,for)
条件:循环初始值,循环结束之,改变值.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
-- 循环 loop while for 循环起始值 循环结束条件 改变值
declare
i int := 0;
begin
loop
i := i + 1;
dbms_output.put_line(i);
exit when i = 10;
end loop;
end;
declare
i int := 0;
begin
while i < 101 loop
i := i + 1;
dbms_output.put_line(i);
end loop;
end;
declare
begin
for i in reverse 1..101 loop
dbms_output.put_line(i);
end loop;
end;
select emp.ename from emp;
-- 类型 存储更多的数据 %type %rowtype record(自定义类型) table varray
declare
v_name emp.ename%type;
begin
select emp.ename into v_name from emp where empno = 7369;
dbms_output.put_line(v_name);
end;
declare
v_name dept%rowtype;
begin
select * into v_name from dept where deptno = 10;
dbms_output.put_line(v_name.deptno || v_name.dname);
end;
declare
type r_name is record (
v_dname dept.dname%type,
v_loc dept.loc%type
);
rec_v r_name;
begin
select dname,loc into rec_v.v_dname,rec_v.v_loc from dept where deptno = 10;
dbms_output.put_line(rec_v.v_dname || rec_v.v_loc);
end;
select * from dept
|
异常处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
-- 异常处理
exception
when 异常 then
--
when 异常 then
when others then
-- 预定义异常
declare
v_name emp.ename%type;
begin
select to_number(ename) into v_name from emp where empno = &empno;
exception
when no_data_found then -- ora-01403
dbms_output.put_line( '没有数据' );
when invalid_number then -- ora-01403
dbms_output.put_line( '转换异常' );
end;
-- 非预定义异常 + 自定义异常
declare
myexception exception;
v_name emp.ename%type;
pragma exception_init(myexception,-30000);
begin
select ename into v_name from emp where empno = &empno;
if v_name = 'SMITH' then
raise myexception;
end if ;
exception
when myexception then -- ora-30000
dbms_output.put_line( 'SMITH不能更改' );
end;
select * from emp;
|
游标
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
-- 游标 如果你在pl/sql 操作多行数据 必须游标
-- 1定义游标 2.打开游标 3 提取数据 4关闭游标
declare
cursor cus_emp is select * from emp;
v_rowemp emp%rowtype;
v_count int ;
begin
select count(*) into v_count from emp;
open cus_emp;
for i in 1..v_count
loop
fetch cus_emp into v_rowemp;
dbms_output.put_line(v_rowemp.empno || ' ' || v_rowemp.ename);
end loop;
end;
declare
cursor cus_emp is select empno,ename from emp;
type record_type is record (
v_empno emp.empno%type,
v_ename emp.ename%type
);
res record_type;
v_count int ;
begin
select count(*) into v_count from emp;
open cus_emp;
for i in 1..v_count
loop
fetch cus_emp into res;
dbms_output.put_line(res.v_empno || ' ' || res.v_ename);
end loop;
close cus_emp;
end;
declare
cursor cus_emp(v_no emp.empno%type) is select empno,ename from emp where empno = v_no;
type record_type is record (
v_empno emp.empno%type,
v_ename emp.ename%type
);
res record_type;
v_count int ;
i int := 0;
begin
open cus_emp(7369);
loop
fetch cus_emp into res;
exit when cus_emp%notfound;
dbms_output.put_line(res.v_empno || ' loop ' || res.v_ename);
end loop;
close cus_emp;
end;
select count(*) into v_count from emp;
open cus_emp(7369);
while i<v_count
loop
fetch cus_emp into res;
dbms_output.put_line(res.v_empno || ' while ' || res.v_ename);
i := i + 1;
end loop;
close cus_emp;
|
转载于:https://www.cnblogs.com/pengjun110/p/3505220.html
oracle PL/SL编程基础相关推荐
- oracle PL/SQL编程基础
目录 1 PL/SQL介绍 1.1 PL/SQL基础结构 1.2 PL/SQL 注释 1.3 PL/SQL常量和变量 1.4条件语句 1.5 循环语句 过完节由于疫情公司没有进行上班,在收拾房屋的过程 ...
- 原创:oracle PL/SQL编程基础 下lt;十二gt;
游标: 游标(cursor)可以被看作指向结果集(a set of rows)中一行的指针(pointer).在oracle数据库中可以使用显示或隐式两种游标. 隐式游标: 在执行一个 sql 语句 ...
- 二、PL/SQL 编程基础
Oracle PL/SQL 编程基础 文章目录 一.PL/SQL编程基础 1.1 字符类型 1.2 词汇单元 1.3 PL/SQL 变量 1.4 使用基础数据类型 1.5 最常见的数据类型 1.6 声 ...
- 10、oracle下PL/SQL编程基础
ORACLE下的PL/SQL编程基础 PL/SQL语言是程序化程序设计语言,块是PL/SQL编程中的基本结构,其优点在于支持SQL.支持面向对象编程.性能好.可移植性.与sql集成.安全性高等. 1. ...
- 视频教程-赵强老师:Oracle数据库从10g到11g(4)PL/SQL编程基础-Oracle
赵强老师:Oracle数据库从10g到11g(4)PL/SQL编程基础 毕业于清华大学,拥有超过13年的工作经验. Oracle认证讲师,拥有6年以上授课经验.精通Oracle数据库.中间(Weblo ...
- Oracle 实验六:PL/SQL编程基础(1)
实验六:PL/SQL编程基础(1) 一.实验目的 1.熟悉掌握PL/SQL编程中的变量定义语句 2.熟悉掌握PL/SQL编程中的条件语句和循环语句等流程控制语句. 3.能熟练使用上述基本语句编写PL/ ...
- [顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)...
[顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功) 原文:[顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之 ...
- ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)
[顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日之功) 继上四篇:ORACLE PL/SQL编程之八:把触发器说透 ORAC ...
- [顶]ORACLE PL/SQL编程详解之二:PL/SQL块结构和组成元素(为山九仞,岂一日之功)
[顶]ORACLE PL/SQL编程详解之二: PL/SQL块结构和组成元素(为山九仞,岂一日之功) 继上四篇:ORACLE PL/SQL编程之八:把触发器说透 ORAC ...
最新文章
- windows快捷键十八式(win10)
- 【C++】字符串中运算符的重载问题
- Spring Schedule关闭订单
- python 追加到字典_扫描器篇(三)之python编写基于字典的网站目录探测脚本
- css-6 df15,webpack 样式文件的代码分割(15)
- c语言 在txt文件中搜索关键词_网络推广外包—网络推广外包如何在搜索引擎中体现关键词优化效果...
- 2299 Ultra-QuickSort(归并)
- 英语人机考试计算机算分吗,英语人机对话考试技巧
- redis的密码验证,及哨兵的相关配置
- div无法触发blur事件解决的方法
- Elasticsearch7.15.2 mysql8.0.26 logstash-input-jdbc 数据增量索引构建
- Oracle自增主键的添加[sequence]--表数据已存在
- OC_内存管理(二)对象复制、循环引用问题、自动释放池
- 最新cs1.6服务器ip地址,CS1.6服务器IP地址
- 让“王码五笔输入法”成为你的专用输入法!
- matlab离散信号z变换,离散信号与系统的Z变换分析
- 上海巨人网络面试经历
- iphone3GS iOS6.0.1不完美越狱详细图文教程
- Delphi 监听安卓(Android)手机通知栏信息
- Arduino库 <TFT_eSPI> 中文字库的制作与使用
热门文章
- python pep8_Python 代码风格 和 PEP8
- WebSocket 实现原理
- 查看php文件的效果,HTML5的交互式动画效果文件夹预览查看特效
- java 反射 数组,Java 数组
- SSL/TLS 存在Bar Mitzvah Attack漏洞
- Mysql 求时间 between 昨天 and 上个月的今天 等时间函数
- Java 面向对象 之 静态内部类
- Android之eclipse简单NDK入门
- [Android ] linux命令英文缩写的含义(方便记忆)
- virtualenv 安装不同版本的虚拟环境的办法