2011-08-01 42 views
5

在Oracle中,编写动态SQL当一个人做这样的事情:什么是动态SQL中Oracle绑定变量的SQL Server等价物?

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)

回答

9

您将使用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; 

全部细节和例子语法是在下面的链接:

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

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

相关问题