Control statements are SQL statements that allow SQL to be used in a manner similar to writing a program in a structured programming language. SQL control statements provide the capability to control the logic flow, declare and set variables, and handle warnings and exceptions. Some SQL control statements include other nested SQL statements.
SQL-control-statement: |--+-assignment-statement------+--------------------------------| +-CALL-statement------------+ +-CASE-statement------------+ +-compound-statement--------+ +-FOR-statement-------------+ +-GET DIAGNOSTICS-statement-+ +-GOTO-statement------------+ +-IF-statement--------------+ +-ITERATE-statement---------+ +-LEAVE-statement-----------+ +-LOOP-statement------------+ +-REPEAT-statement----------+ +-RESIGNAL-statement--------+ +-RETURN-statement----------+ +-SIGNAL-statement----------+ '-WHILE-statement-----------'
Control statements are supported in SQL procedures, SQL functions, and SQL triggers.
SQL procedures are created by specifying LANGUAGE SQL and an SQL routine body on the CREATE PROCEDURE statement. SQL functions are created by specifying LANGUAGE SQL and an SQL routine body on the CREATE FUNCTION statement. SQL routines are SQL procedures or SQL functions. SQL triggers are created by specifying an SQL routine body on the CREATE TRIGGER statement.
An SQL routine body must be a single SQL statement which may be an SQL control statement.
The SQL routine body is the executable part of the procedure, function, or trigger that is transformed by the database manager into a program or service program. When an SQL routine or trigger is created, SQL creates a temporary source file (QTEMP/QSQLSRC and QTEMP/QSQLT00000) that will contain C source code with embedded SQL statements. If either of these source files exist, they will be modified if needed to have the same CCSID as the source. If DBGVIEW(*SOURCE) is specified, SQL creates the root source for the routine or trigger in source file QSQDSRC in the same library as the procedure, function or trigger.
An SQL procedure or SQL trigger is created as a program (*PGM) object using the CRTPGM command. An SQL function is created as a service program (*SRVPGM) object using the CRTSRVPGM command. The program or service program is created in the library that is the implicit or explicit qualifier of the procedure, function, or trigger name.
When the program or service program is created, the SQL statements other than certain control statements become embedded SQL statements in the program or service program. The CALL, SIGNAL, RESIGNAL, and GET DIAGNOSTIC control statements also become embedded SQL statements in the program or service program.
The specified procedure or function is registered in the SYSROUTINES and SYSPARMS catalog tables, and an internal link is created from SYSROUTINES to the program. When the procedure is called using the SQL CALL statement or when the function is invoked in an SQL statement, the program associated with the routine is called. The specified SQL trigger is registered in the SYSTRIGGER catalog table.
The remainder of this chapter contains a description of the control statements including syntax diagrams, semantic descriptions, usage notes, and examples of the use of the statements that constitute the SQL routine body. There is also a section on referencing SQL parameters and variables found in References to SQL parameters and SQL variables. There are two common elements that are used in describing specific SQL control statements. These are:
For syntax and additional information on the SQL control statements see the following topics: