Execute a dynamic statement and return the result with OUTPUT : EXEC « Transact SQL « SQL Server / T-SQL Tutorial






4>
5> DECLARE @sql AS NVARCHAR(12), @i AS INT
6>
7> SET @sql = N'SET @p = 10;';
8>
9> EXEC sp_executesql
10>   @stmt   = @sql,
11>   @params = N'@p AS INT OUTPUT',
12>   @p      = @i OUTPUT;
13>
14> SELECT @i;
15> GO

-----------
         10








20.19.EXEC
20.19.1.The syntax of the EXEC statement
20.19.2.EXECUTE AS
20.19.3.Dynamic Selects
20.19.4.A stored procedure with dynamic execution by using EXEC
20.19.5.Sending Variable Information to an EXEC() Call
20.19.6.Switch database in EXEC
20.19.7.Statement Limit
20.19.8.Parse exception in another level of scope: Exception in EXEC
20.19.9.Execute a dynamic statement and return the result with OUTPUT
20.19.10.Use more than one sql statement in EXEC
20.19.11.Insert into with EXEC
20.19.12.Using EXEC () to Execute a Dynamic TOP n Query