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.

Read syntax diagramSkip visual syntax diagram PARSE_STATEMENT ( SQL_STATEMENT =>  SQL-statement,NAMING => naming,DECIMAL_POINT => decimal-point,SQL_STRING_DELIMITER => SQL-string-delimiter)
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:
  • Names used in data change statements and in any query construct are returned.
  • For CALL, the procedure being called is returned.
  • Start of changeFor 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.
    Any statement that does not contain these constructs, a query, or an expression returns no rows.End of change
  • 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.
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.
Table 1. PARSE_STATEMENT table function
Column Name Data Type Description
NAME_TYPE VARCHAR(30) Type of object name.
ALIAS
This is an alias name.
COLUMN
This is a column name, or a global variable name not returned as a DDL TARGET OBJECT.
CONSTRAINT
This is a constraint name.
FUNCTION
This is a function name.
INDEX
This is an index name.
MASK
This is a mask name.
PACKAGE
This is a package name.
Start of changePARAMETEREnd of change
Start of changeThis is a parameter name for a COMMENT statement.End of change
PERM
This is a permission name.
PROC
This is a procedure name.
Start of changePROGRAMEnd of change
Start of changeThis is the program name for a CREATE FUNCTION, CREATE PROCEDURE, or CREATE TRIGGER statement.End of change
Start of changeRETURN COLUMNEnd of change
Start of changeThis is a return column for a COMMENT statement.End of change
ROUTINE
This is a routine name for a DDL operation.
SCHEMA
This is a schema name.
SEQUENCE
This is a sequence name.
SPECIFIC
This is a routine specific name for a DDL operation.
TABLE
This is a table, view, or alias name.
TRIGGER
This is a trigger name.
TYPE
This is a user-defined type name.
VARIABLE
This is a variable name for a DDL operation.
VIEW
This is a view name for a DDL operation.
XSROBJ
This is an XSROBJECT 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.
  • Name of a member for an alias
  • Name of a parameter
  • Name of a table function return column
  • System object name when FOR SYSTEM NAME provided in DDL
  • System column name when two column names provided in DDL

Contains null if there is no additional name.

USAGE_TYPE VARCHAR(17) How this name is used in the statement.
Start of changeCHECK CONSTRAINTEnd of change
Start of changeColumn is used in a check constraint.End of change
DDL SOURCE OBJECT
Name identifies the table an index or trigger is being created on, the table referenced by CREATE TABLE LIKE, Start of changethe table referenced by an alias, or the object being renamedEnd of change.
DDL TARGET OBJECT
Name is the primary object of a DDL statement Start of changeor the new name for a renamed objectEnd of change.
EXPRESSION
Name is referenced in an index key expression.
Start of changeFOREIGN KEYEnd of change
Start of changeColumn is used in a foreign key constraint.End of change
Start of changeHISTORY TABLEEnd of change
Start of changeTable is the history table for an ALTER TABLE statement with ADD VERSIONING.End of change
PARAMETER DEFAULT
Name is referenced in a parameter default expression.
Start of changePRIMARY KEYEnd of change
Start of changeColumn is used in a primary key constraint.End of change
QUERY
Name is referenced as part of a query construct.
REFERENCES
Name is part of a foreign key constraint referencing another table.
TARGET PROCEDURE
This is the procedure that is the target of a CALL statement.
TARGET TABLE
This is the table that will be affected for an insert, update, delete, merge, truncate, lock table, or refresh table statement. The value is also returned for any explicitly specified columns from the target table for insert, update, and merge.
Start of changeUNIQUE KEYEnd of change
Start of changeColumn is used in a unique key constraint.End of change
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.
  • ALTER FUNCTION
  • Start of changeALTER MASKEnd of change
  • Start of changeALTER PERMISSIONEnd of change
  • ALTER PROCEDURE
  • Start of changeALTER SEQUENCEEnd of change
  • ALTER TABLE
  • Start of changeALTER TRIGGEREnd of change
  • Start of changeASSOCIATE LOCATOREnd of change
  • CALL
  • Start of changeCOMMENT ALIASEnd of change
  • Start of changeCOMMENT COLUMNEnd of change
  • Start of changeCOMMENT CONSTRAINTEnd of change
  • Start of changeCOMMENT FUNCTIONEnd of change
  • Start of changeCOMMENT INDEXEnd of change
  • Start of changeCOMMENT MASKEnd of change
  • Start of changeCOMMENT PACKAGEEnd of change
  • Start of changeCOMMENT PARAMETEREnd of change
  • Start of changeCOMMENT PERMISSIONEnd of change
  • Start of changeCOMMENT PROCEDUREEnd of change
  • Start of changeCOMMENT RETURN COLUMNEnd of change
  • Start of changeCOMMENT ROUTINEEnd of change
  • Start of changeCOMMENT SEQUENCEEnd of change
  • Start of changeCOMMENT TABLEEnd of change
  • Start of changeCOMMENT TRIGGEREnd of change
  • Start of changeCOMMENT TYPEEnd of change
  • Start of changeCOMMENT VARIABLEEnd of change
  • Start of changeCOMMENT XSROBJECTEnd of change
SQL_STATEMENT_TYPE (continued)  
  • Start of changeCREATE ALIASEnd of change
  • CREATE FUNCTION
  • CREATE INDEX
  • CREATE MASK
  • CREATE PERMISSION
  • CREATE PROCEDURE
  • Start of changeCREATE SCHEMAEnd of change
  • Start of changeCREATE SEQUENCEEnd of change
  • CREATE TABLE
  • CREATE TRIGGER
  • Start of changeCREATE TYPEEnd of change
  • CREATE VARIABLE
  • CREATE VIEW
  • DECLARE CURSOR
  • DECLARE GLOBAL TEMPORARY TABLE
  • DELETE
  • Start of changeDESCRIBE PROCEDUREEnd of change
  • DROP ALIAS
  • DROP FUNCTION
  • DROP INDEX
  • DROP MASK
  • DROP PACKAGE
  • DROP PERMISSION
  • DROP PROCEDURE
  • DROP ROUTINE
  • DROP SCHEMA
  • DROP SEQUENCE
  • DROP TABLE
  • DROP TRIGGER
  • DROP TYPE
  • DROP VARIABLE
  • DROP VIEW
  • DROP XSROBJECT
  • EXECUTE IMMEDIATE
  • Start of changeGRANT FUNCTIONEnd of change
  • Start of changeGRANT PACKAGEEnd of change
  • Start of changeGRANT PROCEDUREEnd of change
  • Start of changeGRANT ROUTINEEnd of change
  • Start of changeGRANT SCHEMAEnd of change
  • Start of changeGRANT SEQUENCEEnd of change
  • Start of changeGRANT TABLEEnd of change
  • Start of changeGRANT TYPEEnd of change
  • Start of changeGRANT VARIABLEEnd of change
  • Start of changeGRANT XSROBJECTEnd of change
  • INSERT
SQL_STATEMENT_TYPE (continued)  
  • Start of changeLABEL ALIASEnd of change
  • Start of changeLABEL COLUMNEnd of change
  • Start of changeLABEL CONSTRAINTEnd of change
  • Start of changeLABEL FUNCTIONEnd of change
  • Start of changeLABEL INDEXEnd of change
  • Start of changeLABEL MASKEnd of change
  • Start of changeLABEL PACKAGEEnd of change
  • Start of changeLABEL PERMISSIONEnd of change
  • Start of changeLABEL PROCEDUREEnd of change
  • Start of changeLABEL ROUTINEEnd of change
  • Start of changeLABEL SCHEMAEnd of change
  • Start of changeLABEL SEQUENCEEnd of change
  • Start of changeLABEL TABLEEnd of change
  • Start of changeLABEL TRIGGEREnd of change
  • Start of changeLABEL TYPEEnd of change
  • Start of changeLABEL VARIABLEEnd of change
  • Start of changeLABEL XSROBJECTEnd of change
  • Start of changeLOCK TABLEEnd of change
  • MERGE
  • PREPARE
  • QUERY
  • Start of changeREFRESH TABLEEnd of change
  • Start of changeRENAME INDEXEnd of change
  • Start of changeRENAME TABLEEnd of change
  • Start of changeREVOKE FUNCTIONEnd of change
  • Start of changeREVOKE PACKAGEEnd of change
  • Start of changeREVOKE PROCEDUREEnd of change
  • Start of changeREVOKE ROUTINEEnd of change
  • Start of changeREVOKE SCHEMAEnd of change
  • Start of changeREVOKE SEQUENCEEnd of change
  • Start of changeREVOKE TABLEEnd of change
  • Start of changeREVOKE TYPEEnd of change
  • Start of changeREVOKE VARIABLEEnd of change
  • Start of changeREVOKE XSROBJECTEnd of change
  • SET
  • SET CURRENT TEMPORAL SYSTEM_TIME
  • Start of changeTRANSFER OWNERSHIPEnd of change
  • TRUNCATE
  • UPDATE
  • VALUES INTO

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;