
Using Parameter Names and Markers
Depending on the connection type that the Execute SQL task uses, the syntax of the SQL command uses different parameter markers. For example, the ADO.NET connection manager type requires that the SQL command uses a parameter marker in the format @varParameter, whereas OLE DB connection type requires the question mark (?) parameter marker.
The names that you can use as parameter names in the mappings between variables and parameters also vary by connection manager type. For example, the ADO.NET connection manager type uses a user-defined name with a @ prefix, whereas the OLE DB connection manager type requires that you use the numeric value of a 0-based ordinal as the parameter name.
The following table summarizes the requirements for SQL commands for the connection manager types that the Execute SQL task can use.
|
Connection type
|
Parameter marker
|
Parameter name
|
Example SQL command
|
|---|
|
ADO
|
?
|
Param1, Param2, …
|
SELECT FirstName, LastName, Title FROM Person.Contact WHERE ContactID = ?
|
|
ADO.NET
|
@<parameter name>
|
@<parameter name>
|
SELECT FirstName, LastName, Title FROM Person.Contact WHERE ContactID = @parmContactID
|
|
ODBC
|
?
|
1, 2, 3, …
|
SELECT FirstName, LastName, Title FROM Person.Contact WHERE ContactID = ?
|
|
EXCEL and OLE DB
|
?
|
0, 1, 2, 3, …
|
SELECT FirstName, LastName, Title FROM Person.Contact WHERE ContactID = ?
|
Using Parameters with ADO.NET and ADO Connection Managers
ADO.NET and ADO connection managers have specific requirements for SQL commands that use parameters:
-
ADO.NET connection managers require that the SQL command use parameter names as parameter markers. This means that variables can be mapped directly to parameters. For example, the variable
@varName is mapped to the parameter named @parName and provides a value to the parameter @parName.
-
ADO connection managers require that the SQL command use question marks (?) as parameter markers. However, you can use any user-defined name, except for integer values, as parameter names.
To provide values to parameters, variables are mapped to parameter names. Then, the Execute SQL task uses the ordinal value of the parameter name in the parameter list to load values from variables to parameters.
Using Parameters with EXCEL, ODBC, and OLE DB Connection Managers
EXCEL, ODBC, and OLE DB connection managers require that the SQL command use question marks (?) as parameter markers and 0-based or 1-based numeric values as parameter names. If the Execute SQL task uses the ODBC connection manager, the parameter name that maps to the first parameter in the query is named 1; otherwise, the parameter is named 0. For subsequent parameters, the numeric value of the parameter name indicates the parameter in the SQL command that the parameter name maps to. For example, the parameter named 3 maps to the third parameter, which is represented by the third question mark (?) in the SQL command.
To provide values to parameters, variables are mapped to parameter names and the Execute SQL task uses the ordinal value of the parameter name to load values from variables to parameters.
Depending on the provider that the connection manager uses, some OLE DB data types may not be supported. For example, the Excel driver recognizes only a limited set of data types. For more information about the behavior of the Jet provider with the Excel driver, see Excel Source.
Using Parameters with OLE DB Connection Managers
When the Execute SQL task uses the OLE DB connection manager, the BypassPrepare property of the task is available. You should set this property to true if the Execute SQL task uses SQL statements with parameters.
When you use an OLE DB connection manager, you cannot use parameterized subqueries because the Execute SQL Task cannot derive parameter information through the OLE DB provider. However, you can use an expression to concatenate the parameter values into the query string and to set the SqlStatementSource property of the task.