ssis 表达式任务

In this article, I will give an overview of Execute SQL Task in SSIS and I will try to illustrate some of the differences between writing an expression to evaluate SqlStatementSource property or writing this expression within a variable and change the Execute SQL Task Source Type to variable.

在本文中,我将概述SSIS中的Execute SQL Task,并尝试说明在编写表达式以评估SqlStatementSource属性或在变量中编写此表达式并将Execute SQL Task Source Type更改为变量之间的一些区别。 。

This article is the fourth article in the SSIS feature face to face series which aims to remove confusion and to illustrate some of the differences between similar features provided by SQL Server Integration Services.

本文是SSIS功能面对面系列文章中的第四篇,旨在消除混淆并说明SQL Server Integration Services提供的相似功能之间的某些区别。

执行SQL任务 (Execute SQL Task)

From the SSIS toolbox item description, Execute SQL Task in SSIS is used to execute SQL statements or stored procedures in a relational database.

在SSIS工具箱项描述中,SSIS中的“执行SQL任务”用于在关系数据库中执行SQL语句或存储过程。

Figure 1 – Execute SQL Task description

图1 –执行SQL任务描述

The SQL statement can be a:

SQL语句可以是:

  • Single SQL Command 单个SQL命令
  • Multiple SQL Commands 多个SQL命令
  • Stored procedure 存储过程

Figure 2 – Execute SQL Task in SSIS

图2 –在SSIS中执行SQL任务

This Task need a connection manager to establish a connection with a data source and it supports several data sources other than SQL Server since it can use the following connection managers:

此任务需要连接管理器来建立与数据源的连接,并且它支持SQL Server以外的其他数据源,因为它可以使用以下连接管理器:

  • Excel: An Excel connection manager enables a package to connect to a Microsoft Excel workbook file Excel: Excel连接管理器使程序包可以连接到Microsoft Excel工作簿文件
  • OLE DB: An OLE DB connection manager enables a package to connect to a data source by using an OLE DB provider OLE DB: OLE DB连接管理器使程序包可以使用OLE DB提供程序连接到数据源
  • ODBC: An ODBC connection manager enables a package to connect to a variety of database management systems using the Open Database Connectivity specification (ODBC) ODBC: ODBC连接管理器使程序包可以使用开放数据库连接规范(ODBC)连接到各种数据库管理系统
  • ADO: An ADO connection manager enables a package to connect to ActiveX Data Objects (ADO) objects, such as a recordset ADO: ADO连接管理器使程序包可以连接到ActiveX数据对象(ADO)对象,例如记录集
  • ADO.NET: An ADO.NET connection manager enables a package to access data sources by using a .NET provider ADO.NET:ADO.NET连接管理器使程序包可以使用.NET提供程序访问数据源
  • SQL Compact (SQLMobile): A SQL Server Compact connection manager enables a package to connect to a SQL Server Compact database SQL Compact(SQLMobile): SQL Server Compact连接管理器使程序包可以连接到SQL Server Compact数据库

Note that the SQL Statements syntax differs based on the data source provider, as example you have to write T-SQL when data source is SQL Server. But when it is Excel or Access, SQL commands have to be supported by Microsoft.Jet.OLEDB and Microsoft.ACE.OLEDB providers.

请注意,SQL语句的语法根据数据源提供程序的不同而不同,例如,当数据源是SQL Server时,您必须编写T-SQL。 但是,当它是Excel或Access时, Microsoft.Jet.OLEDBMicrosoft.ACE.OLEDB提供程序必须支持SQL命令。

Excel是关系数据库吗? (Is Excel a relational database?)

As mentioned above, Execute SQL Task in SSIS is used to execute SQL statements on a relational database, while Microsoft Excel is not a database engine. Because Excel is widely used to store data, and many users want to retrieve data using SQL Statements, Microsoft has given the ability to query tabular data stored in Excel and text file using Jet and ACE OLE DB providers, which pretend that Excel is a relational database and tries to force some data integrity rules while reading, i.e. forcing a single data type for each column by only reading a dominant data type and converting all other values to NULL.

如上所述,SSIS中的执行SQL任务用于在关系数据库上执行SQL语句,而Microsoft Excel不是数据库引擎。 因为Excel被广泛用于存储数据,并且许多用户希望使用SQL语句检索数据,所以Microsoft赋予了使用Jet和ACE OLE DB提供程序查询存储在Excel和文本文件中的表格数据的能力,这假装Excel是一种关系型数据库,并尝试在读取时强制执行一些数据完整性规则,即通过仅读取主要数据类型并将所有其他值转换为NULL来为每一列强制使用一种数据类型。

执行SQL任务配置 (Execute SQL Task configuration)

As shown in the image below, the Execute SQL Task in SSIS has many options that can be configured. This article will only focus on the SQL Statement configuration. If you need more details on other options, you can refer to the official documentation.

如下图所示,SSIS中的“执行SQL任务”具有许多可以配置的选项。 本文仅关注SQL语句配置。 如果您需要其他选项的更多详细信息,可以参考官方文档 。

Figure 3 – Execute SQL Task editor

图3 –执行SQL任务编辑器

There are different methods to define the SQL Statement that we need to execute. You can select the method in the SQLSourceType property:

有多种方法可以定义我们需要执行SQL语句。 您可以在SQLSourceType属性中选择方法:

  • Direct Input: You can write the SQL Statement manually in 直接输入:您可以在SQLStatement property; it can be a simple query or you can just enter a stored procedure name and change the SQLStatement属性中手动编写SQL语句。 它可以是一个简单的查询,也可以只输入存储过程名称,然后将IsQueryStoredProcedure property to IsQueryStoredProcedure属性更改为True True
  • File Connection: You can select a text file or .sql file that contains an SQL Statement using a File Connection manager 文件连接:您可以使用文件连接管理器选择包含SQL语句的文本文件或.sql文件
  • Variable: You can select a variable that contains an SQL Statement; when changing the 变量:您可以选择一个包含SQL语句的变量。 当将SQLSourceType property to Variable, a new property appears in the editor which is SQLSourceType属性更改为Variable时,新属性将出现在SourceVariable SourceVariable编辑器中

Another method to set the SQL Statement is by using expressions, you can go to the Expression Tab, and select the SQLStatementSource property to write an expression. To learn more about writing expressions in SSIS, you can refer to the official documentation: Integration Services (SSIS) Expressions.

设置SQL语句的另一种方法是使用表达式,您可以转到“表达式”选项卡,然后选择SQLStatementSource属性以编写表达式。 要了解有关在SSIS中编写表达式的更多信息,可以参考官方文档: Integration Services(SSIS)表达式 。

SqlStatementSource表达式与可变源类型 (SqlStatementSource expression vs Variable Source Type)

Many times I was asked on the difference between writing the SQL Statement within a variable and use it as Source or using expressions to define the SQL Statement. And if one approach is more recommended.

很多时候,我被问到在变量中编写SQL语句并将其用作源或使用表达式定义SQL语句之间的区别。 如果更推荐一种方法。

In this section, I will first mention each method description from the official documentation of Execute SQL Task in SSIS, then I will try to clarify more based on my experience.

在本节中,我将首先从SSIS中的Execute SQL Task的官方文档中提及每个方法的说明,然后我将根据自己的经验尝试进行更多说明。

Figure 4 – SQLStatementSource expression

图4 – SQLStatementSource表达式

官方文件 (Official Documentation)

Referring to the official documentation of Execute SQL Task in SSIS:

请参考SSIS中的执行SQL任务的官方文档 :

  • Variable Source: Set the source to a variable that defines the Transact-SQL statement. Selecting this value displays the dynamic option, SourceVariable 变量源:将源设置为定义Transact-SQL语句的变量。 选择此值将显示动态选项SourceVariable
  • SqlStatementSource expression: When you use an OLE DB connection manager, you cannot use parameterized subqueries because the Execute SQL Task in SSIS 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 SqlStatementSource表达式:使用OLE DB连接管理器时,不能使用参数化的子查询,因为SSIS中的“执行SQL任务”不能通过OLE DB提供程序派生参数信息。 但是,可以使用表达式将参数值连接到查询字符串中,并设置任务的SqlStatementSource属性。

讨论区 (Discussion)

After mentioning each approach description, and based on my experience, I can say that there is no better approach, each one has its own use case.

在提到每种方法的描述之后,根据我的经验,我可以说没有更好的方法,每种方法都有自己的用例。

As mentioned above using expression is needed if the SQL Command is built based on other variables, such as passing the table name dynamically, or passing parameter because Execute SQL Task in SSIS doesn’t support dynamic queries such as:

如上所述,如果SQL Command是基于其他变量构建的,则需要使用表达式,例如动态传递表名或传递参数,因为SSIS中的Execute SQL Task不支持动态查询,例如

"SELECT * FROM [" + @[User::SchemaName] + "].[" + @[User::TableName] + "]"

The first approach can be used if the developer decided to store the whole SQL Command inside a variable even if the variable is evaluated as an expression.

如果开发人员决定将整个SQL Command存储在变量中,即使该变量作为表达式求值,则可以使用第一种方法。

In case that variable is evaluated as an expression, there is no difference both approaches from the SQL command perspective, but it may differ in case that the statement is used multiple times within the package. As an example of the query mentioned above, if the table name is variable and this query needs to be executed at different steps within the package, evaluating the variable as expression is more efficient than writing the expression multiple times. Also, using a variable may facilitate the debugging process since it can be easily monitored.

如果将变量作为表达式求值,则从SQL命令的角度来看,两种方法都没有区别,但是在程序包中多次使用该语句的情况下,可能会有所不同。 作为上述查询的示例,如果表名是变量,并且此查询需要在包内的不同步骤执行,则将变量评估为表达式比多次写入表达式更有效。 同样,使用变量可以简化调试过程,因为可以轻松地对其进行监视。

In addition, as I remember, in the old SSIS versions, the variable source option was not available and it was added later (I think in 2012) version to facilitate the developer work in case the whole SQL command is stored inside a variable rather than adding a one variable expression in SQLStatementSource, as example if the query is built within a Script Task and stored in a variable.

另外,正如我记得的那样,在旧的SSIS版本中,变量源选项不可用,并且稍后版本(我认为在2012年)中添加了该选项,以方便开发人员进行工作,以防整个SQL命令存储在变量中而不是存储在变量中例如,如果查询是在脚本任务中构建并存储在变量中的,则在SQLStatementSource中添加一个变量表达式。

@[User::strQuery]

In the end, it is up to you to decide which approach you feel more comfortable with based on what we have mentioned.

最后,您可以根据我们所说的来决定哪种方式更舒适。

目录 (Table of contents)

SSIS OLE DB Source: SQL Command vs Table or View
SSIS Expression Tasks vs Evaluating variables as expressions
SSIS OLE DB Destination vs SQL Server Destination
Execute SQL Task in SSIS: SqlStatementSource Expressions vs Variable Source Types
Execute SQL Task in SSIS: Output Parameters vs Result Sets
SSIS Derived Columns with Multiple Expressions vs Multiple Transformations
SSIS Data types: Change from the Advanced Editor vs Data Conversion Transformations
SSIS Connection Managers: OLE DB vs ODBC vs ADO.NET
SSIS Flat Files vs Raw Files
SSIS Foreach Loop vs For Loop Container
SSIS: Execute T-SQL Statement Task vs Execute SQL Task
SSIS OLE DB来源:SQL命令与表或视图
SSIS表达式任务与将变量作为表达式求值
SSIS OLE DB目标与SQL Server目标
在SSIS中执行SQL任务:SqlStatementSource表达式与可变源类型
在SSIS中执行SQL任务:输出参数与结果集
具有多个表达式与多个转换的SSIS派生列
SSIS数据类型:高级编辑器的更改与数据转换的转换
SSIS连接管理器:OLE DB与ODBC与ADO.NET
SSIS平面文件与原始文件
SSIS Foreach循环与For循环容器
SSIS:执行T-SQL语句任务与执行SQL任务

翻译自: https://www.sqlshack.com/execute-sql-task-in-ssis-sqlstatementsource-expression-vs-variable-source-type/

ssis 表达式任务

ssis 表达式任务_在SSIS中执行SQL任务:SqlStatementSource表达式与可变源类型相关推荐

  1. ef 执行mysql语句_在EF中执行SQL语句

    一.为什么要在EF中执行SQL语句 使用EF操作数据库,可以避免写SQL语句,完成使用Linq实现,但为什么还要在EF中执行SQL语句呢.如果要写SQL语句,完全可以使用ADO.NET来操作数据库.这 ...

  2. python执行oracle的sql语句_在oracledb中执行SQL脚本一次执行一条语句

    假设我有一个sql脚本,如下所示:--split statement 1 ALTER TABLE abs ADD (make VARCHAR2(2 byte), model varCHAR2(12 B ...

  3. phpmyadmin执行mysql语句_如何在phpMyAdmin中执行sql语句

    大家使用phpMyAdmin中常遇到这样的问题:MySQL Error Message: MySQL Query Error SQL: SELECT main.*, field.* FROM ucho ...

  4. ACCESS中执行sql语句

    ACCESS中执行sql语句 简单的说:查询--新建-- 设计视图--选择表或者不选--右键新建SQL视图 不会就看图 access采用sql语句与sql的区别 Access中提供查询对象,在设计时可 ...

  5. mysql 执行cmd,mysql命令行中执行sql的几种方式总结

    1.直接输入sql执行 MySQL> select now(); +---------------------+ | now() | +---------------------+ | 2013 ...

  6. mysql的调用有哪三种方式_MySQL数据库之mysql命令行中执行sql的几种方式总结

    本文主要向大家介绍了MySQL数据库之mysql命令行中执行sql的几种方式总结 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 1.直接输入sql执行 MySQL> se ...

  7. oracle: 在sqlplus中,执行sql语句

    sqlplus输入格式如下: 用户名:  Vabc_def  口令:     1@10.10.20.37/orcl 在sqlplus中,执行sql语句,比在plsql中批量执行更快.  ps: 一定要 ...

  8. pdo_fetch执行mysql_PDO中执行SQL语句的三种方法

    在PDO中,我们可以使用三种方式来执行SQL语句,分别是 exec()方法,query方法,以及预处理语句prepare()和execute()方法~大理石构件来图加工 在上一篇文章<使用PDO ...

  9. mysql命令行执行复杂sql_mysql命令行中执行sql的几种方式总结

    1.直接输入sql执行 MySQL> select now(); +---------------------+ | now() | +---------------------+ | 2013 ...

最新文章

  1. 第 5 章 Spring Boot
  2. VM虚拟机不能上网的问题解决
  3. 心理正常与异常的区分_医学心理学:如何区分正常心理和异常心理?
  4. 《Arduino开发实战指南:机器人卷》一1.3 安装Arduino Uno驱动
  5. java闰年的计算方法_java中对 闰年的计算 以及月份天数
  6. 集美大学 - 2840 - 实验9 - 编程题
  7. 《LaTeX入门》刘海洋的杂谈勾股定理的完整源码
  8. U盘修复后文件丢失怎么办?U盘数据丢失怎么恢复
  9. PHP给PDF文件加水印(mpdf插件)
  10. MIT 18.01 Single Variable Calculus(单变量微积分)课堂笔记【6】——近似和求最值
  11. uniApp APP端 支付/分享 微信开放平台的Android 包名签名的坑
  12. js实现玫瑰动画效果
  13. js setTimeout 参数传递使用
  14. python之__dict__方法使用
  15. 转载 如何用示波器进行UART串口数据分析
  16. 安卓10 charles抓Https包 和 配置
  17. php 正则手机 w3c,最新手机号码正则表达式(php版)
  18. Virgo与Maven整合开发环境搭建(三)
  19. 虚拟机安装 vmware player
  20. PDF转PPT软件哪个好?这几款软件亲测实用

热门文章

  1. secureCRT 如何上传下载文件
  2. 《小米网抢购系统开发实践》读后感
  3. jboss eap6.1(4)(部署应用)
  4. Linux NFS存储服务部署
  5. Java中的Instanceof
  6. Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
  7. MY-SQL常用命令
  8. PHP实现9x9乘法表
  9. 通过rss阅读器写blog
  10. 计算机网络学习笔记(2. 什么是网络协议)