Oracle PL/SQL 高级编程
1、 复合数据类型——记录类型
Ø 语法格式
type 类型名 is record (
字段1 字段1类型 [not null]:=表达式1;
字段2 字段2类型 [not null] :=表达式2;
… )
Ø 说明:记录之间相互赋值要求两个记录类型全然同样。
Ø 案例
举例1
--创建表并插入记录
create table student(idnumber, name varchar2(20), birthday date);
insert into studentvalues(100, 'xiaoming', to_date('2000.12.17', 'yyyy.mm.dd'));
insert into studentvalues(200, 'xiaohua', to_date('2001.12.17', 'yyyy.mm.dd'));
insert into studentvalues(300, 'xiaoli', to_date('2002.12.17', 'yyyy.mm.dd'));
--定义记录类型
Declare
type t_studentRecord isrecord(
id number,
name varchar2(20),
birthday date);
或者:
type t_StudentRecord isrecord(
idstudent.id%type,
name student.name%type,
birthday student birthday%type);
v_students t_StudentRecord;
begin
select * into v_students from students whereid=200;
dbms_output.put_line(v_students.id||' '||v_students.name||' '||to_char(v_students.birthday,'yyyy-mm-dd'));
end;
举例2
declare
type t_StudentRecord is record(
id number(4),
name varchar2(20),
birthday date);
v_students t_StudentRecord;
v_students_copy t_StudentRecord;
begin
v_students.id:=400;
v_students.name:='赵伟';
v_students.birthday:= to_date('2003.12.17','yyyy.mm.dd');
v_students_copy:=v_students;
dbms_output.put_line(v_students_copy.id||''||v_students_copy.name||' '|| to_char(v_students_copy. birthday, 'yyyy-mm-dd'));
end;
举例3
declare
type t_StudentRecord is record(
id number(4),
name varchar2(20),
birthday date);
type t_StudentRecord2 is record(
id number(4),
namevarchar2(20),
birthday date);
v_students t_studentRecord;
v_students_copy t_studentRecord2;
begin
v_students.id:=400;
v_students.name:='赵伟';
v_students.birthday:= to_date('2003.12.17','yyyy.mm.dd');;
v_students_copy:=v_students;
dbms_output.put_line(v_students_copy.id||''||v_students_copy.name||' '|| to_char(v_students_copy. birthday, 'yyyy-mm-dd'));
end;
出错说明:假设两个记录类型类型名不同,可是内容全然同样,两个类型相应的两个变量不能互相赋值。
2、 集合数据类型——index-by表
Ø 介绍:类似于普通程序设计语言中的数组概念。
Ø 声明index-by表的方法:
Type 类型名 IS TABLE OF typeINDEX BY BINARY_INTEGER;
说明:当中type定义的是index-by表中各元素的类型,能够是内置类型、用户定义的对象类型或者使用%rowtype的表达式等。
Ø index-by表中单个元素
在声明了类型和变量后,能够通过:变量名(index)使用表中的单个元素。当中index是指表中的第几个元素。
Ø Index by表的属性函数
|
属性名称 |
数据类型 |
说 明 |
|
count |
number |
返回表中的行数 |
|
delete |
无 |
用于从表中删除指定(由传入參数指定)的一行数据 |
|
exists |
boolean |
假设指定的行存在则返回true。否则返回false |
|
first |
binary_integer |
返回表中第一行的下标 |
|
last |
binary_integer |
返回表中最后一行的下标 |
|
next |
binary_integer |
返回指定行(由传入參数指定)的下一行的下标 |
|
prior |
binary_integer |
在指定行(由传入參数指定)的上一行的下标 |
Ø 案例
举例1
declare
type t_StudentRecord isrecord(
idstudent.id%type,
name student.name%type,
birthdaystudent birthday%type);
type t_studentTable is table oft_StudentRecord index by binary_integer;
v_students t_studentTable;
begin:
select * into v_students(100) from student whereid=100;
dbms_output.put_line(v_students(100).id||''||v_students(100).name||' '|| to_char(v_students(100). birthday, 'yyyy-mm-dd'));
end;
举例2
declare
type t_studentTable is table of student%rowtypeindex by binary_integer;
v_students t_studentTable;
begin
select * into v_students(1) from student whereid=200;
dbms_output.put_line(v_students(1).id ||''||v_students(1).name||'
'|| to_char(v_students(1). birthday,'yyyy-mm-dd'));
end;
举例3
Declare
Type t_s is table of scott.emp%rowtype Indexby binary_integer;
V_s t_s;
V_index binary_integer;--索引號
V_loop binary_integer;--循环次数
Begin
Select * into v_s(10) from scott.emp whereempno='7788';
Select * into v_s(22) fromscott.emp where empno='7902';
Select * into v_s(-12) fromscott.emp where empno='7934';
V_index:= v_s.first;
V_loop:=v_s.count;
Loop
Dbms_output.put_line(v_s(v_index).empno);
V_index:=v_s.next(v_index);
V_loop:=v_loop-1;
Exit when v_loop<=0;
End loop;
V_index:=v_s.last;
v_s.delete(v_index);
V_index:= v_s.first;
V_loop:=v_s.count;
Loop
Dbms_output.put_line(v_s(v_index).empno);
V_index:=v_s.next(v_index);
V_loop:=v_loop-1;
Exit when v_loop<=0;
End loop;
End;
3、 集合数据类型——变长数组
Ø 介绍:变长数组:是一个存储有序元素的集合,每一个元素都有一个索引,该索引相相应元素在数组中的位置。变长数组存在大小的限制,可是能够动态进行更改。
Ø 创建变长数组语句:
Ø 案例
--创建一个变长数组
Create Type varray_phone as varray(3) of varchar2(50);
--创建一个人员表。表中人员拥有一列电话(可能有1、2或3个电话号码)。
create table person3
(
id integer constraintperson3_pk primary key,
first_name varchar(20),
last_name varchar(20),
phone varray_phone
)
--填充变长数组
insert into person3values(1,'yuan','weixiang',varray_phone('12345','34567','56789'));
select * from person3;
insert intoperson3 values(2,'hao','lihai',varray_phone());
select * from person3;
--改动变长数组中的元素
update person3 setphone= varray_phone('12345','34567') where id = 2;
select * from person3;
--改动变长数组的元素的长度大小
Alter type varray_phone modifyelement type varchar2(49) cascade --报错
Alter type varray_phone modify element type varchar2(60)cascade
说明:cascade选项把更改传播到数据库中的依赖对象 Person3就是依赖对象
--改动变长数组元素的数目
alter type varray_phonemodify limit 2 cascade -- 报错,varray限制仅仅能增大
alter type varray_phonemodify limit 5 cascade
insert into person3 values(3,'yuan','weixiang',varray_phone('12345','34567','56789','34567','56789'));
4、 游标
Ø 介绍:在PL/SQL程序设计中,有时须要对查询返回结果集进行逐行处理。这就须要将该查询返回结果集缓存到一个内存区中。为了能对返回的结果集进行逐行操作,需返回该内存区的首地址。这个地址被称为游标。
Ø 定义游标语法
Cursor 游标名 is select语句;
注意:在游标定义中的select语句不能包括into子句。
Ø 游标属性
|
游标属性 |
描 述 |
|
游标名%isopen |
布尔值,假设游标已打开,取值为true。否则为false。 |
|
游标名%notfound |
布尔值。假设近期一次fetch操作没有返回结果,则取值为true,否则为false。 |
|
游标名%found |
布尔值,假设近期一次fetch操作没有返回结果,则取值为false,否则为true。 |
|
游标名%rowcount |
数字型值,值为到当前为止返回的行数。 |
Ø 案例
举例1
DECLARE
v_no scott.emp.empno%type。
v_name scott.emp.ename%type;
CURSORc_e IS SELECT empno, ename FROM scott.emp;
BEGIN
OPEN c_e;
LOOP
FETCH c_e INTO v_no, v_name;
EXIT WHEN c_e%NOTFOUND;
Dbms_output.put_lint(v_no||' '||v_name);
END LOOP;
CLOSE c_e;
END;
举例2
DECLARE
CURSOR c_e IS SELECT empno, ename FROM scott.emp;
BEGIN
For c1 in c_e loop
Dbms_output.put_lint(c1.empno||' '||c1.ename);
END LOOP;
END;
5、 可更新的游标
Ø 介绍:从游标中抽取数据,能够对数据库中的数据进行update和delete操作。
Ø 语法:在定义游标时。必须加for update of子句;在update和delete语句中加上where current of子句。
Ø 案例
举例1:对scott方案emp表中某部门的各员工。假设其工资小于1600元,则将其工资设为1600元。
Accept p_deptno prompt‘please enter the deptno’;
--Accept类似与c语言中的scanf,意为从屏幕接受输入到p_deptno变量中。
declare
v_deptno scott.emp.deptno%type:=&p_deptno;
cursor emp_cursor is select empno,job,sal from scott.emp wheredeptno=v_deptno for update of sal;
begin
for emp_record in emp_cursor loop
if emp_record.sal<1600 then
update emp set sal=1600 where currentof emp_cursor;
end if;
end loop;
end;
举例2:为职工涨工资,对员工按工资从低到高排序。从工资低的员工開始涨,每位员工涨10%。但要控制员工总工资在50万之内,一旦总额超过50万。就停止对剩余的员工涨工资。
6、 带參数的游标
Ø 介绍:定义显示游标时,能够增加參数的定义。在使用游标时,对于參数输入不同的数值,则游标缓存中的数据也随之变化。
Ø 定义游标语法:
cursor 游标名(參数1 数据类型, ….) is select 子句;
Ø 打开游标语法:
open游标名(&參数1, ….) ;
Ø 案例:从scott方案的emp表中查询并打印某个部门的雇员情况,当中部门号由用户交互式输入。
Accept v_deptnoprompt‘please enter the deptno;
declare
v_ename scott.emp.ename%type;
v_sal scott.emp.sal%type;
cursor emp_cursor (v_deptno number) is selectename,sal from scott.emp where deptno=v_deptno;
begin
open emp_cursor(&p_deptno);
loop
fetch emp_cursor into v_ename,v_sal;
exit when emp_cursor%notfound;
dbms_output.put_line(v_ename|| ' '||v_sal);
end loop;
close emp_cursor;
end;
Oracle PL/SQL 高级编程的更多相关文章
- oracle PL/SQL基础编程
PL/SQL(Procedural Language/SQL)是oracle中引入的一种过程化编程语言 PLS-00103:出现符号"declare"在需要下列之一时 符号&quo ...
- oracle PL/SQL高级特性
触发器:存放在数据库中,并被隐含执行的存储过程. 由触发事件,触发条件,触发操作组成. DML触发器:指定触发器时机(before or after),触发事件(insert , delete, u ...
- Oracle PL/SQL高级应用 存储过程
有名字的Plsql块,成为Oracle的对象,在以后用到时可以直接调用. CREATE OR REPLACE PROCEDURE myproc(id IN varchar2) IS -IN 为输入参数 ...
- Oracle PL/SQL高级应用 视图 同义词 序列
视图: 视图叫虚表,即是在哪个表上建立的视图,将那个表的数据用一条查询sql语句查出的数据展现在该视图中,对这个视图操作就是只能对该视图中的数据进行操作,该操作也会保存在建立的表中.可以理解为表上表, ...
- Oracle PL/SQL高级应用 游标
游标可以处理SQL语句查询出来的结果集,进行逐条控制,其实游标在内存中申请空间,将自己指向SQL语句查询出来的结果集,有点像指针的感觉,游标使SQL更加的灵活. DECLARE CURSOR mycu ...
- Oracle.PL/SQL高级
一.匿名块 .使用returning ... INTO 保存增删改表数据时的一些列的值 ()增加数据时保存数据 DECLARE v_ename emp.ename%TYPE; v_sal emp.sa ...
- Oracle PL/SQL DBA 编程实践基础
[附:一文一图]
- Oracle+PL+SQL从入门到精通.丁士锋.清华大学出版社.2012
\t第1篇 pl/sql开发入门第1章 oracle 11g数据库系统1.1 关系型数据库系统介绍1.1.1 什么是关系型数据模型1.1.2 数据库系统范式1.1.3 关系型数据库管理系统1.1.4 ...
- 浅析Oracle PL/SQL 学习--未完待续
这是一篇关于Oracle Pl/SQL数据库编程的课程学习分享... 首先说明几点: 学习这门课程之前,已经学过并且掌握一些基础的SQL语句.数据库结构分析.ER图设计等知识: 这里也只是较为大概地将 ...
随机推荐
- Chrome的开发必备小技巧
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- UITableView的HeaderView和FooterView
header通过下面两个代理方法设置 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSIntege ...
- 怎对于Foreach 不能添加IF的问题
我不们直接在Foreach 里面直接添加IF,这样会报错,这个前提是子视图,其他的我没有试验过.像这样: @foreach (Gift.Modules.Model.Entitys.XT_CZ item ...
- win10安装nodejs
https://jingyan.baidu.com/article/b0b63dbfca599a4a483070a5.html 1 去官网下载对应版本的msi文件 2安装,path会自动设置 3 检验 ...
- nginx $document_uri 参数使用
$document_uri 表示访问的url 现在我的需求是,访问 www.abc.com 请求到 www.abc.com/abc/在nginx配置文件中加入 if ($document_uri ...
- 在linux下玩转usb摄像头
硬件平台:PC机一台 .usb摄像头 操作系统:Linux3.0.8 交叉编译环境:arm-none-Linux-gnueabi-gcc 4.5.1 调试步骤: 一.linux 内核解压 1.1使用 ...
- ElasticSearch 组合过滤器
1.布尔过滤器 前篇文章中(term精确查找)的两个例子都是单个过滤器(filter)的使用方式. 在实际应用中,我们很有可能会过滤多个值或字段.比方说,怎样用 Elasticsearch 来表达下面 ...
- 跟我一起透彻理解template模板模式
#include <iostream> using namespace std; //template模式. class Base { public: void DealWhat() { ...
- zabbix web监测
web monitoring(监测)属于业务监控,用来监控Web站点多方面的可用性,可以监控Web站点的下载速度.返回码和响应时间.Zabbix能够检测HTML中包含的预先定义的字符串,也可以模拟登录 ...
- POJ 1040 Transportation
链接:http://poj.org/problem?id=1040 Transportation Time Limit: 1000MS Memory Limit: 10000K Total Submi ...