2011-08-01 6 views
5

は、一つはこのような何かを行います。動的SQLのOracleバインド変数と同等のSQL Serverは何ですか?動的SQLを書くときに、Oracleでは

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

関連する問題