Oracle 11g Release 1 (11.1) PL/SQL_理解 Collection 类型
了解 Oracle 集合意义在于:当我们采用编程语言开发应用程序时,对其内置的集合类型,如数组、链表等,使用得很多,也很方便,但
本文内容
了解 Oracle 集合意义在于:当我们采用编程语言开发应用程序时,对其内置的集合类型,如数组、链表等,使用得很多,也很方便,但是当业务逻辑变得复杂时,如需要同时向多个表插入数据,和一个表插入多条数据,也许还需要事物控制,此时使用匿名子程序似乎很合适。那么,在匿名子程序中使用集合类型就不可避免。
另外,若有一个用编程语言写的函数,其涉及集合操作,出于某种必要的原因,想改写成 Oracle 函数,那么,对了解 Oracle 集合操作很有必要。
定义 Collection 类型先定义 collection 类型,之后再声明该类型的变量。
你可以在模式级别、包或 PL/SQL 块内定义一个 collection 类型。
在模式级别创建的 collection 类型是 standalone stored type。用 CREATE TYPE 语句创建。它存储在数据库,直到用 DROP TYPE 语句删除该类型。
在包内创建的 collection 类型是 packaged type。它存储在数据库,直到用 DROP PACKAGE 语句删除包。
在 PL/SQL 块创建的 collection 类型只在块内可用,只有块嵌入在 standalone 或 packaged subprogram 中它才存储在数据库。
collection 类型遵循与其他类型、变量一样的作用域和实例化规则。当你输入一个块或子程序时,collection 被实例化,当退出时,销毁。在一个包中,当初从引用包时,collection 被实例化,并在结束数据库会话时,销毁。
你可以在任何 PL/SQL 块、子程序,或包的声明部分,使用 TYPE 来定义 TABLE 和 VARRAY 类型。
对于在 PL/SQL 内声明的 nested table 和 varray,table 或 varray 的元素类型可以是,除了 REF CURSOR 以外的任意 PL/SQL 数据类型。
当定义一个 VARRAY 类型时,必须用一个正整数指定最大大小。如下所示,定义一个能最多存储 366 个日期的 VARRAY:
DECLARE
TYPE Calendar IS VARRAY(366) OF DATE;
associative arrays 可以用任意键值插入元素。键不需要是连续的。键的数据类型可以是 PLS_INTEGER、VARCHAR2,或 VARCHAR2 子类型的一个:VARCHAR、STRING、或 LONG.
必须指定基于 VARCHAR2 键的长度,除了 LONG,因为,,它等价于声明一个 VARCHAR2(32760) 类型的键。类型 RAW、LONG RAW、ROWID、CHAR 和 CHARACTER 不允许做为一个 associative array 的键。LONG 和 LONG RAW 仅仅是为了向后兼容。
不允许初始化子句。associative arrays 没有构造标记。当使用基于 VARCHAR2 键来引用 associative arrays 中的一个元素时,你可以使用如 DATE 或 TIMESTAMP 等类型,只要该类型可以用 TO_CHAR 函数转换成 VARCHAR2。
声明 Collection 变量定义一个 collection 类型后,用该类型声明变量。在声明中使用新类型名,同预定义类型一样,如 NUMBER。
示例 1:演示声明 nested table、varray 和 associative array
VARCHAR2(30); TYPE varray_type NUMBER VARCHAR2(32) VARCHAR2(32) INDEX BY VARCHAR2(64); v1 nested_type; v2 varray_type; v3 assoc_array_num_type; v4 assoc_array_str_type; v5 assoc_array_str_type2; BEGIN -- an arbitrary number of strings can be inserted v1 v1 := nested_type('Shipping', 'Sales', 'Finance', 'Payroll'); v2 := varray_type(1, 2, 3, 4, 5); -- Up to 5 integers v3(99) := 10; -- Just start assigning to elements v3(7) := 100; -- Subscripts can be any integer values v4(42) := 'Smith'; -- Just start assigning to elements v4(54) := 'Jones'; -- Subscripts can be any integer values v5('Canada') := 'North America'; -- Just start assigning to elements v5('Greece') := 'Europe'; -- Subscripts can be string valuesEND;/

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

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



Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.
