在Oracle中,编写动态sql时,会执行以下操作:
create or replace procedure myProc(n in number) as begin execute immediate 'update myTable set myColumn = :n' using n; commit; end;
然后’魔术发生’. sql Server中等效的概念/语法是什么(如果有的话)? (顺便说一句,我正在使用sql Server 2005)
解决方法
您将使用sp_executesql.绑定变量如下所示:@ var1.
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;
完整详细信息和示例语法位于以下链接: