Oracle中record和table类型的创建和使用
来源:互联网 发布:mac office 账号 编辑:程序博客网 时间:2024/06/04 19:51
今天在看一个sql脚本,有几个地方似懂非懂,就上网具体查了一下,终于彻底搞明白了,做以下总结以防忘记。(千万不要似懂非懂就过去了,本人已经在这方面吃过很多亏了,正努力改正中)
1、recoed定义
由单行多列的标量构成的复合结构。
可以认为是用户自定义的一种数据类型,本人自己理解为类似于C语言中的结构体扮演的角色。
2、如何创建和使用record
2.1 创建
type Record_Name is record
(
filed1 type1 [not null][:=expr1],
......
filedn typen [not null][:=exprn],
);
其中,filed1为标量名字,type1为类型,类型不能为空。
2.2 声明record类型的变量
r_Record Record_Name;
r_Record为变量名(类似结构体变量名),Record_Name为2.1中定义的类型名(类似结构体名)。
2.3 赋值
r_Record.filed1 := xxx
2.4 访问字段
对于记录内每个字段(filed1.。。。),可以指定也可以使用TABLE_NAME%TYPE和TABLE_NAME%ROWTYPE动态指定记录字段类型。好处是表字段发生变化,记录字段自动改变。但是,由于每次执行前,遇到%TYPR或%ROWTYPE,数据库系统都会去查看对应表字段类型,会造成一定的数据库开销,如果系统中大量使用记录类型,则对性能会有一定影响。所以,如果能够确定类型,而且类型不是经常改动的话,还是直接写类型比较好,但一定要在对应的表更改类型之后更改记录类型,不然会出现错误(这一点容易忘记)。r_Record.filed1
......
r_Record filedn
注意:
3、table类型
table类型是oracle中的一种集合类型,可以通俗理解为类似于C语言中的数组。
4、创建和使用table类型
4.1 创建
type Table_Name is table of integer;
其中,Table_Name是类型名字,integer是集合中将要存放元素的类型。
4.2 声明table类型的变量
t_Table Table_Name;
4.3 赋值
t_Table := Table_Name(); --用一个空集合来初始化t_Table
t_Table.extend; --增加一条记录
注意:
一般情况下table类型的大小是不会自动变化的,没添加一条记录之前必须extend一下,否则会出错,如果想自动增加,创建table的时候需要在后面添加index by binary_integer,加上这段语句,使用的时候不需要初始化也不需要extend,比较方便。
t_Table(1) := 5; --如果元素类型是integer可以这么写
4.4 访问
一般访问都是放在一个循环中。
for i in 1 .. 100 loop
t_Table.extend;
t_Table(i) := 5;
end loop;
5、实例
下面写一个record和table一起使用的例子,例子很简单,主要就是熟悉一下如何使用record和table类型的变量。
create table Student( s_stuName varchar2(32) not null, n_stuID integer primary key);insert into Student values('stu001', 1);insert into Student values('stu002', 2);insert into Student values('stu003', 3);insert into Student values('stu004', 4);declare type Stu_Record is record(s_stuName varchar2(32), n_stuID integer); type Stu_Table is table of Stu_Record index by binary_integer; t_Stu_Table Stu_Table; i integer := 0;begin for rec in (select * from student) loop i := i+1; t_Stu_Table(i).s_stuName := rec.s_stuName; t_Stu_Table(i).n_stuID := rec.n_stuID; end loop; for i in 1 .. t_Stu_Table.count loop dbms_output.put_line('Name = ' || t_Stu_Table(i).s_stuName || ', ID = ' || t_Stu_Table(i).n_stuID); end loop; end;
运行结果为:
Name = stu001, ID = 1
Name = stu002, ID = 2
Name = stu003, ID = 3
Name = stu004, ID = 4
参考文章:http://blog.csdn.net/qfs_v/article/details/2446941
http://blog.csdn.net/liyongjie/article/details/7397471
- Oracle中record和table类型的创建和使用
- ORACLE中record、varray、table和%type、%rowtype的使用详解
- ORACLE中RECORD、VARRAY、TABLE的使用详解
- ORACLE中RECORD、VARRAY、TABLE的使用详解
- ORACLE中RECORD、VARRAY、TABLE的使用详解
- ORACLE中RECORD、VARRAY、TABLE的使用详解
- ORACLE中RECORD、VARRAY、TABLE的使用详解
- ORACLE中RECORD、VARRAY、TABLE的使用详解
- pl/sql table和record类型
- ORACLE中RECORD的使用
- oracle 中序列的创建和使用
- Oracle中索引的创建和使用
- oracle中视图的创建和使用
- oracle中数据类型rowtype,Table,Object,Record
- delphi中Record 和Packed Record的区别
- delphi中Record 和Packed Record的区别
- delphi中Record 和Packed Record的区别
- delphi中Record 和Packed Record的区别
- 【android, 3】3.操作数据保存到rom,sd卡上,sharedpreference的使用,pul解析xml
- 临界区、互斥对象、WaitForSingleObject、WaitForMultipleObjects
- Android-Looper类介绍
- 【Java并发编程】之十八:第五篇中volatile意外问题的正确分析解答(含代码)
- 子元素使用float后使父元素有高度的方法
- Oracle中record和table类型的创建和使用
- IOS push消息的数字不减少的问题
- xcode+ APP 打包以及提交apple审核详细流程(新版本更新提交审核)
- Windows Service里面的Afaria Server不能启动 - SQL Anywhere服务器不能启动
- 基于C#弹幕类射击游戏的实现——(八)敌机
- 山东理工大学ACM平台题答案关于C语言 1238 求数列的和
- Windows2008安装IIS方法
- History of Programming Languages Must Know [Infographic]
- 在线重定义