PL/SQL复合类型变量的使用(record、pl/sql表、bulk collect)
2013-10-19 14:14
471 查看
标量类型
变量—<
复合类型
1、******************************record复合类型变量的使用(用于取一行多列)*****************************************************
declare
--第一个变量声明
v_sal number(7,2);
--第二个变量声明
TYPE emp_record_type IS RECORD
(ename VARCHAR2(25),
job VARCHAR2(10),
sal NUMBER(7,2));
emp_record emp_record_type;
begin
emp_record.ename := 'Alvin';
emp_record.job := 'clerk';
emp_record.sal := 1000;
dbms_output.put_line(emp_record.ename||' '||emp_record.job||' '||emp_record.sal);
end;
/
--取一行多列的例子
declare
type emp_table_record is record (ename varchar2(20),empno number(10),sal number(7));
emp_1 emp_table_record ;
begin
select ename,empno,sal into emp_1 from scott.emp where empno=7788;
dbms_output.put_line(emp_1.ename ||chr(10) ||emp_1.empno||chr(10)||emp_1.sal);
end;
/
-----------%rowtype--------------
declare
emp_record scott.emp%rowtype;
begin
select * into emp_record from scott.emp where empno=7788;
dbms_output.put_line(emp_record.ename||' '||emp_record.sal);
end;
/
在我理解,%rowtype就是一种特殊的record。
2、*************************************PL/SQL表(INDEX BY表)类似数组(用于取一列多行)***********************************************************
declare
type emp_table_type is table of varchar2(20) index by binary_integer;
emp_table emp_table_type;
begin
emp_table(0) :='LUYANG';
emp_table(-1) :='SUNYI';
emp_table(2) :='zhengda';
dbms_output.put_line('index 0: '||emp_table(0));
dbms_output.put_line('index -1: '||emp_table(-1));
dbms_output.put_line('index 2: '||emp_table(2));
dbms_output.put_line('the first element of index --> '||emp_table.first );
dbms_output.put_line('the count of element in index --> '||emp_table.count);
dbms_output.put_line('the last element of index --> '||emp_table.last);
dbms_output.put_line('The index ''0'' prior element is --> '||emp_table.prior(0));
dbms_output.put_line('The index ''0'' next element is --> '||emp_table.next(0));
end;
/
--取一列多行
declare
type emp_table_type is table of varchar2(20) index by binary_integer;
emp_table emp_table_type;
begin
for i in 1..10 loop
select ename into emp_table(i)
from (select rownum a,ename from scott.emp)
where a=i;
dbms_output.put_line(emp_table(i)||chr(10));
end loop;
end;
/
--PL/SQL表 of后定义类型比较死板
3、********************************PL/SQL表+record类型(取多行多列)************************************************************
将PL/SQL表和record结合起来使用,这样避免了PL/SQL表类型 of后定义类型的死板缺点
declare
TYPE emp_record_type is record (ename varchar2(30),job varchar2(10),sal number(7,2));
TYPE emp_table_type is table of
emp_record_type index by binary_integer;
emp_table emp_table_type;
begin
select ename,job,sal into emp_table(0) from scott.emp where empno=7788;
dbms_output.put_line('index 0 : '||emp_table(0).ename);
end;
/
数组用来取多行,record/%rowtype用来取多列
4、*****************************************bulk collect 批量采集数据******************************************
①单列多行
declare
type abc is table of scott.emp.sal%type;
var abc;
begin
select sal bulk collect into var from scott.emp;
for i in 1..14 loop
dbms_output.put_line(var(i));
end loop;
end;
/
②多行多列
declare
type abc is table of scott.emp%rowtype;
var abc;
begin
select * bulk collect into var from scott.emp;
for i in 1..14 loop
dbms_output.put_line(var(i).ename||' '||var(i).sal);
end loop;
end;
/
**********************************************************************************************************************
练习:Plsql表+record+循环打印结果集(dept表的所有行所有列)
declare
type dept_type is table of scott.dept%rowtype index by binary_integer;
v_dept dept_type;
v_rows number;
begin
--dbms_output.put_line('DEPTNO'||' '||'DNAME'||' '||'LOC');
select count(*) into v_rows from scott.dept;
for i in 1..v_rows loop
select deptno,dname,loc into v_dept(i)
from (select rownum rn ,d.* from scott.dept d)
where rn=i;
dbms_output.put_line(v_dept(i).deptno||' '||v_dept(i).dname||' '||v_dept(i).loc);
end loop;
end;
/
---用bulk collect实现---------
declare
type dept_type is table of scott.dept%rowtype;
var dept_type;
v_rows number;
begin
select * bulk collect into var from scott.dept;
select count(*) into v_rows from scott.dept;
for i in 1..v_rows loop
dbms_output.put_line(var(i).deptno||' '||var(i).dname||' '||var(i).loc);
end loop;
end;
/
**********************************************************************************************************************
变量—<
复合类型
1、******************************record复合类型变量的使用(用于取一行多列)*****************************************************
declare
--第一个变量声明
v_sal number(7,2);
--第二个变量声明
TYPE emp_record_type IS RECORD
(ename VARCHAR2(25),
job VARCHAR2(10),
sal NUMBER(7,2));
emp_record emp_record_type;
begin
emp_record.ename := 'Alvin';
emp_record.job := 'clerk';
emp_record.sal := 1000;
dbms_output.put_line(emp_record.ename||' '||emp_record.job||' '||emp_record.sal);
end;
/
--取一行多列的例子
declare
type emp_table_record is record (ename varchar2(20),empno number(10),sal number(7));
emp_1 emp_table_record ;
begin
select ename,empno,sal into emp_1 from scott.emp where empno=7788;
dbms_output.put_line(emp_1.ename ||chr(10) ||emp_1.empno||chr(10)||emp_1.sal);
end;
/
-----------%rowtype--------------
declare
emp_record scott.emp%rowtype;
begin
select * into emp_record from scott.emp where empno=7788;
dbms_output.put_line(emp_record.ename||' '||emp_record.sal);
end;
/
在我理解,%rowtype就是一种特殊的record。
2、*************************************PL/SQL表(INDEX BY表)类似数组(用于取一列多行)***********************************************************
declare
type emp_table_type is table of varchar2(20) index by binary_integer;
emp_table emp_table_type;
begin
emp_table(0) :='LUYANG';
emp_table(-1) :='SUNYI';
emp_table(2) :='zhengda';
dbms_output.put_line('index 0: '||emp_table(0));
dbms_output.put_line('index -1: '||emp_table(-1));
dbms_output.put_line('index 2: '||emp_table(2));
dbms_output.put_line('the first element of index --> '||emp_table.first );
dbms_output.put_line('the count of element in index --> '||emp_table.count);
dbms_output.put_line('the last element of index --> '||emp_table.last);
dbms_output.put_line('The index ''0'' prior element is --> '||emp_table.prior(0));
dbms_output.put_line('The index ''0'' next element is --> '||emp_table.next(0));
end;
/
--取一列多行
declare
type emp_table_type is table of varchar2(20) index by binary_integer;
emp_table emp_table_type;
begin
for i in 1..10 loop
select ename into emp_table(i)
from (select rownum a,ename from scott.emp)
where a=i;
dbms_output.put_line(emp_table(i)||chr(10));
end loop;
end;
/
--PL/SQL表 of后定义类型比较死板
3、********************************PL/SQL表+record类型(取多行多列)************************************************************
将PL/SQL表和record结合起来使用,这样避免了PL/SQL表类型 of后定义类型的死板缺点
declare
TYPE emp_record_type is record (ename varchar2(30),job varchar2(10),sal number(7,2));
TYPE emp_table_type is table of
emp_record_type index by binary_integer;
emp_table emp_table_type;
begin
select ename,job,sal into emp_table(0) from scott.emp where empno=7788;
dbms_output.put_line('index 0 : '||emp_table(0).ename);
end;
/
数组用来取多行,record/%rowtype用来取多列
4、*****************************************bulk collect 批量采集数据******************************************
①单列多行
declare
type abc is table of scott.emp.sal%type;
var abc;
begin
select sal bulk collect into var from scott.emp;
for i in 1..14 loop
dbms_output.put_line(var(i));
end loop;
end;
/
②多行多列
declare
type abc is table of scott.emp%rowtype;
var abc;
begin
select * bulk collect into var from scott.emp;
for i in 1..14 loop
dbms_output.put_line(var(i).ename||' '||var(i).sal);
end loop;
end;
/
**********************************************************************************************************************
练习:Plsql表+record+循环打印结果集(dept表的所有行所有列)
declare
type dept_type is table of scott.dept%rowtype index by binary_integer;
v_dept dept_type;
v_rows number;
begin
--dbms_output.put_line('DEPTNO'||' '||'DNAME'||' '||'LOC');
select count(*) into v_rows from scott.dept;
for i in 1..v_rows loop
select deptno,dname,loc into v_dept(i)
from (select rownum rn ,d.* from scott.dept d)
where rn=i;
dbms_output.put_line(v_dept(i).deptno||' '||v_dept(i).dname||' '||v_dept(i).loc);
end loop;
end;
/
---用bulk collect实现---------
declare
type dept_type is table of scott.dept%rowtype;
var dept_type;
v_rows number;
begin
select * bulk collect into var from scott.dept;
select count(*) into v_rows from scott.dept;
for i in 1..v_rows loop
dbms_output.put_line(var(i).deptno||' '||var(i).dname||' '||var(i).loc);
end loop;
end;
/
**********************************************************************************************************************
相关文章推荐
- 刚安装时mysql不能修改密码以及忘记root密码
- MySQL增删改查
- MySQL增删改查
- SQL - 根据父节点查询所有子节点
- 安装oracle 11g rac (11.2.03) 出现PRVF-5637 错误(无DNS验证不通过)
- oracle11G在linux环境下的卸载操作
- 数据文件的DIO
- 文件系统和数据库系统的区别
- Oracle下载地址
- 几种连接数据库的OLEDB驱动程序
- LINQ 的select 和where 查询语句总结
- c#数据库帮助类SqlHelper
- win8 64位安装oracle(64位) plsql(32位)流程
- ORACLE修改列的数据类型的2种方法
- AIX安装恢复oracle问题-内存不足
- [SQL]透過redgate SQL Monitor 來找出 ASYNC_NETWORK_IO 問題
- SQL Server 存储过程
- sqlserver死锁查询及查杀
- SQL Server 检查死锁
- 查询Sqlserver数据库死锁的一个存储过程