PL/SQL复合类型变量的使用(record、pl/sql表、bulk collect)

2024-05-13 02:58

本文主要是介绍PL/SQL复合类型变量的使用(record、pl/sql表、bulk collect),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        标量类型
变量—<
        复合类型




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;
/
**********************************************************************************************************************










这篇关于PL/SQL复合类型变量的使用(record、pl/sql表、bulk collect)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/984493

相关文章

java中XML的使用全过程

《java中XML的使用全过程》:本文主要介绍java中XML的使用全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录什么是XML特点XML作用XML的编写语法基本语法特殊字符编写约束XML的书写格式DTD文档schema文档解析XML的方法​​DOM解析XM

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

sql语句字段截取方法

《sql语句字段截取方法》在MySQL中,使用SUBSTRING函数可以实现字段截取,下面给大家分享sql语句字段截取方法,感兴趣的朋友一起看看吧... 目录sql语句字段截取sql 截取表中指定字段sql语句字段截取1、在mysql中,使用SUBSTRING函数可以实现字段截取。例如,要截取一个字符串字

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

使用Nginx配置文件服务器方式

《使用Nginx配置文件服务器方式》:本文主要介绍使用Nginx配置文件服务器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 为什么选择 Nginx 作为文件服务器?2. 环境准备3. 配置 Nginx 文件服务器4. 将文件放入服务器目录5. 启动 N

MySQL 字符串截取函数及用法详解

《MySQL字符串截取函数及用法详解》在MySQL中,字符串截取是常见的操作,主要用于从字符串中提取特定部分,MySQL提供了多种函数来实现这一功能,包括LEFT()、RIGHT()、SUBST... 目录mysql 字符串截取函数详解RIGHT(str, length):从右侧截取指定长度的字符SUBST

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

MySQL中的事务隔离级别详解

《MySQL中的事务隔离级别详解》在MySQL中,事务(Transaction)是一个执行单元,它要么完全执行,要么完全回滚,以保证数据的完整性和一致性,下面给大家介绍MySQL中的事务隔离级别详解,... 目录一、事务并发问题二、mysql 事务隔离级别1. READ UNCOMMITTED(读未提交)2

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件