本文章详细的介绍了关于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 |

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment