oracle数组类型简单实例介绍
本文章详细的介绍了关于oracle数组的各种操作,有需要的同学可以参考一下。
本文章详细的介绍了关于oracle数组的各种操作,有需要的同学可以参考一下。Oracle数组一般可以分为固定数组和可变数组
固定数组
代码如下 | 复制代码 |
declare type v_ar is varray(10) of varchar2(30); my_ar v_ar:=v_ar('g','m','d','龚','帅'); begin for i in 1..my_ar.count loop dbms_output.put_line(my_ar(i)); end loop; end; declare type v_ar is varray(10) of varchar2(30); my_ar v_ar:=v_ar('g','m','d','龚','帅'); begin for i in 1..my_ar.count loop dbms_output.put_line(my_ar(i)); end loop; end; --可变数组 --一维数组 declare type v_table is table of varchar2(30) index by binary_integer; --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引, --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。 my_table v_table; begin for i in 1..20 loop my_table(i):=i; dbms_output.put_line(my_table(i)); end loop; end; declare type v_table is table of varchar2(30) index by binary_integer; --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引, --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。 my_table v_table; begin for i in 1..20 loop my_table(i):=i; dbms_output.put_line(my_table(i)); end loop; end; --多维数组--多条记录 declare type v_table is table of t_user%rowtype index by binary_integer; my_table v_table; begin * bulk collect into my_table from t_user; for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值 loop dbms_output.put_line('suser--'||my_table(i).suser); dbms_output.put_line('name---'||my_table(i).name); dbms_output.put_line('sex----'||my_table(i).sex); end loop; end; declare type v_table is table of t_user%rowtype index by binary_integer; my_table v_table; begin select * bulk collect into my_table from t_user; for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值 loop dbms_output.put_line('suser--'||my_table(i).suser); dbms_output.put_line('name---'||my_table(i).name); dbms_output.put_line('sex----'||my_table(i).sex); end loop; end; 多维数组--单条记录 declare type v_table is table of t_user%rowtype index by binary_integer; my_table v_table; begin select * into my_table(9) from t_user where suser='admin'; --my_table(i) i可以为任意整数,但取值时必须保持以i一致; dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name); end; declare type v_table is table of t_user%rowtype index by binary_integer; my_table v_table; begin select * into my_table(9) from t_user where suser='admin'; --my_table(i) i可以为任意整数,但取值时必须保持以i一致; dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name); end; --自定义数组 create or replace type varray_list as varray(30) of varchar2(50); --使用自定义数组 create or replace procedure show_list(p_varlist in varray_list) is v_str varchar2(50); begin for i in 1..p_varlist.count loop v_str:=p_varlist(i); dbms_output.put_line('v_str='||v_str); dbms_output.put_line('p_varlist('||i||')='||p_varlist(i)); end loop; end; declare my_var varray_list:=varray_list('g','m','d','龚','帅'); begin show_list(my_var); end; |
实例
代码如下 | 复制代码 |
--固定数组 --可变数组 --可变数组取表 create or replace procedure proc_stock(n number) --用数组实现 4.813 1.953 2 --访问自定义表 |
关于ORACLE中的数组:记录同集合
集合可以有三种实现方式:
1 自定义一个TYPE使用VARRAY来得到一个数组但只能对基本类型定义如:
CREATE TYPE 类型名 AS VARRAY OF VARCHAR2(20);
1 自定义一个TYPE使用VARRAY来得到一个数组但只能对基本类型定义如:
CREATE TYPE 类型名 AS VARRAY(52) OF VARCHAR2(20);
不能使用如下:
CREATE TYPE 类型名 AS VARRAY(52) OF 表名%ROWTYPE;
注意:使用VARRAY时一定要先指定数组大小
不然搞创建数组类型
2 内嵌表如:
TYPE 类型名 IS TABLE OF 具体类型如:(表名%ROWTYPE);
内嵌表数组分二种:Index_by表同嵌套表如上的就是嵌套表而Index_by表只要在其尾回上 INDEX BY
BINARY_INTEGER就可以了
例子:
代码如下 | 复制代码 |
declare cursor cur_test is select id,mc from test; type t_test1 is table of varchar2(60) index by binary_integer; type t_test2 is table of test%rowtype index by binary_integer; var_test1 t_test1; var_test2 t_test2; var_new t_test2; begin SELECT id,mc INTO var_test2(0) FROM test WHERE id='111'; dbms_output.put_line('var_test2(0):'||var_test2(0).id||'---'||var_test2(0).mc); SELECT id,mc INTO var_test2(8) FROM test WHERE id='333'; dbms_output.put_line('var_test2(8):'||var_test2(8).id||'---'||var_test2(8).mc); var_new := var_test2; dbms_output.put_line('===== copy var_test2 to var_new ====='); dbms_output.put_line('var_new(0):'||var_new(0).id||'---'||var_new(0).mc); dbms_output.put_line('var_new(8):'||var_new(8).id||'---'||var_new(8).mc); end; =================================================================================== DECLARE TYPE t_test1 IS TABLE OF test.id%TYPE; TYPE t_test2 IS VARRAY (10) OF test.id%TYPE; var_test1 t_test1; var_test2 t_test2; begin --var_test1(1) := ('test1.1'); --没有初始化不能赋值 var_test1 := t_test1('test1.1','test1.2','test1.3'); dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3)); var_test2 := t_test2('test2.1','test2.2','test2.3'); dbms_output.put_line('var_test2: '||var_test2(1)||','||var_test2(2)||','||var_test2(3)); var_test1(2) := 'test1.2_update'; dbms_output.put_line('==== 修改了var_test1(2) ===='); dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3)); dbms_output.put_line(var_test1.next(3)); dbms_output.put_line('var_test2元素个数: '||var_test2.limit()); end; |
嵌套表的元素可以是集合,注意赋值的时候是varray_element.record_column := 的形式.
除了构造函数外,集合还有很多内建函数,按照面向对象编成的叫法称之为方法。
方法==========描述====================================================================使用限
制
COUNT=========返回集合中元素的个数
DELETE========删除集合中所有元素
DELETE(x)=====删除元素下标为x的元素===================================================对
VARRAY非法
DELETE(x,y)===删除元素下标从X到Y的元素================================================对
VARRAY非法
EXIST(x)======如果集合元素x已经初始化,则返回TRUE, 否则返回FALSE
EXTEND========在集合末尾添加一个元素==================================================对
Index_by非法
EXTEND(x)=====在集合末尾添加x个元素===================================================对
Index_by非法
EXTEND(x,n)===在集合末尾添加元素n的x个副本============================================对
Index_by非法
FIRST=========返回集合中的第一个元素的下标号,对于VARRAY集合始终返回1。
LAST==========返回集合中最后一个元素的下标号, 对于VARRAY返回值始终等于COUNT.
LIMIT=========返回VARRY集合的最大的元素个数
===========================================Index_by集合和嵌套表无用
NEXT(x)=======返回在第x个元素之后及紧挨着它的元素的值,如果x是最后一个元素,返回null.
PRIOR(x)======返回在第x个元素之前紧挨着它的元素的值,如果x是第一个元素,则返回null。
TRIM==========从集合末端开始删除一个元素==============================================对于
index_by不合法
TRIM(x)=======从集合末端开始删除x个元素===============================================对
index_by不合法
********************************************************************************************
记录可以定义为:
TYPE 类型名 IS RECORDER (具休类型)
也可用:变量名 表名%ROWTYPE
例子:
隐式定义记录中,我们不用描述记录的每一个域,在声明记录变量时使用%ROWTYPE命令定义与表,
视图,游标有相同结构的记录。
有一些PL/SQL指令在使用隐式定义记录时没有使用%ROWTYPE属性,比如游标FOR循环或中的:old
和:new记录
代码如下 | 复制代码 |
declare t_record1 test%rowtype; cursor cur_test(v_id in varchar2) is select id,mc from test where id t_record2 cur_test%rowtype; begin for row_test in cur_test('333') loop t_record1.id := row_test.id; t_record1.mc := row_test.mc; t_record2.id := row_test.id; t_record2.mc := row_test.id; dbms_output.put_line('t_record1:'||t_record1.id||'---'||t_record1.mc); dbms_output.put_line('t_record2:'||t_record2.id||'---'||t_record2.mc); dbms_output.put_line('row_test:'||row_test.id||'---'||row_test.mc); dbms_output.put_line('================loop '||cur_test%rowcount||' times.'); end loop; exception when others then dbms_output.put_line(sqlcode||sqlerrm); end; ====================================================================================== declare type t_record is record ( id test.id%type, mc test.mc%type ); var_record t_record; counter number default 0; begin for row_test in (select id,mc from test) loop counter := counter + 1; var_record.id := row_test.id; var_record.mc := row_test.mc; dbms_output.put_line('var_record:'||var_record.id||'---'||var_record.mc); dbms_output.put_line('row_test:'||row_test.id||'---'||row_test.mc); dbms_output.put_line('================loop '||counter||' times.'); end loop; exception when others then dbms_output.put_line(sqlcode||sqlerrm); end; |
三、综合实例BULK COLLECT的用法
代码如下 | 复制代码 |
*/ set serverout on |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There are two types of PHP arrays, namely: 1. Index array, the subscript consists of numbers, starting from 0 by default, each number corresponds to the position of an array element in the array; 2. Associative array, the subscript consists of numerical values and characters It is composed of a mixture of strings. If a key name in an array is not a number, then the array is an associative array.

The data types in PHP arrays are divided into three categories: scalar type, composite type and special type. The eight subcategories are: 1. boolean, Boolean type; 2. integer, integer type; 3. float, floating point type, also known as As double; 4. string, string; 5. array, array; 6. object, object; 7. resource, resource type; 8. NULL, empty null.

Method of converting to array type: 1. Add the target type "(array)" enclosed in parentheses before the numerical variable to be converted, and the syntax is "(array) conversion variable"; 2. Use the settype() function, the syntax is " settype(conversion variable,"array")".

There are four types of PHP arrays, namely: 1. Numeric index array, each element of which has an integer subscript, starting from 0 and increasing; 2. Associative array, each element of which has a string Key; 3. Multi-dimensional array, you can create multi-dimensional arrays of any level; 4. Array with variable keys, you can dynamically add and delete arrays of key-value pairs.

In Golang, the data types of functions can be divided into structure types and array types. There are important differences between these two types. This article will analyze their differences. 1. Structure type A structure is a data type composed of some fields. These fields can be of different types, basic types or other custom types. In Golang, use the keyword "struct" to define a structure type, and then use the type name to create an instance of the structure. A structure can access its fields through dot notation, or

What types of arrays are there in PHP7.0? Array is a very important data type in PHP7.0. It can store multiple values and these values can be accessed through index or key. In PHP 7.0, array types are very flexible and can therefore be used to solve many different programming problems. In this article, we will introduce the array types in PHP7.0 and how to use them. 1. Ordinary arrays Ordinary arrays are one of the most common types in PHP7.0. It consists of an ordered list of key-value pairs, where

Traversal and operation skills of array data types in PHP In PHP, array is a very commonly used data type that allows us to store a group of related data elements in a variable. Using arrays, we can access and manipulate data in the array through indexes or associated keys. This article will introduce array traversal and manipulation techniques in PHP and provide some sample code for reference. Traversing Indexed Arrays Indexed arrays are the most common array type that use automatically assigned numbers as indexes. We can use for loop or fore

Question: Write a C program to find the array type that needs to be checked to determine whether the elements in a given array are even, odd, or both. Solution The user needs to input an array of integers and then display the type of the array. Example 1 − Input: 531, output: odd array. Example 2 − Input: 2468, output: even array. Example 3 − Input: 12345, output: mixed array. The algorithm refers to the algorithm given below to find the array type entered by the user. Step 1 − Read the size of the array at runtime. Step 2−Input array elements. Step 3 − as
