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

相关文章

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I