T-SQL Tutorial

T-SQL Sp_executesql

On Transact SQL language the sp_executesql is part of Database Engine Stored Procedures and executes a Transact-SQL statement or batch that can be reused many times.

Sp_executesql syntax:

sp_executesql [ @stmt = ] statement ;

Sp_executesql example 1:

USE model;
EXECUTE sp_executesql
N'SELECT * FROM students WHERE id= @id',
N'@id int',
@id = 3;

Sp_executesql example 2:

USE model;
DECLARE @sql_statement nvarchar(250);
SET @sql_statement = 'SELECT * FROM students WHERE id=3';
EXECUTE sp_executesql @sql_statement;