Collection Contents Index EXECUTE statement [T-SQL] EXIT statement [ISQL] pdf/chap9.pdf

Reference Manual
   CHAPTER 9. SQL Statements     

EXECUTE IMMEDIATE statement [ESQL] [SP]


Function 

To enable dynamically constructed statements to be executed from within a procedure.

Syntax 1 

EXECUTE IMMEDIATE string-expression

Syntax 2 

EXECUTE ( string-expression )

Permissions 

None. The statement is executed with the permissions of the owner of the procedure, not with the permissions of the user who calls the procedure.

Side effects 

None. However, if the statement is a data definition statement with an automatic commit as a side effect, that commit does take place.

See also 

CREATE PROCEDURE statement

BEGIN... END statement

Description 

The EXECUTE IMMEDIATE statement extends the range of statements that can be executed from within procedures and triggers. It lets you execute dynamically prepared statements, such as statements that are constructed using the parameters passed in to a procedure.

Literal strings in the statement must be enclosed in single quotes, and the statement must be on a single line.

Standards and compatibility 

Example 

The following procedure creates a table, where the table name is supplied as a parameter to the procedure. The EXECUTE IMMEDIATE statement must all be on a single line.

CREATE PROCEDURE CreateTableProc(
                  IN tablename char(30)
                  )
BEGIN
   EXECUTE IMMEDIATE 'CREATE TABLE ' || tablename ||
' ( column1 INT PRIMARY KEY)'
END

To call the procedure and create a table mytable:

CALL CreateTableProc( 'mytable' )

Collection Contents Index EXECUTE statement [T-SQL] EXIT statement [ISQL] pdf/chap9.pdf