PL/SQL User's Guide and Reference

Contents Index Home Previous Next

SELECT INTO Statement

Description

The SELECT INTO statement retrieves data from one or more database tables, then assigns the selected values to variables or fields. For a full description of the SELECT statement, see Oracle7 Server SQL Reference.

Syntax

select_into_statement ::=

SELECT [DISTINCT | ALL] {* | select_item[, select_item]...}
   INTO {variable_name[, variable_name]... | record_name}
   FROM {table_reference | (subquery)} [alias]
          [, {table_reference | (subquery)} [alias]]...
   rest_of_select_statement;

select_item ::=

{  function_name[(parameter_name[, parameter_name]...)]
 | NULL
 | numeric_literal
 | [schema_name.]{table_name | view_name}.*
 | [[schema_name.]{table_name. | view_name.}]column_name
 | sequence_name.{CURRVAL | NEXTVAL}
 | 'text'} [[AS] alias]

table_reference ::=

[schema_name.]{table_name | view_name}[@dblink_name]

Keyword and Parameter Description

select_item

This is a value returned by the SELECT statement, then assigned to the corresponding variable or field in the INTO clause.

variable_name[, variable_name]...

This identifies a list of previously declared scalar variables into which select_item values are fetched. For each select_item value returned by the query, there must be a corresponding, type-compatible variable in the list.

record_name

This identifies a user-defined or %ROWTYPE record into which rows of values are fetched. For each select_item value returned by the query, there must be a corresponding, type-compatible field in the record.

subquery

This is query that provides a value or set of values to the SELECT statement. The syntax of subquery is like the syntax of select_into_ statement, except that subquery cannot have an INTO clause.

alias

This is another (usually short) name for the referenced column, table, or view, and can be used in the WHERE clause.

rest_of_select_statement

This is anything that can legally follow the FROM clause in a SELECT statement.

Usage Notes

The implicit SQL cursor and the cursor attributes %NOTFOUND, %FOUND, %ROWCOUNT, and %ISOPEN let you access useful information about the execution of a SELECT INTO statement.

When you use a SELECT INTO statement to assign values to variables, it should return only one row. If it returns more than one row, you get the following results:

If no rows are returned, you get these results:

Example

The following SELECT statement returns an employee's name, job title, and salary from the emp database table:

SELECT ename, job, sal INTO my_ename, my_job, my_sal FROM emp
   WHERE empno = my_empno;

Related Topics

Assignment Statement, FETCH Statement, %ROWTYPE Attribute


Contents Index Home Previous Next