SELECT INTO

The SELECT INTO statement produces a result table consisting of at most one row, and assigns the values in that row to variables. If the table is empty, the statement assigns +100 to SQLCODE and '02000' to SQLSTATE and does not assign values to the variables. If more than one row satisfies the search condition, statement processing is terminated, and an error occurs.

Invocation

This statement can only be embedded in an application program. It is an executable statement that cannot be dynamically prepared. It must not be specified in REXX.

Authorization

The privileges held by the authorization ID of the statement must include at least one of the following:

For information on the system authorities corresponding to SQL privileges, see Corresponding System Authorities When Checking Privileges to a Table or View.

Syntax

Click to skip syntax diagram
Read syntax diagramSkip visual syntax diagram                        .-,--------.
                        V          |
>>-select-clause--INTO----variable-+---------------------------->
 
>--from-clause--+--------------+--+-----------------+--+---------------+-->
                '-where-clause-'  '-group-by-clause-'  '-having-clause-'
 
                                               (1)
>--+-----------------+--+--------------------+-------+------------------+-><
   '-order-by-clause-'  '-fetch-first-clause-'       '-isolation-clause-'
 
Notes:
  1. Only one row may be specified in the fetch-first-clause.

Description

The result table is derived by evaluating the isolation-clause, from-clause, where-clause, group-by-clause, having-clause, order-by-clause, fetch-first-clause, and select-clause, in this order.

See Queries for a description of the select-clause, from-clause, where-clause, group-by-clause, having-clause, order-by-clause, fetch-first-clause, and isolation-clause.

INTO variable,...
Identifies one or more host structures or variables that must be declared in the program in accordance with the rules for declaring host structures and variables. In the operational form of the INTO clause, a reference to a host structure is replaced by a reference to each of its variables. The first value in the result row is assigned to the first variable in the list, the second value to the second variable, and so on. The data type of each variable must be compatible with its corresponding column.

Note

Variable assignment: Each assignment to a variable is performed according to the retrieval assignment rules described in Assignments and comparisons. If the number of variables is less than the number of values in the row, an SQL warning (SQLSTATE 01503) is returned (and the SQLWARN3 field of the SQLCA is set to 'W'). Note that there is no warning if there are more variables than the number of result columns. If a value is null, an indicator variable must be provided for that value.

If the specified variable is character and is not large enough to contain the result, a warning (SQLSTATE 01004) is returned (and 'W' is assigned to SQLWARN1 in the SQLCA). The actual length of the result may be returned in the indicator variable associated with the variable, if an indicator variable is provided. For further information, see References to variables.

If an assignment error occurs, the value of that variable and any following variables is unpredictable. Any values that have already been assigned to variables remain assigned.

Empty result table: If the result table is empty, the statement assigns '02000' to the SQLSTATE variable and does not assign values to the variables.

Result tables with more than one row: If more than one row satisfies the search condition, statement processing is terminated and an error is returned (SQLSTATE 21000). If an error occurs because the result table has more than one row, values may or may not be assigned to the variables. If values are assigned to the variables, the row that is the source of the values is undefined and not predictable.

Result column evaluation considerations: When a TIME value is selected, if the ISO, EUR, or JIS format is used, the length of the variable must not be less than 5. If the length is 5, 6, or 7, the seconds part of the time is omitted from the result, a warning (SQLSTATE 01004) is returned (and 'W' is assigned to SQLWARN1 in the SQLCA). In this case, the seconds part of the time is assigned to the indicator variable if one is provided, and, if the length is 6 or 7, blank padding occurs so that the value is a valid string representation of a time.

If an error occurs while evaluating a result column in the SELECT list of a SELECT INTO statement, as the result of an arithmetic expression (such as division by zero, or overflow) or a numeric or character conversion error, the result is the null value. As in any other case of a null value, an indicator variable must be provided. The value of the variable is undefined. In this case, however, the indicator variable is set to the value of -2. Processing of the statement continues and a warning is returned. If an indicator variable is not provided, an error is returned and no more values are assigned to variables. It is possible that some values have already been assigned to variables and will remain assigned when the error is returned.

Examples

Example 1: Using a COBOL program statement, put the maximum salary (SALARY) from the EMPLOYEE table into the host variable MAX-SALARY (DECIMAL(9,2)).

   EXEC SQL  SELECT MAX(SALARY)
               INTO :MAX-SALARY
               FROM EMPLOYEE WITH CS
   END-EXEC.

Example 2: Using a Java™ program statement, select the row from the EMPLOYEE table on the connection context 'ctx' with a employee number (EMPNO) value the same as that stored in the host variable HOST_EMP (java.lang.String). Then put the last name (LASTNAME) and education level (EDLEVEL) from that row into the host variables HOST_NAME (String) and HOST_EDUCATE (Integer).

   #sql [ctx] {  SELECT LASTNAME, EDLEVEL
                 INTO :HOST_NAME, :HOST_EDUCATE
                 FROM EMPLOYEE
                 WHERE EMPNO = :HOST_EMP   };

Example 3: Put the row for employee 528671, from the EMPLOYEE table, into the host structure EMPREC. Assume that the row will be updated later and should be locked when the query executes.

   EXEC SQL  SELECT *
               INTO :EMPREC
               FROM EMPLOYEE
               WHERE EMPNO = '528671'
               WITH RS USE AND KEEP EXCLUSIVE LOCKS
   END-EXEC.



[ Top of Page | Previous Page | Next Page | Contents | Index ]