|
DB2 Notes SQLCODEs
SQL Communication Area
Inserted into Working Storage by
05 SQLCODE PIC S9(9) COMP. SQL return code unique to MVS
05 SQLERRD(3) PIC S9(9) COMP. Number of rows changed by
INSERT, DELETE, or UPDATE
05 SQLSTATE PIC X(5). SQL return code recognized by all ANSI-compliant
platforms. Recommended for MVS programs that
communicate across platforms.
Row Names
A literal, computed value, or qualified name is given a unique name in the result table
by the
EXEC SQL
SELECT 'FULL AMOUNT' AS AMOUNT
,PRINC + INT AS LOAN
,H.CURR AS CURRENCY
,C.CUST AS CUSTOMER
INTO :AMOUNT
,:LOAN
,:CURRENCY
,:CUSTOMER
FROM T100.HIST H Implicit syntax for inner join
,T100.CUST C
WHERE H.CUST = C.CUST
END-EXEC.
Unions
The
EXEC SQL
DECLARE CURSOR ROW-CSR FOR
SELECT ROW_A AS ROW1
,ROW_B AS ROW2
FROM T01.TABLE
WHERE CDE > 55
UNION ALL Include duplicate rows
SELECT ROW_C AS ROW1
,ROW_D AS ROW2
FROM T02.TABLE
WHERE CDE > 55
UNION ALL
SELECT ROW_E AS ROW1
,ROW_F AS ROW2
FROM T03.TABLE
WHERE CDE > 55
ORDER BY ROW1, ROW2
END-EXEC.
Joins A join matches rows of multiple tables by designated columns, and creates a result table having rows composed of columns taken from the different tables. An inner join selects columns from only the matched rows. An outer join, selects columns from both matched and unmatched rows.
The inner join has an implicit and explicit syntax. In the explicit syntax,
Implicit Syntax
EXEC SQL
DECLARE CURSOR ROW-CSR FOR Multiple rows expected
SELECT ACT.REG AS REGION
,HST.CURR AS CURRENCY
,TRN.AMT AS AMOUNT
FROM T100.ACCT ACT All joined tables named
,T100.HIST HST
,T100.TRAN TRN
WHERE ACT.CUST = HST.CUST Need not include matching columns in result table
AND ACT.CUST = TRN.CUST
AND ACT.CUST = '5221'
ORDER BY REGION, CURRENCY, AMOUNT
END-EXEC.
Explicit Syntax
EXEC SQL
DECLARE CURSOR ROW-CSR FOR Multiple rows expected
SELECT ACT.REG AS REGION
,HST.CURR AS CURRENCY
,TRN.AMT AS AMOUNT
FROM T100.ACCT ACT First outer table named
JOIN T100.HIST HST
ON ACT.CUST = HST.CUST Need not include matching columns in result table
JOIN T100.TRAN TRN
ON ACT.CUST = TRN.CUST
WHERE ACT.CUST = '5221'
ORDER BY REGION, CURRENCY, AMOUNT
END-EXEC.
The result table is assembled in a series of steps in which each unjoined component table, called the new table or inner table, is joined to the provisional result table (which in the first step, is the first component table chosen for the join), called the composite table or outer table. In the explicit syntax, the sequence of tables entering the join is the order in which the tables appear in the SQL. In the implicit syntax, the sequence is unclear. Read-Only Tables
A view or cursor produces a read-only result table when the underlying
No update nor delete operation may be performed on a read-only table. Cursor-Controlled Tables
When multiple rows are expected in the result table, they must be accessed through a
cursor, which is created by a Views
A view is a Handling Nulls The value of an indicator variable tells the status of a row after a query.
01 FILLER.
05 WS-AMOUNT PIC S9(5)V9(2) COMP-3. Host variables
05 WS-CUSTNUM PIC X(5).
01 FILLER.
05 AMT-IND PIC S9(4) COMP. Indicator variable
EXEC SQL
SELECT CUST_AMOUNT
INTO :WS-AMOUNT:AMT-IND No intervening space
FROM T100.CUST
WHERE CUST_ID = :WS-CUSTNUM
END-EXEC.
After a query, the indicator variable contains the following:
Load -1 to the indicator variable to set a column to a null value, during
If a column is always to be set to a null value, code the
A column omitted from the row list of an
Code a predicate to test for null with the following syntax:
The scalar functions,
EXEC SQL
SELECT ACCT_REG AS REGION
,VALUE(ACCT_A1, ACCT_A2, 'NO ACCT') AS ACCOUNT
INTO :WS-REGION
,:WS-ACCOUNT
FROM T200.ACCT
WHERE ACCT_REG <> '65'
END-EXEC.
Variable-Length Columns The host variable for a variable-length column must be coded as a group item containing two fields: first, a length field, then a data field. Both must be 49-levels.
01 FILLER.
05 WS-NAME.
49 WS-NAME-LEN PIC S9(4) COMP. Column length
49 WS-NAME-TEXT PIC X(128) Column data
EXEC SQL
SELECT CUST_NAME
INTO :WS-NAME Load the group-level
FROM T100.CUST
WHERE CUST_ID = :WS-CUST-ID
END-EXEC.
EXEC SQL
SELECT CUST_ID
INTO :WS-CUST-ID
FROM T100.CUST
WHERE CUST_NAME = :WS-NAME-TEXT Test the data field
END-EXEC.
To Special Predicates “It’s like in-between.”
The
A mask may be a host variable or a literal enclosed in quotes and may contain any number
of:
For example:
To use a host variable for a mask to produce the same effect as the literal mask in the second-to-last example, code it right-padded with “%” characters to avoid the effect of the last example.
The
The
Subqueries
A subquery is an inner
EXEC SQL
DECLARE CURSOR ACCT_CSR FOR
SELECT ACCT_ID
,ACCT_NAME
FROM T100.ACCT
WHERE ACCT_ID NOT IN
(SELECT TRD_ACCT Select a fixed list of accounts
FROM T100.TRADE before the outer query
WHERE TRD_ADDR-STATE = :WS-STATE)
ORDER BY ACCT_ID
END-EXEC.
A correlated subquery has one or more predicates that reference columns returned by the outer query, providing a result that does vary depending upon the rows examined by the outer query. It is evaluated anew for each row that the outer query examines.
EXEC SQL
DECLARE CURSOR ACCT_CSR FOR
SELECT ACCT_ID
,ACCT_NAME
FROM T100.ACCT ACT
WHERE EXISTS
(SELECT * FROM T100.BROKER Select a new result table
WHERE ACT.ACCT_ID BETWEEN BKR_CUSTID_LOW for each row of the
AND BKR_CUSTID_HI) outer query
ORDER BY ACCT_ID
END-EXEC.
In a correlated subquery, the outer query must provide a correlation name, in this
case, the qualifier Grouping of Rows
The
The For example, to find the monthly total dollar-amount of trades in a given foreign currency, for each account executing at least 3 trades in that currency in a given month:
EXEC SQL
DECLARE CURSOR TRD-CSR FOR
SELECT TRD_ACCT
,SUM(TRD_DOLAMT)
FROM T100.TRADE
WHERE TRD_CUR_CDE = :WS-CUR-CODE Limit rows selected into the work table
AND TRD_MON_YR = :WS-MONTH-YEAR
GROUP BY TRD_ACCT
HAVING COUNT(*) >= 3 Limit groups of rows summarized into
ORDER BY TRD_ACCT the result table
END-EXEC.
To find the monthly total dollar-amount of all trades in all currencies, for each account executing at least 3 trades in a given currency in a given month: EXEC SQL
DECLARE CURSOR TRD-CSR FOR
SELECT TRD_ACCT Outer select grouped by account
,SUM(TRD_DOLAMT)
FROM T100.TRADE
WHERE TRD_ACCT IN
(SELECT TRD_ACCT Subquery also grouped by account
FROM T100.TRADE
WHERE TRD_CUR_CDE = :WS-CUR-CODE
AND TRD_MON_YR = :WS-MONTH-YEAR
GROUP BY TRD_ACCT
HAVING COUNT(*) >= 3)
GROUP BY TRD_ACCT
ORDER BY TRD_ACCT
END-EXEC.
Efficiency and Performance
A compound condition will not use row indexes, but a Copyright © 2006 The Stevens Computing Services Company, Inc. All rights reserved. |