Anmerkung:
The documentation updates provided here only cover the changes
specific to Natural for DB2 Version 8.4.
Using Natural Statements and System Variables under Natural for DB2 Version 8.4
Dynamic and Static SQL Support under Natural for DB2 Version 8.4
For the changes in installation, see Installing Natural for DB2 Version 8.4.1 in the Natural Installation documentation.
Anmerkung:
This is an extract of the chapter Using Natural SQL Statements and System
Variables and only describes the changes specific to
Natural for DB2 Version 8.4.
When a Natural program contains a DML UPDATE
statement,
this statement is translated into an SQL UPDATE
statement and a
FOR UPDATE OF
clause is
added to the SELECT
statement.
Be aware that a primary key field is not part of a FOR UPDATE
OF
list except that the compiler option
DB2PKYU
is set to ON
. If DB2PKYU
is set to OFF
(default), a primary key field can only be updated by using a non-cursor
UPDATE
operation (see also Natural SQL
UPDATE
statement in the section Using Natural SQL Statements).
A scalar function is a built-in function that can be used in the construction of scalar computational expressions. Scalar functions are specific to DB2 and belong to the Natural SQL Extended Set.
The scalar functions Natural for DB2 supports are listed below in alphabetical order:
A - H | I - R | S - Z |
---|---|---|
|
|
|
Each scalar function is followed by one or more scalar expressions in parentheses. The number of scalar expressions depends upon the scalar function. Multiple scalar expressions must be separated from one another by commas.
Example:
SELECT NAME INTO NAME FROM SQL-PERSONNEL WHERE SUBSTR ( NAME, 1, 3 ) = 'Fri' ...
The following column functions do not conform to standard SQL. They are specific to DB2 and belong to the Natural SQL Extended Set.
COUNT_BIG
CORRELATION
COVARIANCE
COVARIANCE_SAMP
MEDIAN
PERCENTILE_CONT
PERCENTILE_DISC
STDDEV
STDDEV_POP
STDDEV_SAMP
VAR
VAR_POP
VAR_SAMP
VARIANCE
VARIANCE_SAMP
XMLAGG
With the exception of USER
, the following special
registers do not conform to standard SQL. They are specific to DB2 and belong
to the Natural SQL Extended
Set:
CURRENT APPLICATION COMPATIBILITY
CURRENT APPLICATION ENCODING SCHEME
CURRENT CLIENT_ACCNTG
CURRENT CLIENT_APPLNAME
CURRENT CLIENT_CORR_TOKEN
CURRENT CLIENT_USERID
CURRENT CLIENT_WRKSTNNAME
CURRENT DATE
CURRENT_DATE
CURRENT DEBUG MODE
CURRENT DECFLOAT ROUNDING MODE
CURRENT DEGREE
CURRENT FUNCTION PATH
CURRENT GET_ACCEL_ARCHIVE
CURRENT_LC_CTYPE
CURRENT LC_CTYPE
CURRENT LOCALE LC_CTYPE
CURRENT MAINTAINED TABLE TYPES FOR
OPTIMIZATION
CURRENT_MEMBER
CURRENT OPTIMIZATION HINT
CURRENT PACKAGE PATH
CURRENT PACKAGESET
CURRENT_PATH
CURRENT PRECISION
CURRENT QUERY ACCELERATION
CURRENT REFRESH AGE
CURRENT ROUTINE VERSION
CURRENT RULES
CURRENT SCHEMA
CURRENT SERVER
CURRENT SQLID
CURRENT TEMPORAL BUSINESS_TIME
CURRENT TEMPORAL_SYSTEM_TIME
CURRENT TIME
CURRENT_TIME
CURRENT TIMESTAMP
CURRENT TIMEZONE
CURRENT_TIMEZONE USER
SESSION TIME ZONE
SESSION_USER
USER
A reference to a special register returns a scalar value.
Using the command SET CURRENT SQLID
, the
creator name of a table can be substituted by the current SQLID. This enables
you to access identical tables with the same table name but with different
creator names.
Anmerkung:
This is an extract of the chapter
Select
Expressions (Statements
documentation) and only describes the changes specific to Natural for DB2
Version 8.4.
, | ||||||||
* |
A selection
specifies the
columns of the result set tables to be selected.
Syntax Element Description:
Syntax Element | Description |
---|---|
ALL|DISTINCT |
Elimination of Duplicate Rows:
Duplicate rows are not automatically eliminated from the result of
a The alternative to |
scalar-expression |
Scalar Expression:
Instead of, or as well as, simple column names, a selection can also include general scalar expressions containing scalar operators and scalar functions which provide computed values (see also the section Scalar Expressions). Example: SELECT NAME, 65 - AGE FROM SQL-PERSONNEL ... |
AS |
The optional keyword AS introduces
a correlation-name
for a column.
|
correlation-name |
Correlation Name:
A The |
unpack-row |
See unpack-row below. |
* |
Asterisk Notation:
All columns of the result table are selected. Example: SELECT * FROM SQL-PERSONNEL, SQL-AUTOMOBILES ... |
UNPACK (scalar-expression)
.* AS ({field-name
data-type} ,
)
|
An unpack-row
specifies a row
of unpacked binary values that are returned when the SQL UNPACK function is
invoked. The number of field-names and
data-types must match the number of fields returned
by the UNPACK function.
GROUP BY |
, |
The GROUP BY
clause specifies a grouping of the result
table. The result of GROUP BY
is a set of groups of rows. Within
each group of more than one row, all values defining the group are equal.
- grouping-expression
A grouping expression is a scalar expression that defines the grouping of a result set.
- grouping-set
GROUPING SETS
( , ) ( , ) A
grouping-set
is used to specify multiple grouping clauses in a single statement. Agrouping-set
combines two or more groups of rows into a single result set. It is the same as the union of multiple select expressions with aGROUP BY
clause where each expression corresponds to onegrouping-set
. Agrouping-set
is a single element or a list of elements delimited by parentheses. An element is either agrouping–expression
or asuper-group
. Agrouping-set
has the advantage that the groups are computed with a single pass over the base table.- super-group
(grouping-expression-list)
(grouping-expression-list( )
A
super-group
is a more complexgrouping-set
.A
grouping-expression-list
defines the number of elements used in aROLLUP
orCUBE
operation. Elements with multiplegrouping-expressions
are delimited by parentheses:
grouping-expression , (grouping-expression, )
ROLLUP
andCUBE
return a row which is the overall (grand total) aggregation. This can be specified with empty parentheses( )
within theGROUPING SETS
clause.
- ROLLUP
A
ROLLUP
grouping is like a series ofgrouping-sets
. In addition to the regular grouped rows, aROLLUP
grouping produces a result set that contains subtotal rows. Subtotal rows are "super-aggregate" rows which contain additional aggregates. The aggregate values are retrieved with the same column functions that are used to obtain the regular grouped rows.In general, you specify a
ROLLUP
withn
elements asGROUP BY ROLLUP (c1, c2, ..., cn-1, cn)which is the equivalent of:
GROUP BY GROUPING SETS ((c1, c2, ..., cn-1, cn), (c1, c2, ..., cn-1), ... (c1, c2), (c1), ( ))- CUBE
A
CUBE
grouping is like a series ofgrouping-sets
. In addition to theROLLUP
aggregation rows,CUBE
produces a result set that contains cross-tabulation rows. Cross-tabulation rows are additional "super-aggregate" rows. Thegrouping-expression-list
of aCUBE
computes all permutations along with the grand total. As a result, then
elements of aCUBE
translate to 2**ngrouping-sets
. For example:GROUP BY CUBE (a, b, c)is the equivalent of:
GROUP BY GROUPING SETS ((a, b, c), (a, b), (a, c), (b, c), (a), (b), (c), ())
Anmerkung:
This is an extract of the chapter Dynamic and Static SQL
Support and only describes the changes specific to Natural
for DB2 Version 8.4.
If #SWITCH-BY-TRANSACTION-ID
is set to FALSE
,
the desired plan name is written to a temporary storage queue for a CICS/DB2
exit routine specified as PLANExit attribute of a DB2ENTRY or of the DB2CONN
definition, the NATPLAN
program must be invoked before the first
DB2 access. Natural for DB2 provides NDBUEXT
as CICS DB2 plan
selection exit program. For additional information on CICS/DB2 exit routines,
refer to the relevant IBM literature.
The name of the temporary storage queue is
PLANsssstttt
, where
ssss
is the remote or local CICS system
identifier and tttt
the CICS terminal
identifier.
When running in a CICSplex environment, the CICS temporary storage
queue PLANsssstttt
containing the plan
name must be defined with TYPE=SHARED
or TYPE=REMOTE
in a CICS TST.
For each new DB2 unit of recovery, the appropriate plan selection exit routine is automatically invoked. This exit routine reads the temporary storage record and uses the contained plan name for plan selection.
When no temporary storage record exists for the Natural session, a default plan name, contained in the plan exit, can be used. If no plan name is specified by the exit, the name of the plan used is the same as the name of the static program (DBRM) issuing the SQL call. If no such plan name exists, an SQL error results.
Anmerkung:
This is an extract of
Syntax 1
- Extended Set and
Syntax
Element Description in the section
SELECT
(SQL) (Statements documentation) and
only describes the changes specific to Natural for DB2 Version 8.4.
[WITH_CTE
common-table-expression,
]
|
||||||||||||
SELECT selection
into-clause
table-expression
|
||||||||||||
|
|
|
||||||||||
[ORDER
BY criteria] |
||||||||||||
OPTIMIZE
FOR integer |
|
|||||||||||
[WITH
isolation-level] |
||||||||||||
[SKIP LOCKED
DATA] |
||||||||||||
[QUERYNO integer]
|
||||||||||||
[OFFSET
row-count] |
||||||||||||
[FETCH
FIRST row-limit] |
||||||||||||
[WITH
HOLD]
|
||||||||||||
[WITH
RETURN]
|
||||||||||||
[WITH
scroll-mode] |
||||||||||||
[WITH ROWSET POSITIONING FOR
max-rowsets] |
||||||||||||
[IF NO
RECORDS FOUND instruction] |
||||||||||||
statement | ||||||||||||
|
- OFFSET row-count
OFFSET
[offset-row-count]
ROW
ROWS
The
OFFSET
clause specifies the number of rows to skip in the result table before retrieving any rows from there. A limited number of rows at the end of a result set can improve the performance of queries with potentially large result sets.
offset-row-count
is a numeric variable or constant which determines the number of rows to be skipped. The number must be zero (0
) or a positive integer.- FETCH FIRST row-limit
FETCH FIRST
1
row-count
ROW
ROWS
The
FETCH FIRST
clause limits the number of rows to be fetched. A limited number of rows can improve the performance of queries with potentially large result sets.
row-count
specifies a numeric variable or constant which determines the number of rows to be fetched. The number must be zero (0
) or a positive integer.
Anmerkung:
This is an extract of the chapter MERGE ( SQL)
(Statements documentation) and only describes the changes
specific to Natural for DB2 Version 8.4.
Syntax Element | Description |
---|---|
MERGE
INTO |
MERGE INTO Clause:
|
table-name |
Table Name:
Identifies the target of the |
[AS]
correlation-name
|
[AS] correlation-name
Clause:
Specifies an alternate name for the target table. The alternate name can be used as qualifier when referencing columns of the intermediate result table. |
include-columns |
Include Columns Clause:
Specifies a set of columns that are included, along with the
columns of the target table, in the result table of the |
USING
source-table |
USING source-table
Clause:
Specifies the values for the row data to merge into the target table. See source-table. |
ON
search-condition |
ON search-condition
Clause:
Specifies join conditions between the
|
WHEN
matching-condition |
WHEN matching-condition
Clause:
Specifies the condition for which to perform the modification
operation defined in the following |
THEN
modification-operation |
THEN modification-operation
Clause:
Specifies the operation to perform on the matches of the condition
defined in the preceding |
ELSE
IGNORE |
Specifies that no action is taken on source
columns that do not match the condition specified in the WHEN
clause.
|
NOT
ATOMIC CONTINUE ON SQLEXCEPTION |
NOT ATOMIC CONTINUE ON SQLEXCEPTION
Clause:
Specifies whether merge processing continues in case an error occurred during processing one row of a set of source rows. |
QUERYNO
integer |
QUERYNO integer
Clause:
Specifies the number for this SQL statement that is used in
|
table-reference | ||||
(VALUES |
) | |||
[AS ]
correlation-name (column-name,...)
|
Syntax Element | Description |
---|---|
table-reference |
Specifies the source table to merge into the target table. |
VALUES |
VALUES introduces the specification
of values for the row data to merge into the target table.
|
values-single-row |
Specifies a single row of source data. |
values-multiple-row |
Specifies multiple rows of source data. |
[AS]
correlation-name |
Specifies a correlation name for the source table. |
column-name |
Specifies a column name to associate the input
data to the UPDATE SET assignment clause for an
UPDATE operation or the VALUES clause for an
INSERT operation.
|
[NOT ]
MATCHED [AND
search-condition]
|
Syntax Element | Description |
---|---|
[NOT ] MATCHED |
Specifies the
modification-operation
to perform when an ON
search-condition evaluates to true
or not true (NOT can be specified optionally).
|
[AND
search-condition ]
|
Specifies an (optional) additional condition to
evaluate to true before the
modification-operation
performs.
|
update-operation |
Syntax Element | Description |
---|---|
update-operation |
Specifies that the matching target row is
updated with the values assigned in the UPDATE SET assignment
clause.
An |
DELETE |
Specifies that the matching target row is
deleted.
A |
signal-operation |
Specifies the SQL error to raise.
A |
insert-operation |
Specifies the rows to insert into the target
table.
An |
SIGNAL SQLSTATE
[VALUE ] sqlstate [SET MESSAGE_TEXT =
scalar-expression]
|
Syntax Element | Description |
---|---|
SIGNAL |
Specifies the SIGNAL operation to
perform when the matching-condition
evaluates to true.
DB2 sets an SQLCODE -438 if an error is raised by the
|
SQLSTATE [VALUE]
sqlstate |
Specifies the SQLSTATE to be set by DB2.
|
[SET MESSAGE_TEXT =
scalar-expression] |
This optional clause specifies an error or
warning message which is placed into the SQLEERRMC field of the SQLCA or which
can be retrieved with the GET DIAGNOSTICS statement.
|
Merge sales data from the MSALES
table into the
MPRODUCT
table. Demonstrate the MERGE
operation with
DELETE
, UPDATE
, INSERT
and
SIGNAL
statements.
DEFINE DATA LOCAL USING DEMSQLCA LOCAL 1 V1 VIEW OF MPRODUCT 2 ID 2 NAME 2 INVENTORY 1 #M_TEXT (A10) INIT <'Oversold: '> END-DEFINE ... MERGE INTO MPRODUCT AS T USING (SELECT MSALES.ID ,SUM(MSALES.SOLD) AS SOLD, MAX(MCATALOG.NAME) AS NAME FROM MSALES, MCATALOG WHERE MSALES.ID = MCATALOG.ID GROUP BY MSALES.ID) AS S (ID,SOLD,NAME) ON S.ID = T.ID WHEN MATCHED AND T.INVENTORY = S.SOLD THEN DELETE WHEN MATCHED AND T.INVENTORY < S.SOLD THEN SIGNAL SQLSTATE '78000' SET MESSAGE_TEXT =:#M_TEXT || S.NAME WHEN MATCHED THEN UPDATE SET T.INVENTORY = T.INVENTORY - S.SOLD WHEN NOT MATCHED THEN INSERT VALUES(S.ID, S.NAME, -S.SOLD) END TRANSACTION END
Anmerkung:
This is an extract of
Syntax 1 -
Searched DELETE in the chapter
DELETE
(SQL) (Statements documentation) and
only describes the changes specific to Natural for DB2 Version 8.4.
DELETE FROM
table-name
[period-clause]
[correlation-name]
|
|||||||
[include-columns [SET
assignment-clause]]
|
|||||||
[WHERE
search-condition]
|
|||||||
[FETCH FIRST row-limit |
|||||||
WITH
|
|
[SKIP LOCKED
DATA ] [QUERYNO
integer]
|
The FETCH FIRST
clause limits the effects of the
DELETE
statement to a subset of qualifying rows. It corresponds to
the FETCH FIRST
clause of the SELECT
statement
described in FETCH FIRST
row-limit.
Anmerkung:
This is an extract of the chapter
Search
Conditions (Statements
documentation) and only describes the changes specific to Natural for DB2
Version 8.4.
scalar-expression | comparison | scalar-expression | ||||
row-value-expression | comparison | row-value-expression |
A comparison predicate compares two values or a set of values with another set of values.
In the syntax diagram above,
comparison
can be one of the following operators:
=
|
equal to |
<
|
less than |
>
|
greater than |
<=
|
less than or equal to |
>=
|
greater than or equal to |
<>
|
not equal to |