在动态SQL中SQL Server等效的Oracle绑定变量是什么?

bri*_*ler 7 t-sql sql-server dynamic-sql

在Oracle中,编写动态SQL时,会执行以下操作:

create or replace procedure myProc(n in number)
as
begin
  execute immediate
   'update myTable set myColumn = :n' 
   using n;
commit;
end;
Run Code Online (Sandbox Code Playgroud)

然后'魔术发生'.SQL Server中等效的概念/语法是什么(如果有的话)?(顺便说一句,我正在使用SQL Server 2005)

mwi*_*ahl 12

你会用的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, @ParmDefinition,
                      @BusinessEntityID = @IntVariable;
Run Code Online (Sandbox Code Playgroud)

完整详细信息和示例语法位于以下链接:

http://msdn.microsoft.com/en-us/library/ms188001.aspx

http://msdn.microsoft.com/en-us/library/ms175170.aspx