94 Modernizing IBM Eserver iSeries Application Data Acess - A Roadmap Cornerstone
C Return
7.7 Static SQL without cursor
Because the use of SQL-described tables and views in native I/O has some restrictions (that
is, they cannot be sorted), we must look for an alternate method.
With static and dynamic SQL you can embed SQL statements into your source code.
In static SQL, the statement is determined at compile time. All SQL scalar functions can be
used in the embedded SQL statements. You can integrate host variables, which are set at
runtime. The syntax is checked by the precompiler and then the SQL statements are replaced
by adequate function calls.
Static SQL is commonly used in these ways:
To return one single row from a select statement into host variables
To insert, update, or delete several rows using one single SQL statement
For other actions such as:
– To declare global temporary tables
– To create and drop temporary aliases
– To grant temporary privileges
– To set path or set schema
7.7.1 Static SQL returning a single row
If the result of an SQL statement will be only one row, it can be directly returned into host
variables in one of the following manners:
SELECT ... INTO
SET HostVariable = SELECT ...
SELECT ... INTO
The SELECT INTO statement produces a result table consisting of at most one row, and
assigns the values in that row to host variables.
If a single row is returned, SQLCODE is set to 0 or a value between 1 and 99, and the
appropriate values are assigned to the host variables.
If the table is empty, the statement assigns +100 to SQLCODE and '02000' to
SQLSTATE. If you do not use indicator variables to detect NULL values, the host
variables are
not updated; otherwise NULL is returned.
If the result consists of more than one row, SQLCODE -811 is returned, but the host
variables are updated with the results from the first row.
Example 7-17 shows how the total amount of an order is calculated and returned within a
SELECT ... INTO statement.
Example 7-17 Using SELECT ... INTO to retrieve summary values
D TotalDetail S 11P 2
*-----------------------------------------------------------------------------------------
C/EXEC SQL
C+ Select Sum(OrderDtl_Total)
C+ into :TotalDetail
C+ from Order_Detail