2011-08-01 14 views

Respuesta

9

Utilizaría sp_executesql. Las variables enlazadas se ven así: @var1.

Desde el enlace de abajo, una consulta de ejemplo en contra de la base de datos Neptuno estándar:

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; 

Los detalles completos y el ejemplo de sintaxis se encuentran en los siguientes enlaces:

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

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

Cuestiones relacionadas