>DECLARE @IntVariable INT; >DECLARE @SQLString NVARCHAR(500); >DECLARE @ParmDefinition NVARCHAR(500); > >/* Build the SQL string one time. */ >SET @SQLString = > N'SELECT * FROM AdventureWorks.Sales.Store WHERE SalesPersonID = @SalesID'; >/* Specify the parameter format one time. */ >SET @ParmDefinition = N'@SalesID int'; > >/* Execute the string with the first parameter value. */ >SET @IntVariable = 275; >EXECUTE sp_executesql @SQLString, @ParmDefinition, > @SalesID = @IntVariable; >>or
>DECLARE @IntVariable INT; >DECLARE @ParmDefinition NVARCHAR(500); > >/* Build the SQL string one time. */ >SET @SQLString = > N'SELECT * FROM AdventureWorks.Sales.Store WHERE SalesPersonID = @SalesID'; >/* Specify the parameter format one time. */ >SET @ParmDefinition = N'@SalesID int'; > >/* Execute the string with the first parameter value. */ >SET @IntVariable = 275; >EXECUTE sp_executesql N'SELECT * FROM AdventureWorks.Sales.Store WHERE SalesPersonID = @SalesID', > @ParmDefinition, > @SalesID = @IntVariable; >