Command reference / db
RUNSQL
Run SQL
The Run SQL (RUNSQL) command will run the Structured Query Language (SQL) statement specified for the SQL (SQL) parameter. The SQL statement will be parsed when the command is run. Syntax errors will not be identified until the command is run. RUNSQL will run SQL statements in the caller's activation group. If RUNSQL is run in a compiled CL program or ILE CL procedure, the activation group of the program or procedure is used.
Allowed environments: *ALL.
Positional parameters: 3.
Restrictions
- In other SQL interfaces, an SQL statement is limited to 2 megabytes in length. The limit on this command is 5000 bytes, which is the maximum for a CL command *CHAR parameter.
- Unlike the RUNSQLSTM (Run SQL Statements) command, the RUNSQL command does not produce a spooled file. When an error occurs during command execution, the corresponding SQL failure message is sent as an escape message to the caller. For a complex SQL statement that returns a syntax error, the easiest way to find the cause of the syntax error is to start a database monitor, run the RUNSQL command, and then analyze the database monitor using System i Navigator. This is especially true when the SQL statement is built as an expression in a control language (CL) program or procedure.
- Unlike the RUNSQLSTM command which implicitly commits or rolls back the statements when it ends and commitment control is used, the RUNSQL command will not implicitly perform a commit or rollback; that is left up to the user's application. This is similar to how embedded SQL statements work in other programming languages, such as RPG or COBOL.
- The RUNSQL command cannot be used to open a database file and leave it open. Any file opened by RUNSQL is closed before control is returned.
Parameters
SQL
SQL statement
Specifies a single Structured Query Language (SQL) statement to be run. This is a required parameter.
character-value- Specify the SQL statement to be run. The maximum statement length is 5000 bytes.
COMMIT
Commitment control
Specifies whether SQL statements are run under commitment control.
Allowed values:
*CHG, *UR, *CS, *ALL, *RS, *NONE, *NC, *RR
*CHG or *UR- Specifies the objects referred to in SQL ALTER, CALL, COMMENT ON, CREATE, DROP, GRANT, LABEL ON, RENAME, and REVOKE statements and the rows updated, deleted, and inserted are locked until the end of the unit of work (transaction). Uncommitted changes in other jobs can be seen.
*CS- Specifies the objects referred to in SQL ALTER, CALL, COMMENT ON, CREATE, DROP, GRANT, LABEL ON, RENAME, and REVOKE statements and the rows updated, deleted, and inserted are locked until the end of the unit of work (transaction). A row that is selected, but not updated, is locked until the next row is selected. Uncommitted changes in other jobs cannot be seen.
*ALL or *RS- Specifies the objects referred to in SQL ALTER, CALL, COMMENT ON, CREATE, DROP, GRANT, LABEL ON, RENAME, and REVOKE statements and the rows selected, updated, deleted, and inserted are locked until the end of the unit of work (transaction). Uncommitted changes in other jobs cannot be seen.
*NONE or *NC- Specifies that commitment control is not used. Uncommitted changes in other jobs can be seen. If the SQL DROP SCHEMA statement is included in the program, *NONE or *NC must be used.
*RR- Specifies the objects referred to in SQL ALTER, CALL, COMMENT ON, CREATE, DROP, GRANT, LABEL ON, RENAME, and REVOKE statements and the rows selected, updated, deleted, and inserted are locked until the end of the unit of work (transaction). Uncommitted changes in other jobs cannot be seen. All tables referred to in SELECT, UPDATE, DELETE, and INSERT statements are locked exclusively until the end of the unit of work (transaction).
NAMING
Naming
Specifies the naming convention used for objects in SQL statements.
Allowed values:
*SYS, *SQL
*SYS- The system naming convention (library-name/file-name) is used.
*SQL- The SQL naming convention (schema-name.table-name) is used.
DATFMT
Date format
Specifies the format used when accessing date result columns. For input date strings, the specified value is used to determine whether the date is specified in a valid format. Note: An input date string that uses the format *USA, *ISO, *EUR, or *JIS is always valid.
Allowed values:
*JOB, *USA, *ISO, *EUR, *JIS, *MDY, *DMY, *YMD, *JUL
*JOB- The format specified for the job is used. Use the Display Job (DSPJOB) command to determine the current date format for the job.
*USA- The United States date format mm/dd/yyyy is used.
*ISO- The International Organization for Standardization (ISO) date format yyyy-mm-dd is used.
*EUR- The European date format dd.mm.yyyy is used.
*JIS- The Japanese Industrial Standard date format yyyy-mm-dd is used.
*MDY- The date format mm/dd/yy is used.
*DMY- The date format dd/mm/yy is used.
*YMD- The date format yy/mm/dd is used.
*JUL- The Julian date format yy/ddd is used.
DATSEP
Date separator
Specifies the separator used when accessing date result columns. Note: This parameter applies only when *JOB, *MDY, *DMY, *YMD, or *JUL is specified for the Date format (DATFMT) parameter.
Suggested / special values:
/, ., ,, -, , *JOB, *BLANK
*JOB- The date separator specified for the job at precompile time, when a new interactive SQL session is created, or when RUNSQLSTM is run is used. Use the Display Job (DSPJOB) command to determine the current date separator value for the job.
'/'- A slash is used as the date separator.
'.'- A period is used as the date separator.
'-'- A dash is used as the date separator.
','- A comma is used as the date separator.
' ' or *BLANK- A blank is used as the date separator.
TIMFMT
Time format
Specifies the format used when accessing time result columns. For input time strings, the specified value is used to determine whether the time is specified in a valid format. Note: An input time string that uses the format *USA, *ISO, *EUR, or *JIS is always valid.
Allowed values:
*HMS, *USA, *ISO, *EUR, *JIS
*HMS- The hh:mm:ss format is used.
*USA- The United States time format hh:mmxx is used, where xx is AM or PM.
*ISO- The International Organization for Standardization (ISO) time format hh.mm.ss is used.
*EUR- The European time format hh.mm.ss is used.
*JIS- The Japanese Industrial Standard time format hh:mm:ss is used.
TIMSEP
Time separator
Specifies the separator used when accessing time result columns. Note: This parameter applies only when *HMS is specified for the Time format (TIMFMT) parameter.
Suggested / special values:
:, ., ,, , *JOB, *BLANK
*JOB- The time separator specified for the job at precompile time, when a new interactive SQL session is created, or when RUNSQLSTM is run is used. Use the Display Job (DSPJOB) command to determine the current time separator value for the job.
':'- A colon is used as the time separator.
'.'- A period is used as the time separator.
','- A comma is used as the time separator.
' ' or *BLANK- A blank is used as the time separator.
DFTRDBCOL
Default collection
Specifies the name of the schema identifier used for the unqualified names of the tables, views, indexes, SQL packages, aliases, constraints, external programs, node groups, and triggers. This parameter applies only to static SQL statements.
Suggested / special values:
*NONE
*NONE- The naming convention specified for the Naming convention (NAMING) parameter is used.
name- Specify the name of the schema identifier to be used instead of the naming convention specified for the NAMING parameter.
DECMPT
Decimal point
Specifies the decimal point value used for numeric constants in SQL statements. This value is also used as the decimal point character when casting between character and numeric values.
Allowed values:
*JOB, *SYSVAL, *PERIOD, *COMMA
*JOB- The representation for the decimal point is the value used by the job running the statement.
*SYSVAL- The QDECFMT system value is used as the decimal point.
*PERIOD- A period represents the decimal point.
*COMMA- A comma represents the decimal point.
LANGID
Language id
Specifies the language identifier to be used when SRTSEQ(*LANGIDUNQ) or SRTSEQ(*LANGIDSHR) is specified.
Suggested / special values:
*JOB
*JOB- The LANGID value for the job is retrieved.
language-identifier- Specify a language identifier.
OPTION
Source listing options
Specifies whether a listing is generated by the command.
Allowed values:
*NOLIST, *LIST
*NOLIST- No listing is generated. Any messages will be sent to the joblog.
*LIST- A listing is generated.
PRTFILE
Print file
Specifies the printer device file to which the printout from the command is directed. The file must have a minimum length of 132 bytes. If a file with a record length of less than 132 bytes is specified, information is lost.
QSYSPRT- The output file is directed to the IBM-supplied printer file, QSYSPRT.
Qualifier 1: Print file: name- Specify the name of the printer device file to which the output is directed.
Qualifier 2: Library: name- Specify the name of the library where the printer file is located.
*LIBL- All libraries in the library list for the current thread are searched until the first match is found.
*CURLIB- The current library for the job is searched. If no library is specified as the current library for the job, the QGPL library is used.
Qualified parts (object, then library; write LIBRARY/OBJECT)
Print file
Print file
Library
Library
Suggested / special values:
*LIBL, *CURLIB
SECLVLTXT
Second level text
Specifies whether the second-level message text descriptions should be written to the output listing.
Allowed values:
*NO, *YES
*NO- Second-level text is not included in the listing.
*YES- Second-level text with replacement data is added to the listing for all messages.
ALWCPYDTA
Allow copy of data
Specifies whether a copy of the data can be used in a SELECT statement.
Allowed values:
*OPTIMIZE, *YES, *NO
*OPTIMIZE- The system determines whether to use the data retrieved directly from the database or to use a copy of the data. The decision is based on which method provides the best performance. If the Commitment control (COMMIT) parameter is not *NONE, the Allow blocking (ALWBLK) parameter should be set to *ALLREAD, when possible, for best performance.
*YES- A copy of the data is used only when necessary.
*NO- A copy of the data is not used. If a temporary copy of the data is required to perform the query, an error message is returned.
ALWBLK
Allow blocking
Specifies whether the database manager can use record blocking and the extent to which blocking can be used for read-only cursors.
Allowed values:
*ALLREAD, *NONE, *READ
*ALLREAD- Rows are blocked for read-only cursors. All cursors in a program that are not explicitly able to be changed are opened for read-only processing even though there may be EXECUTE or EXECUTE IMMEDIATE statements in the program. Specifying *ALLREAD: Allows record blocking for all read-only cursors. Can improve the performance of almost all read-only cursors in programs, but limits queries in the following ways: The Rollback (ROLLBACK) command, a ROLLBACK statement in host languages, or the ROLLBACK HOLD SQL statement does not reposition a read-only cursor when *ALLREAD is specified. Dynamic running of a positioned UPDATE or DELETE statement (for example, using EXECUTE IMMEDIATE), can not be used to update a row in a cursor unless the DECLARE statement for the cursor includes the FOR UPDATE clause.
*NONE- Rows are not blocked for retrieval of data for cursors. Specifying *NONE: Guarantees that the data retrieved is current. May reduce the amount of time required to retrieve the first row of data for a query. Stops the database manager from retrieving a block of data rows that is not used by the program when only the first few rows of a query are retrieved before the query is closed. Can degrade the overall performance of a query that retrieves a large number of rows.
*READ- Records are blocked for read-only retrieval of data for cursors when: *NONE is specified for the Commitment control (COMMIT) parameter, which indicates that commitment control is not used. The cursor is declared with a FOR READ ONLY clause or there are no dynamic statements that could run a positioned UPDATE or DELETE statement for the cursor.
SQLCURRULE
SQL rules
Specifies the semantics used for SQL statements.
Allowed values:
*DB2, *STD
*DB2- The semantics of all SQL statements will default to the rules established for DB2. The following semantics are controlled by this option: Hexadecimal constants are treated as character data.
*STD- The semantics of all SQL statements will default to the rules established by the ISO and ANSI SQL standards. The following semantics are controlled by this option: Hexadecimal constants are treated as binary data.
CONACC
Concurrent access resolution
Specifies how the database manager should handle record lock conflicts for data in the process of being updated.
Allowed values:
*DFT, *CURCMT, *WAIT
*DFT- Specifies that the concurrent access option will not be explicitly set for this program. The value that is in effect when the program is invoked will be used. The value can be set using the SQL_CONCURRENT_ACCESS_RESOLUTION option in the query options file QAQQINI.
*CURCMT- Directs the database manager to use the currently committed data when possible in the case of record lock conflicts for read-only queries. This only applies when the commit level is *CS.
*WAIT- Directs the database manager to wait for the outcome in the case of record lock conflicts.
SYSTIME
System time sensitive
Specifies whether references to system-period temporal tables in both static and dynamic SQL statements are affected by the value of the CURRENT TEMPORAL SYSTEM_TIME special register.
Allowed values:
*YES, *NO
*YES- References to system-period temporal tables are affected by the value of the CURRENT TEMPORAL SYSTEM_TIME special register.
*NO- References to system-period temporal tables are not affected by the value of the CURRENT TEMPORAL SYSTEM_TIME special register.
Examples
RUNSQL SQL('insert into testlib/t1 values (1)')
Insert a Row into a Table. This command will insert one row into file T1 in library TESTLIB.
RUNSQL SQL('CREATE TABLE qtemp.t1 AS (SELECT * FROM qsys2.systables WHERE table_schema = ''TESTLIB'') WITH DATA') COMMIT(*NONE) NAMING(*SQL)
Run a Query and Store the Results in a Temporary Table. This command runs a query and stores the results in a temporary table. The table would be file T1 in library QTEMP. If the command is run in a CL program or procedure, you can use the Receive File (RCVF) command to read the results of the query.
RUNSQL1: PGM PARM(&LIB) DCL &LIB TYPE(*CHAR) LEN(10) DCL &SQLSTMT TYPE(*CHAR) LEN(1000) CHGVAR VAR(&SQLSTMT) + VALUE('DECLARE GLOBAL TEMPORARY TABLE result + AS (SELECT * FROM qsys2.systables WHERE + table_schema = ''' || &LIB || ''') with + data WITH REPLACE NOT LOGGED') RUNSQL SQL(&SQLSTMT) COMMIT(*NONE) NAMING(*SQL) ENDSQL1: ENDPGM
Using an Expression for the SQL Statement. This example shows how the RUNSQL command can be used in a CL program or procedure. The SQL parameter is built as a CL character expression concatenating literal text with the value of CL character variable &LIB.
Generated from os/cl/catalog/RUNSQL.json and os/cl/help/RUNSQL.json.