SQL Keywords

SQL EXEC

What does the SQL EXEC command do?

EXEC runs a stored procedure, prepared statement, or ad-hoc T-SQL batch immediately.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL EXEC: Microsoft SQL Server (all editions), Azure SQL Database, Azure Synapse, SQL Server on Linux, Sybase ASE. Other systems such as MySQL and PostgreSQL support EXECUTE but not the EXEC shorthand.

SQL EXEC Full Explanation

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.

SQL EXEC Syntax

-- Stored procedure
EXEC [ @return_status = ] procedure_name 
     [ [ @param_name = ] value ] [, ...];

-- Dynamic SQL
EXEC ( N'select * from dbo.Users where is_active = 1' );

-- Execute on linked server
EXEC ( 'dbo.uspSyncData' ) AT [LinkedSrv];

SQL EXEC Parameters

  • 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.

Example Queries Using SQL EXEC

-- 1. Run a stored procedure with named parameter
EXEC dbo.uspGetEmployeeManagers @BusinessEntityID = 5;

-- 2. Capture return code
DECLARE @rc int;
EXEC @rc = dbo.uspDoWork @Verbose = 1;
SELECT @rc AS ReturnCode;

-- 3. Execute dynamic SQL from a variable
DECLARE @sql nvarchar(max) = N'SELECT COUNT(*) AS users FROM dbo.Users';
EXEC (@sql);

-- 4. Run a procedure on a linked server
EXEC ('dbo.uspRefreshDimCustomer') AT AnalyticsSrv;

Expected Output Using SQL EXEC

  • The target procedure or batch runs
  • Any result sets it produces are sent back to the client
  • Output parameters are populated, and if @return_status is specified the integer return code is stored in the supplied variable
  • For dynamic SQL, the statement’s result set is returned just as if it had been written inline

Use Cases with SQL EXEC

  • Call business-logic stored procedures from application code.
  • Run administrative procedures such as backups or index maintenance.
  • Build flexible reporting queries by assembling dynamic SQL in server-side code.
  • Launch remote sync jobs on a linked server without opening a new connection.
  • Capture a procedure’s return status to branch logic in T-SQL scripts.

Common Mistakes with SQL EXEC

  • Concatenating untrusted input into dynamic SQL and exposing SQL injection risk.
  • Forgetting parentheses around a string batch: EXEC 'SELECT 1' is invalid; use EXEC('SELECT 1').
  • Supplying parameters in the wrong order when not using named notation.
  • Expecting EXEC to run table-valued functions (use SELECT instead).
  • Trying to nest EXEC inside a user-defined function (not allowed).

Related Topics

EXECUTE, sp_executesql, CALL, PREPARE, Stored Procedures, Dynamic SQL, Linked Servers

First Introduced In

Sybase SQL Server 4.x; carried into Microsoft SQL Server 6.0

Frequently Asked Questions

What is the difference between EXEC and EXECUTE?

In SQL Server both keywords are synonyms. EXEC is simply a shorter alias for EXECUTE and supports identical syntax.

How do I pass parameters safely to dynamic SQL?

Use sp_executesql with parameter placeholders instead of string concatenation. This approach avoids SQL injection and improves plan caching.

Does EXEC start or end a transaction?

EXEC neither starts nor commits a transaction. It runs within the current session’s transaction context. Commit or rollback explicitly as needed.

Can EXEC return values to the caller?

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.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!