-----PL/SQL的记录类型(复合数据类型)----------------------
定义——一种由逻辑上相关联的独立元素组成的复合数据类型。
记录只能存放一行数据,行变量table_name%rowtype cursor_name%rowtype是一种特殊的记录,记录结构有继承的源。
1、直接访问记录的元素,分别给每个字段赋值
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_org_id:=1001;
v_example_rec.hrc_descr:='CEO/COO';
v_example_rec.org_short_name:='Office of CEO/COO ABC Inc.';
dbms_output.put_line(to_char(v_example_rec.hrc_org_id));
dbms_output.put_line(v_example_rec.hrc_descr);
dbms_output.put_line(v_example_rec.org_short_name);
exception when others then
null;
end;
2、通过变量赋值
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec1 hrc_org_rec;
v_example_rec2 hrc_org_rec;
begin
v_example_rec1.hrc_org_id:=1001;
v_example_rec1.hrc_descr:='CEO/COO';
v_example_rec1.org_short_name:='Office of CEO/COO ABC Inc.';
v_example_rec2:=v_example_rec1; --变量赋值
dbms_output.put_line(to_char(v_example_rec2.hrc_org_id));
dbms_output.put_line(v_example_rec2.hrc_descr);
dbms_output.put_line(v_example_rec2.org_short_name);
exception when others then
null;
end;
3、通过select into语句来填充记录(与隐式游标结合使用)(psql用户下)
-----------------
oracle序列:序列是一种oracle的对象
SQL> create sequence hrc_org_seq minvalue 1 maxvalue 9999999 start with 100 increment by 1;
Sequence created.
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
100
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
101
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
102
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
103
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
104
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
105
用途:生成主键,表中的主键一般不会拿有业务含义的字段作为主键
生成主键的方法:
insert into tab_name values('06'||'A01'||to_char(sysdate,'yyyymmdd'))||hrc_org_seq.nextval,ss,xx,xx,xx,xx,....);
数据字典:
select * from dba_sequences where sequence_name=upper('hrc_org_seq'); --序列的定义
select * from dba_objects where object_name=upper('hrc_org_seq') --序列的基本属性
删除序列:
drop sequence hrc_org_seq;
-----------------------------------------------------------------------
==通过select into语句来填充记录(与隐式游标结合使用)==
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
select hrc_org_seq.nextval,h.hrc_descr,o.org_short_name
into v_example_rec
from org_tab o,hrc_tab h
where h.hrc_code=o.hrc_code
and o.org_id=1001;
dbms_output.put_line(to_char(v_example_rec.hrc_org_id));
dbms_output.put_line(to_char(v_example_rec.hrc_descr));
dbms_output.put_line(to_char(v_example_rec.org_short_name));
end;
通过循环的方式分别把值赋给记录。
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),
org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
for idx in (select org_id from org_tab) loop
select hrc_org_seq.nextval,h.hrc_descr,o.org_short_name
into v_example_rec
from org_tab o,hrc_tab h
where h.hrc_code=o.hrc_code
and o.org_id=idx.org_id;
dbms_output.put_line(to_char(v_example_rec.hrc_org_id));
dbms_output.put_line(to_char(v_example_rec.hrc_descr));
dbms_output.put_line(to_char(v_example_rec.org_short_name));
end loop;
end;
不用select into也可以实现,用分别赋值的方式
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
for idx in (select hrc_org_seq.nextval org_id,h.hrc_descr,o.org_short_name
from org_tab o,hrc_tab h
where h.hrc_code=o.hrc_code) loop
v_example_rec.hrc_org_id:=idx.org_id;
v_example_rec.hrc_descr:=idx.hrc_descr;
v_example_rec.org_short_name:=idx.org_short_name;
dbms_output.put_line(to_char(v_example_rec.hrc_org_id));
dbms_output.put_line(to_char(v_example_rec.hrc_descr));
dbms_output.put_line(to_char(v_example_rec.org_short_name));
end loop;
end;
==通过显式游标填充记录========
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec hrc_org_rec;
cursor csr_hrc_org is select hrc_org_seq.nextval org_id,h.hrc_descr,o.org_short_name
from org_tab o,hrc_tab h
where h.hrc_code=o.hrc_code;
begin
open csr_hrc_org;
loop
fetch csr_hrc_org into v_example_rec;
exit when (csr_hrc_org%notfound);
dbms_output.put_line(to_char(v_example_rec.hrc_org_id));
dbms_output.put_line(to_char(v_example_rec.hrc_descr));
dbms_output.put_line(to_char(v_example_rec.org_short_name));
end loop;
close csr_hrc_org;
end;
==通过隐式游标聚集赋值:====
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec1 hrc_org_rec;
v_example_rec2 hrc_org_rec;
begin
for idx in (select org_id from org_tab) loop
select hrc_org_seq.nextval,h.hrc_descr,o.org_short_name
into v_example_rec1
from org_tab o,hrc_tab h
where h.hrc_code=o.hrc_code
and o.org_id=idx.org_id;
v_example_rec2:=v_example_rec1; ---聚集赋值
dbms_output.put_line(to_char(v_example_rec2.hrc_org_id));
dbms_output.put_line(to_char(v_example_rec2.hrc_descr));
dbms_output.put_line(to_char(v_example_rec2.org_short_name));
end loop;
end;
给记录赋值方式:1、用显式游标赋值 2、用隐式赋值 3、分别赋值 4、聚集赋值
===4、测试两个记录是否相等====
if v_example_rec1=v_example_rec2 then xxxxxx 这种写法是错误的。
---------------------------
要进行一一比较
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec1 hrc_org_rec;
v_example_rec2 hrc_org_rec;
begin
for idx in (select org_id from org_tab) loop
select hrc_org_seq.nextval,h.hrc_descr,o.org_short_name
into v_example_rec1
from org_tab o,hrc_tab h
where h.hrc_code=o.hrc_code
and o.org_id=idx.org_id;
v_example_rec2:=v_example_rec1;
if (v_example_rec1.hrc_org_id=v_example_rec2.hrc_org_id and v_example_rec1.hrc_descr=v_example_rec2.hrc_descr and v_example_rec1.org_short_name=v_example_rec2.org_short_name) then --必须要每个属性比较
dbms_output.put_line('YES');
else
dbms_output.put_line('NO');
end if;
end loop;
end;
-------------------------------------------------------------
练习:通过隐式游标填充记录的方式输出员工的姓名、薪资、职位、部门名称。
declare
type rc is record(rc_ename varchar2(10),rc_sal number(7,2),rc_job varchar(9),rc_dname varchar(14));
v_emp rc;
begin
for idx in (select a.ename,a.sal,a.job,b.dname from emp a,dept b
where a.deptno=b.deptno)
loop
v_emp.rc_ename:=idx.ename;
v_emp.rc_sal:=idx.sal;
v_emp.rc_job:=idx.job;
v_emp.rc_dname:=idx.dname;
dbms_output.put_line(to_char(v_emp.rc_ename));
dbms_output.put_line(v_emp.rc_sal);
dbms_output.put_line(to_char(v_emp.rc_job));
dbms_output.put_line(to_char(v_emp.rc_dname));
end loop;
exception when others then
null;
end;
-------------------------------------------
5、行变量的记录
declare
cursor csr_hrc is select * from hrc_tab order by 1; --按升序排列
hrc_rec csr_hrc%rowtype;
hrc_rec1 hrc_tab%rowtype;
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
hrc_rec2 hrc_org_rec;
begin
open csr_hrc;
loop
fetch csr_hrc into hrc_rec;
exit when (csr_hrc%notfound);
dbms_output.put_line(to_char(hrc_rec.hrc_code)||' '||hrc_rec.hrc_descr);
end loop;
close csr_hrc;
dbms_output.put_line('=======================');
open csr_hrc;
loop
fetch csr_hrc into hrc_rec1;
exit when (csr_hrc%notfound);
dbms_output.put_line(to_char(hrc_rec1.hrc_code)||' '||hrc_rec1.hrc_descr);
end loop;
close csr_hrc;
dbms_output.put_line('=======================');
open csr_hrc;
loop
fetch csr_hrc into hrc_rec2;
exit when (csr_hrc%notfound);
dbms_output.put_line(to_char(hrc_rec2.hrc_code)||' '||hrc_rec2.hrc_descr);
end loop;
close csr_hrc;
end;
6、涉及到记录的DML操作
A insert语句
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:='Web analyze 1';
insert into hrc_tab values v_example_rec;
commit;
exception when others then
null;
end;
B update操作
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:='Web analyze 2';
update hrc_tab set row=v_example_rec where hrc_code=v_example_rec.hrc_code;
commit;
exception when others then
null;
end;
C delete操作
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:='Web analyze 2';
delete from hrc_tab where hrc_code=v_example_rec.hrc_code;
commit;
exception when others then
null;
end;
删除操作不能用记录作过滤条件的。
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:='Web analyze 2';
delete from hrc_tab where row=v_example_rec; --注意
commit;
exception when others then
null;
end;
报错!