EXECUTE IMMEDIATE Statement [ESQL] [SP]

Extends the range of statements that can be executed from within procedures. It lets you execute dynamically prepared statements, such as statements that are constructed using the parameters passed in to a procedure.

Syntax

Syntax 1

EXECUTE IMMEDIATEexecute-option ] string-expression
execute-option:
   WITH QUOTESON | OFF ]
   | WITH ESCAPESON | OFF } 
   | WITH RESULT SETON | OFF }

Syntax 2

EXECUTEstring-expression )

Parameters

Examples

Usage

Literal strings in the statement must be enclosed in single quotes, and must differ from any existing statement name in a PREPARE or EXECUTE IMMEDIATE statement. The statement must be on a single line.

Only global variables can be referenced in a statement executed by EXECUTE IMMEDIATE.

Only syntax 2 can be used inside Transact-SQL stored procedures.

Side Effects

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

Standards

  • SQL—ISO/ANSI SQL compliant.

  • Sybase—Supported in Open Client/Open Server.

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.

Related reference
BEGIN … END Statement
CREATE PROCEDURE Statement
QUOTED_IDENTIFIER Option [TSQL]