简体   繁体   English

使用 SSIS,如何将 SQL 结果导出到多个 CSV 文件?

[英]With SSIS, how do you export SQL results to multiple CSV files?

In my SSIS package, I have an Execute SQL Task that is supposed to return up to one hundred million (100,000,000) rows.在我的 SSIS package 中,我有一个执行 SQL 任务,它应该返回多达一亿(100,0000)行。

I would like to export these results to multiple CSV files, where each file has a maximum of 500,000 rows.我想将这些结果导出到多个 CSV 文件,其中每个文件最多有 500,000 行。 So if the SQL task generates 100,000,000 results, I would like to produce 200 csv files with 500,000 records in each.因此,如果 SQL 任务生成 100,000,000 个结果,我想生成 200 个 csv 文件,每个文件有 500,000 条记录。

What are the best SSIS tasks that can automatically partition the results into many exported CSV files?什么是最好的 SSIS 任务,可以自动将结果分割成许多导出的 CSV 文件?

I am currently developing a script task but find that it's not very performant.我目前正在开发一个脚本任务,但发现它的性能不是很好。 I am a bit new to SSIS so I am not familiar with all the different tasks available, and I'm wondering if maybe there's another one that can do it much more efficiently.我对 SSIS 有点陌生,所以我不熟悉所有可用的不同任务,我想知道是否还有另一个可以更有效地完成它。

Any recommendations?有什么建议吗?

Static approach Static 方法

First add a dataflow task.首先添加一个数据流任务。

In the dataflow task add the following:在数据流任务中添加以下内容:

  1. A source: in the screenshot ADO NET Source.来源:在屏幕截图中的 ADO NET Source。 That contains the query to retrieve the data包含检索数据的查询
  2. A conditional split: Every condtion you add will result in a blue output arrow.条件拆分:您添加的每个条件都会导致蓝色 output 箭头。 You need to connect every arrow to a destination您需要将每个箭头连接到目的地
  3. Excel destination or flat file destiation. Excel 目标或平面文件目标。 Depending if you want Excel files or csv files.取决于您想要 Excel 文件还是 csv 文件。 For CSV files you'll need to setup a file connection.对于 CSV 文件,您需要设置文件连接。

有条件拆分的数据流

In the conditional split you can add multiple conditions to split out your data and have a default output.在条件拆分中,您可以添加多个条件来拆分数据并具有默认的 output。

条件拆分

Flat file connection manager:平面文件连接管理器: 平面文件连接管理器

Dynamic approach动态方法

  1. Use Execute SQL Task to retrieve the variables to start a for loop.使用 Execute SQL 任务检索变量以启动 for 循环。 (BatchSize, Start, End) (批量大小,开始,结束)
  2. Add a for / foreach添加一个 for / foreach
  3. Add a dataflow task in the loop, pass in the parameters from the loop.在循环中添加一个数据流任务,从循环中传入参数。 (You can pass parameters/expressions to sub process in the dataflow using the expressions property. (您可以使用表达式属性将参数/表达式传递给数据流中的子进程。 数据流表达式 ) )
  4. Fetch the data with a source in a dataflow task based on the parameters from the for loop.根据来自 for 循环的参数,在数据流任务中使用源获取数据。
  5. Write to a destination (Excel/CSV) with a dynamic name based from the parameters of the loop.使用基于循环参数的动态名称写入目标 (Excel/CSV)。

动态方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM