您将使用sp_executesql.绑定变量如下所示:@ var1.
从下面的链接,对标准Northwind数据库的示例查询:
DECLARE @IntVariable int;
DECLARE @sqlString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
/* Build the sql string one time.*/
SET @sqlString =
N'SELECT BusinessEntityID,NationalIDNumber,JobTitle,LoginID
FROM AdventureWorks2008R2.HumanResources.Employee
WHERE BusinessEntityID = @BusinessEntityID';
SET @ParmDefinition = N'@BusinessEntityID tinyint';
/* Execute the string with the first parameter value. */
SET @IntVariable = 197;
EXECUTE sp_executesql @sqlString,@ParmDefinition,@BusinessEntityID = @IntVariable;
/* Execute the same string with the second parameter value. */
SET @IntVariable = 109;
EXECUTE sp_executesql @sqlString,@BusinessEntityID = @IntVariable;
完整详细信息和示例语法位于以下链接: