[转]Working with Parameters and Return Codes in the Execute SQL Task
本文转自:http://msdn.microsoft.com/zh-cn/magazine/cc280502(en-us,SQL.100).aspx
SQL statements and stored procedures frequently use input parameters, output parameters, and return codes. In Integration Services, the Execute SQL task supports the Input, Output, and ReturnValue parameter types. You use the Input type for input parameters, Output for output parameters, and ReturnValue for return codes.
Note |
|---|
|
You can use parameters in an Execute SQL task only if the data provider supports them. |
Parameters in SQL commands, including queries and stored procedures, are mapped to user-defined variables that are created within the scope of the Execute SQL task, a parent container, or within the scope of the package. The values of variables can be set at design time or populated dynamically at run time. You can also map parameters to system variables. For more information, see Integration Services Variables and System Variables.
However, working with parameters and return codes in an Execute SQL task is more than just knowing what parameter types the task supports and how these parameters will be mapped. There are additional usage requirements and guidelines to successfully use parameters and return codes in the Execute SQL task. The remainder of this topic covers these usage requirements and guidelines:
Using Parameter Names and MarkersDepending 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.
Using Parameters with Date and Time Data TypesUsing Date and Time Parameters with ADO.NET and ADO Connection Managers
When reading data of the SQL Server types, time and datetimeoffset, an Execute SQL task that uses either an ADO.NET or ADO connection manager has the following additional requirements:
For time data, an ADO.NET connection manager requires this data to be stored in a parameter whose parameter type is Input or Output, and whose data type is string.
For datetimeoffset data, an ADO.NET connection manager requires this data to be stored in one of the following parameters:
A parameter whose parameter type is Input and whose data type is string.
A parameter whose parameter type is Output or ReturnValue, and whose data type is datetimeoffset, string, or datetime2. If you select a parameter whose data type is either string or datetime2, Integration Services converts the data to either string or datetime2.
An ADO connection manager requires that either time or datetimeoffset data be stored in a parameter whose parameter type is Input or Output, and whose data type is adVarWchar.
For more information about SQL Server data types and how they map to Integration Services data types, see Data Types (Transact-SQL) and Integration Services Data Types.
Using Date and Time Parameters with OLE DB Connection Managers
When using an OLE DB connection manager, an Execute SQL task has specific storage requirements for data of the SQL Server data types, date, time, datetime, datetime2, and datetimeoffset. You must store this data in one of the following parameter types:
An input parameter of the NVARCHAR data type.
An output parameter of with the appropriate data type, as listed in the following table.
Output parameter type
Date data type
DBDATE
date
DBTIME2
time
DBTIMESTAMP
datetime, datetime2
DBTIMESTAMPOFFSET
datetimeoffset
If the data is not stored in the appropriate input or output parameter, the package fails.
Using Date and Time Parameters with ODBC Connection Managers
When using an ODBC connection manager, an Execute SQL task has specific storage requirements for data with one of the SQL Server data types, date, time, datetime, datetime2, or datetimeoffset. You must store this data in one of the following parameter types:
An input parameter of the SQL_WVARCHAR data type
An output parameter with the appropriate data type, as listed in the following table.
Output parameter type
Date data type
SQL_DATE
date
SQL_SS_TIME2
time
SQL_TYPE_TIMESTAMP
-or-
SQL_TIMESTAMP
datetime, datetime2
SQL_SS_TIMESTAMPOFFSET
datetimeoffset
If the data is not stored in the appropriate input or output parameter, the package fails.
Using Parameters in WHERE ClausesSELECT, INSERT, UPDATE, and DELETE commands frequently include WHERE clauses to specify filters that define the conditions each row in the source tables must meet to qualify for an SQL command. Parameters provide the filter values in the WHERE clauses.
You can use parameter markers to dynamically provide parameter values. The rules for which parameter markers and parameter names can be used in the SQL statement depend on the type of connection manager that the Execute SQL uses.
The following table lists examples of the SELECT command by connection manager type. The INSERT, UPDATE, and DELETE statements are similar. The examples use SELECT to return products from the Product table in AdventureWorks that have a ProductID greater than and less than the values specified by two parameters.
|
Connection type |
SELECT syntax |
|---|---|
|
EXCEL, ODBC, and OLEDB |
SELECT* FROM Production.Product WHERE ProductId > ? AND ProductID < ? |
|
ADO |
SELECT* FROM Production.Product WHERE ProductId > ? AND ProductID < ? |
|
ADO.NET |
SELECT* FROM Production.Product WHERE ProductId > @parmMinProductID AND ProductID < @parmMaxProductID |
The examples would require parameters that have the following names:
The EXCEL and OLED DB connection managers use the parameter names 0 and 1. The ODBC connection type uses 1 and 2.
The ADO connection type could use any two parameter names, such as Param1 and Param2, but the parameters must be mapped by their ordinal position in the parameter list.
The ADO.NET connection type uses the parameter names @parmMinProductID and @parmMaxProductID.
Using Parameters with Stored ProceduresSQL commands that run stored procedures can also use parameter mapping. The rules for how to use parameter markers and parameter names depends on the type of connection manager that the Execute SQL uses, just like the rules for parameterized queries.
The following table lists examples of the EXEC command by connection manager type. The examples run the uspGetBillOfMaterials stored procedure in AdventureWorks. The stored procedure uses the @StartProductID and @CheckDate input parameters.
|
Connection type |
EXEC syntax |
|---|---|
|
EXCEL and OLEDB |
EXEC uspGetBillOfMaterials ?, ? |
|
ODBC |
{call uspGetBillOfMaterials(?, ?)} For more information about ODBC call syntax, see the topic, Procedure Parameters, in the ODBC Programmer's Reference in the MSDN Library. |
|
ADO |
If IsQueryStoredProcedure is set to False, EXEC uspGetBillOfMaterials ?, ? If IsQueryStoredProcedure is set to True, uspGetBillOfMaterials |
|
ADO.NET |
If IsQueryStoredProcedure is set to False, EXEC uspGetBillOfMaterials @StartProductID, @CheckDate If IsQueryStoredProcedure is set to True, uspGetBillOfMaterials |
To use output parameters, the syntax requires that the OUTPUT keyword follow each parameter marker. For example, the following output parameter syntax is correct: EXEC myStoredProcedure ? OUTPUT.
For more information about using input and output parameters with Transact-SQL stored procedures, see Parameters (Database Engine), Returning Data by Using OUTPUT Parameters, and EXECUTE (Transact-SQL).
Getting Values of Return CodesA stored procedure can return an integer value, called a return code, to indicate the execution status of a procedure. To implement return codes in the Execute SQL task, you use parameters of the ReturnValue type.
The following table lists by connection type some examples of EXEC commands that implement return codes. All examples use an input parameter. The rules for how to use parameter markers and parameter names are the same for all parameter types—Input, Output, and ReturnValue.
Some syntax does not support parameter literals. In that case, you must provide the parameter value by using a variable.
|
Connection type |
EXEC syntax |
|---|---|
|
EXCEL and OLEDB |
EXEC ? = myStoredProcedure 1 |
|
ODBC |
{? = call myStoredProcedure(1)} For more information about ODBC call syntax, see the topic, Procedure Parameters, in the ODBC Programmer's Reference in the MSDN Library. |
|
ADO |
If IsQueryStoreProcedure is set to False, EXEC ? = myStoredProcedure 1 If IsQueryStoreProcedure is set to True, myStoredProcedure |
|
ADO.NET |
Set IsQueryStoreProcedure is set to True. myStoredProcedure |
In the syntax shown in the previous table, the Execute SQL task uses the Direct Input source type to run the stored procedure. The Execute SQL task can also use the File Connection source type to run a stored procedure. Regardlesss of whether the Excecute SQL task uses the Direct Input or File Connection source type, use a parameter of the ReturnValue type to implement the return code. For more information about how to configure the source type of the SQL statement that the Execute SQL task runs, see Execute SQL Task Editor (General Page).
For more information about using return codes with Transact-SQL stored procedures, see Returning Data by Using a Return Code and RETURN (Transact-SQL).
Configuring Parameters and Return Codes in the Execute SQL TaskFor more information about the properties of parameters and return codes that you can set in SSIS Designer, click the following topic:
For more information about how to set these properties in SSIS Designer, click the following topic:
External ResourcesBlog entry, Stored procedures with output parameters, on blogs.msdn.com
CodePlex sample, Execute SQL Parameters and Result Sets, on msftisprodsamples.codeplex.com
|
See Also[转]Working with Parameters and Return Codes in the Execute SQL Task的更多相关文章
- [转]SSIS Execute SQL Task : Mapping Parameters And Result Sets
本文转自:http://www.programmersedge.com/post/2013/03/05/ssis-execute-sql-task-mapping-parameters-and-res ...
- Execute SQL Task 参数和变量的映射
Execute SQL Task能够执行带参数的SQL查询语句或存储过程(SP),通过SSIS的变量(Variable)对参数赋值.对于不同的Connection Manager,在Task中需要使用 ...
- SSIS 增量更新
本文转自 http://sqlblog.com/blogs/andy_leonard/archive/2007/07/09/ssis-design-pattern-incremental-loads. ...
- 周末惊魂:因struts2 016 017 019漏洞被入侵,修复。
入侵(暴风雨前的宁静) 下午阳光甚好,想趁着安静的周末静下心来写写代码.刚过一个小时,3点左右,客服MM找我,告知客户都在说平台登录不了(我们有专门的客户qq群).看了下数据库连接数,正常.登录阿里云 ...
- Control Flow 如何处理 Error
在Package的执行过程中,如果在Data Flow中出现Error,那么Data Flow component能够将错误行输出,只需要在组件的ErrorOutput中进行简单地配置,参考<D ...
- django 1.7+ default_permissions
由于做Caption要做权限设计.在核心类的设计的时候需要做好权限的基础设计.django 1.7+以后 django.db.modes新增特性 default_permissions,官方文档语焉不 ...
- Unit Of Work--工作单元(一)
简介 最近忙着新项目的架构,已经有一段时间没有更新博客了,一直考虑着要写些什么,直到有一天跟朋友谈起他们公司开发数据层遇到的一些问题时,我想应该分享一些项目中使用的数据访问模式. 虽然最近一直都在使用 ...
- C# 调用配置文件SQL语句 真2B!
/********************************************************************************* ** File Name : SQ ...
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
随机推荐
- WordPress 一键置顶文章(推荐用SM Sticky Clicky Star)
在 WordPress入门 之 发布新文章和管理文章 中,倡萌已经简单提到可以在文章编辑界面或者快速编辑界面设置置顶文章,但是如果你想在后台文章列表中添加一键置顶文章的功能,不妨试试 Quick St ...
- OpenStack 认证服务 KeyStone连接和用户管理(五)
一) 创建环境变量链接keyston vim adminrc export OS_USERNAME=admin export OS_PASSWORD=redhat export OS_PROJECT_ ...
- cocos2djs ctor init onEnter的区别
cocos2d-html5 onEnter init ctor构造函数 ---js特有特性(和c++有点不一样 ctor 构造函数, new 一个对象的时候调用-----coco2d-js , 默认c ...
- logging模块介绍
在我们的实际开发过程中,我们有时候需要记录一些重要操作,或者程序运行情况,我们就需要在程序里面写入日志,来达到更快的排错跟记录重要操作的目的.在Python中logging模块就很好的解决了这个问题, ...
- VMware Workstation虚拟机进入BIOS
1.<F2>键 2.
- Flask实战第50天:cms添加轮播图的模态对话框制作
编辑cms_banners.html, 在{% block main_content%}中加上表给内容如下 {% block main_content %} <table class=" ...
- Abp数据库迁移注意事项
前记:昨天下载了一个Abp模板,然后尝试利用EF CodeFirst进行数据库生成操作,然后就是一直报错 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务 ...
- Web应用扫描测试工具Vega
Web应用扫描测试工具Vega Vega是Kali Linux提供的图形化的Web应用扫描和测试平台工具.该工具提供代理和扫描两种模式.在代理模式中,安全人员可以分析Web应用的会话信息.通过工具 ...
- nyoj 1007 GCD(数学题 欧拉函数的应用)
GCD 描述 The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b) ...
- 【深度搜索+剪枝】POJ1011-Sticks
深搜部分和之前的POJ2362差不多,只是有几处需要额外的剪枝. [思路]排序后从最短木棒开始搜索至木棒长总和,如果木棒长总和sum能整除当前棒长,则进入深搜. [剪枝]先前POJ2362的剪枝部分不 ...
Note
Stay Up to Date with Integration Services