RSS Feed

Difference between EXECUTE & EXECUTE()

Difference between EXECUTE & EXECUTE()


The EXECUTE, or EXEC statement executes a stored procedure by following the command with the name of the stored procedure and any proper parameter arguments:

EXEC sp_Empfind @City='DE'


The EXECUTE(character_string) function executes a character string containing a Transact-SQL command.
It can be written like EXEC(character_string) also. The EXECUTE(character_string) function is not compiled until run time and the generated plan is not cached and reused. The following example executes a dynamic string that concatenates literal strings with variable values:

DECLARE @mycol varchar(30)
DECLARE @mytable varchar(30)
SELECT @mycol = 'EMPID'
SELECT @mytable = 'Employee'
EXEC('SELECT ' + @mycol + ' FROM ' + @mytable)