PARSE_STATEMENT table function
The PARSE_STATEMENT table function returns a list of object and column names that are used in an SQL query, data change statement, or other statement where a query or expression is specified.
Authorization: None required.
The schema is QSYS2.
- SQL-statement
- A character or graphic string expression that contains a valid SQL statement. The maximum string length is 2 megabytes.
- naming
- A character or graphic string expression that defines the naming
rule for the statement.
- *SYS
- System naming rules apply. This is the default.
- *SQL
- SQL naming rules apply.
- decimal-point
- A character or graphic string expression that defines the decimal
point for numeric constants in SQL-statement.
- *PERIOD or .
- The decimal point is the period. This is the default.
- *COMMA or ,
- The decimal point is the comma.
- SQL-string-delimiter
- A character or graphic string expression that defines the string
delimiter for strings in SQL-statement. Delimited identifiers
in the SQL statement will use the opposite character.
- *APOSTSQL or '
- The apostrophe character (') is used to delimit strings. This is the default.
- *QUOTESQL or "
- The quote character (") is used to delimit strings.
When the SQL statement is parsed, object names are identified and a result row is
returned for every name. This is done at the SQL parser level where names are identified strictly by
where they appear in the syntax. The following rules apply:
The result of the function is a table containing a row for each name reference with the
format shown in the following table. All the columns are null capable.- Names used in data change statements and in any query construct are returned.
- For CALL, the procedure being called is returned.
For DDL statements, most items are returned. Notable exceptions are:
- RCDFMT name for CREATE TABLE, CREATE VIEW, and CREATE INDEX.
- FIELDPROC program name for CREATE TABLE and ALTER TABLE.
- Columns used as partitioning keys for CREATE TABLE and ALTER TABLE.
- Parameter names for CREATE FUNCTION and CREATE PROCEDURE.
- Return column names for CREATE FUNCTION (Table).
- External program name for functions and procedures.
- Names in a routine-body, triggered-action, and trigger-body are not returned. To see these references, use QSYS2.SYSPROGRAMSTMTSTAT to find all the statements for the generated program or service program and pass each of them as an argument to this table function.
- If the SQL statement is the null value, an empty string, a string of all blanks, or contains a syntax error, no row is returned.
Column Name | Data Type | Description |
---|---|---|
NAME_TYPE | VARCHAR(30) | Type of object name.
|
NAME | VARCHAR(128) | The object name. Contains null if NAME_TYPE is COLUMN without a table qualifier. |
SCHEMA | VARCHAR(128) | The schema name. Contains null if NAME is not qualified with a schema name. |
RDB | VARCHAR(128) | The relational database name. Contains null if NAME is not qualified with a relational database name. |
COLUMN_NAME | VARCHAR(128) | The column name. Contains null if NAME_TYPE is not COLUMN. |
ADDITIONAL_NAME | VARCHAR(128) | Contains an additional name for some name types.
Contains null if there is no additional name. |
USAGE_TYPE | VARCHAR(17) | How this name is used in the statement.
|
NAME_START_POSITION | INTEGER | Position within the SQL-statement string that this name begins. For qualified TABLE names, this is the position where the RDB or schema name begins. For all other name types, this is the position of the name. |
SQL_STATEMENT_TYPE | VARCHAR(32) | Type of SQL statement.
|
SQL_STATEMENT_TYPE (continued) |
|
|
SQL_STATEMENT_TYPE (continued) |
|
Example
For every program and service program in library APPLIB, find all the references to table
names in static SQL
statements.
WITH program_statements(naming_mode, dec_point, string_delim, stmt_text,
system_program_name, program_type)
AS (SELECT a.naming, a.decimal_point, a.sql_string_delimiter, b.statement_text,
a.system_program_name, a.program_type
FROM qsys2.sysprogramstat a INNER JOIN
qsys2.sysprogramstmtstat b ON a.program_schema = b.program_schema AND
a.program_name = b.program_name AND
a.module_name = b.module_name
WHERE a.number_statements > 0 AND
a.program_schema = 'APPLIB' AND b.program_schema = 'APPLIB')
SELECT system_program_name, program_type, c.schema, c.name, stmt_text
FROM program_statements,
TABLE(qsys2.parse_statement(stmt_text, naming_mode, dec_point, string_delim)) c
WHERE c.name_type = 'TABLE'
ORDER BY c.schema, c.name;