Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to use sp_executesql
Message
From
28/07/2009 10:09:30
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
How to use sp_executesql
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01415047
Message ID:
01415047
Views:
113
Hello All.
What code is more likely for SQL Server query optimizer :
DECLARE @IntVariable INT;
DECLARE @SQLString NVARCHAR(500);
DECLARE @ParmDefinition NVARCHAR(500);

/* Build the SQL string one time. */
SET @SQLString =
     N'SELECT * FROM AdventureWorks.Sales.Store WHERE SalesPersonID = @SalesID';
/* Specify the parameter format one time. */
SET @ParmDefinition = N'@SalesID int';

/* Execute the string with the first parameter value. */
SET @IntVariable = 275;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
                      @SalesID = @IntVariable;
or
DECLARE @IntVariable INT;
DECLARE @ParmDefinition NVARCHAR(500);

/* Build the SQL string one time. */
SET @SQLString =
     N'SELECT * FROM AdventureWorks.Sales.Store WHERE SalesPersonID = @SalesID';
/* Specify the parameter format one time. */
SET @ParmDefinition = N'@SalesID int';

/* Execute the string with the first parameter value. */
SET @IntVariable = 275;
EXECUTE sp_executesql N'SELECT * FROM AdventureWorks.Sales.Store WHERE SalesPersonID = @SalesID',
          @ParmDefinition,
          @SalesID = @IntVariable;
Next
Reply
Map
View

Click here to load this message in the networking platform