Using EXECUTE with a Character String

All items within the EXECUTE string must consist of character data; all numeric data must be converted prior to using EXECUTE. Functions cannot be used to build the string to be executed; however, any valid Transact-SQL statements, including functions, are available to be passed within the tsql_string, and nested EXECUTE statements are permitted.

Statement(s) inside the EXECUTE statement are not parsed, normalized, or compiled until the EXECUTE statement is executed. All table names (referenced within the same batch) are resolved when the batch is compiled. Combining statements that change database context (for example, USE dbname) and table references within the same batch will cause object resolution errors (the objects will not be found).

Incorrect:

EXEC ("USE pubs")
SELECT * FROM authors

Correct:

USE pubs
SELECT * FROM authors
OR
EXEC("USE pubs")
EXEC("SELECT * FROM authors")