Oracle® Database PL/SQL Language Reference 11g Release 2 (11.2) Part Number E10472-05 |
|
|
View PDF |
The SELECT
INTO
statement retrieves values from one or more database tables (as the SQL SELECT
statement does) and stores them in either variables or a record (which the SQL SELECT
statement does not do).
By default, the SELECT
INTO
statement retrieves one or more columns from a single row. With the BULK
COLLECT
clause, this statement retrieves an entire result set into one or more collections. The SELECT
INTO
statement with the BULK
COLLECT
clause is faster than equivalent loops with FETCH
statements. For more information, see "Reducing Loop Overhead with Bulk SQL".
Topics:
Syntax
select_into_statement ::=
See table_reference ::=.
select_item ::=
See:
table_reference ::=
Semantics
select_into_statement
DISTINCT or UNIQUE
Causes the database to return only one copy of each set of duplicate rows selected. These two keywords are synonymous. Duplicate rows are those with matching values for each select_item
.
Restrictions on DISTINCT and UNIQUE
The total number of bytes in all select_item
expressions is limited to the size of a data block minus some overhead. This size is specified by the initialization parameter DB_BLOCK_SIZE
.
No select_item
expression can contain a LOB column.
ALL
Causes the database to return all rows selected, including all copies of duplicates. This is the default.
*
Selects all columns.
BULK COLLECT
Enables the SELECT
INTO
statement to return multiple rows, which it stores in one or more collections of records (which must already exist). You can declare associative arrays or nested tables that grow as needed to hold the entire result set.
Without BULK
COLLECT
, the SELECT
INTO
statement must return only one row. Otherwise, PL/SQL raises the predefined exception TOO_MANY_ROWS
and the values of the variables in the INTO
clause are undefined.
variable_name
The name of a variable into which a select_item
value is fetched. For each select_item
value returned by the query, there must be a corresponding, type-compatible variable in the list. With BULK
COLLECT
, variable_name
can be the name of a collection of records.
Restriction on variable_name You cannot select into a BOOLEAN
variable.
record_name
A user-defined or %ROWTYPE
record into which rows of values are selected. The record must have a corresponding, type-compatible field for each select_item
.
subquery
A SQL SELECT
statement (not a PL/SQL SELECT
INTO
statement) that provides a set of rows for processing.
alias
Another (usually short) name for the referenced column, table, or view.
rest_of_statement
Anything that can follow the FROM
clause in a SQL SELECT
statement (except the SAMPLE
clause). For the syntax of the SQL SELECT
statement, see Oracle Database SQL Language Reference.
select_item
If the SELECT
INTO
statement returns no rows, PL/SQL raises the exception NO_DATA_FOUND
. To guard against this exception, select the result of the aggregate function COUNT(*)
, which returns a single value even if no rows match the condition.
schema_name
The name of the schema that contains the table or view. The default is your own schema.
table_name
The name of a database table.
view_name
The name of a database view.
column_name
The name of a column of the table or view.
*
Selects all columns of the table or view.
sequence_name
The name of a sequence.
CURRVAL
The current value in the sequence.
NEXTVAL
The next value in the sequence.
alias
Another (usually short) name for the referenced column, table, or view.
table_reference
A reference to a table or view for which you have the SELECT
privilege, which is accessible when you run the SELECT
INTO
statement.
Examples
Example 6-48, "Declaring an Autonomous Function in a Package"
Example 7-13, "Validation Checks Guarding Against SQL Injection"
Related Topics
In this chapter:
In other chapters: