Oracle8i SQLJ Developer's Guide and Reference
Release 8.1.5

A64684-01

Library

Product

Contents

Index

Prev  Chap Top Next

Single-Row Query Results--SELECT INTO Statements

When only a single row of data is being returned from the database, SQLJ allows you to assign selected items directly to Java host expressions inside SQL syntax. This is done using the SELECT INTO statement. The syntax is as follows:

#sql { SELECT expression1,..., expressionN  INTO :host_exp1,..., :host_expN  
       FROM datasource <optional clauses> };

Where:

A SELECT INTO statement must return one and only one row of data, otherwise an error will be generated at runtime.

The default is OUT for a host expression in an INTO-list, but you can optionally state this explicitly:

#sql { SELECT column_name1, column_name2  INTO :OUT host_exp1, :OUT host_exp2  
       FROM table WHERE condition };

Trying to use an IN or INOUT token in the INTO-list will result in an error at translation time.


Notes:

  • Permissible syntax for expression1 through expressionN, the datasource, and the optional clauses is the same as for any SQL SELECT statement. For information about what is permissible in Oracle SQL, see the Oracle8i SQL Reference.

  • There can be any number of SELECT-list and INTO-list items, as long as they match (one INTO-list item per SELECT-list item, with compatible types).

 

Examples of SELECT INTO Statements

The examples below assume the following table definition:

CREATE TABLE EMP (
   EMP_NUM NUMBER,
   EMP_NAME CHAR(30),
   HIRE_DATE DATE );

The first example is a SELECT INTO statement with a single host expression in the INTO-list:

String name;
#sql { SELECT emp_name INTO :name FROM emp WHERE emp_num=28959 };

The second example is a SELECT INTO statement with multiple host expressions in the INTO-list:

String name;
Date hiredate;
#sql { SELECT emp_name, hire_date INTO :name, :hiredate FROM emp 
       WHERE emp_num=28959 };

Examples with Host Expressions in SELECT-List

It is legal to use Java host expressions in the SELECT-list as well as in the INTO-list.

For example, you can select directly from one host expression into another (though this is of limited usefulness):

...
#sql { SELECT :name1 INTO :name2 FROM emptable WHERE emp_num=28959 };
...

More realistically, you may want to perform an operation or concatenation on the data that is selected, as in the following examples (assume Java variables were previously declared and assigned, as necessary):

...
#sql { SELECT salary + :raise INTO :newsal FROM emptable WHERE emp_num=28959 };
...

...
#sql { SELECT :(firstname + " ") || emp_last_name INTO :name FROM emptable 
       WHERE emp_num=28959 };
...

In the second example, firstname is concatenated to " " using a Java host expression and Java string concatenation (the + operator). This result is then passed to the SQL engine which uses SQL string concatenation (the || operator) to append the last name.




Prev

Top

Next
Oracle
Copyright © 1999 Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index