SQL control statements for SQL routines

SQL control statements for SQL routines can be used in SQL functions and native SQL procedures. 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:
Read syntax diagram
>>-+-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-----------'   

Start of changeControl statements are supported in SQL functions and SQL procedures.End of change

Start of change
  • SQL functions are created by specifying LANGUAGE SQL and an SQL routine body in a CREATE FUNCTION (compiled SQL) statement. An SQL function can be changed. A new SQL routine body can be specified in an ALTER FUNCTION (compiled SQL) statement.
  • SQL procedures are created by specifying LANGUAGE SQL and an SQL routine body in a CREATE PROCEDURE (native SQL) statement. An SQL procedure can be changed. A new SQL routine body can be specified in an ALTER PROCEDURE (native SQL) statement.
End of change

Start of changeThe SQL routine body is the executable part of the function or procedure and is transformed by DB2® into a program.End of change

Start of changeThe remainder of the topics about SQL control statements for SQL routines contain information about references to SQL parameters and variables, SQL condition names, SQL cursor names, labels, and reference information for the use of the statements that constitute the SQL routine body.End of change

The two common elements that are used in describing specific SQL control statements are: