In Transact-SQL, EXEC (short for EXECUTE) is the command that forces SQL Server to run another batch of T-SQL. Most commonly it launches a stored procedure, but it can also run dynamic SQL held in a string variable, invoke a remote procedure on a linked server, or capture the return status of the called routine. Because EXEC switches context to the called code, it inherits the current transaction and permissions of the caller. Results sets, output parameters, and return codes are passed back to the session that issued EXEC. Dynamic SQL executed via EXEC is parsed and compiled at run time, so user-supplied string concatenation can introduce SQL injection if not properly parameterized. When executing a stored procedure, omitted parameters take their default values. If you supply a variable before the procedure name, SQL Server stores the integer return value into that variable.
procedure_name
(sysname) - Name of stored procedure or scalar function to run.@return_status
(int) - Optional local variable to capture the returned integer code.@param_name
(sysname) - Name of a formal parameter in the target procedure.value
(sql_variant) - Literal or variable supplied to the parameter.string_batch: nvarchar
(max) - T-SQL batch to run when EXEC is given a string.linked_server
(sysname) - Name of a configured linked server for remote execution.EXECUTE, sp_executesql, CALL, PREPARE, Stored Procedures, Dynamic SQL, Linked Servers
Sybase SQL Server 4.x; carried into Microsoft SQL Server 6.0
In SQL Server both keywords are synonyms. EXEC is simply a shorter alias for EXECUTE and supports identical syntax.
Use sp_executesql with parameter placeholders instead of string concatenation. This approach avoids SQL injection and improves plan caching.
EXEC neither starts nor commits a transaction. It runs within the current session’s transaction context. Commit or rollback explicitly as needed.
Yes. A stored procedure can return an integer status code via the RETURN statement, fill output parameters, and emit result sets. Capture the status with a variable placed before the procedure name.