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

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
