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

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

C# 中变量未赋值能用吗,各种类型的初始值是什么

对于一个局部变量,如果未赋值,是不能使用的 对于属性,未赋值,也能使用有系统默认值,默认值如下: 对于 int 类型,默认值是 0;对于 int? 类型,默认值是 null;对于 bool 类型,默认值是 false;对于 bool? 类型,默认值是 null;对于 string 类型,默认值是 null;对于 string? 类型,哈哈,没有这种写法,会出错;对于 DateTime 类型,默

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

mysql索引四(组合索引)

单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引;组合索引,即一个索引包含多个列。 因为有事,下面内容全部转自:https://www.cnblogs.com/farmer-cabbage/p/5793589.html 为了形象地对比单列索引和组合索引,为表添加多个字段:    CREATE TABLE mytable( ID INT NOT NULL, use

mysql索引三(全文索引)

前面分别介绍了mysql索引一(普通索引)、mysql索引二(唯一索引)。 本文学习mysql全文索引。 全文索引(也称全文检索)是目前搜索引擎使用的一种关键技术。它能够利用【分词技术】等多种算法智能分析出文本文字中关键词的频率和重要性,然后按照一定的算法规则智能地筛选出我们想要的搜索结果。 在MySql中,创建全文索引相对比较简单。例如:我们有一个文章表(article),其中有主键ID(

mysql索引二(唯一索引)

前文中介绍了MySQL中普通索引用法,和没有索引的区别。mysql索引一(普通索引) 下面学习一下唯一索引。 创建唯一索引的目的不是为了提高访问速度,而只是为了避免数据出现重复。唯一索引可以有多个但索引列的值必须唯一,索引列的值允许有空值。如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的时候就应该使用关键字UNIQUE,把它定义为一个唯一索引。 添加数据库唯一索引的几种

mysql索引一(普通索引)

mysql的索引分为两大类,聚簇索引、非聚簇索引。聚簇索引是按照数据存放的物理位置为顺序的,而非聚簇索引则不同。聚簇索引能够提高多行检索的速度、非聚簇索引则对单行检索的速度很快。         在这两大类的索引类型下,还可以降索引分为4个小类型:         1,普通索引:最基本的索引,没有任何限制,是我们经常使用到的索引。         2,唯一索引:与普通索引

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主