sql 生成csv数据

介绍 ( Introduction )

A few months back, I presented a paper at SQL Saturday 327 in Johannesburg, South Africa. Late last month I received an email from one of the attendees. His issue was quite interesting and I decided to share it with you. The gentleman wanted a SSIS script that would permit him to extract data from a SQL Server database table and place it in a CSV file with a dynamically allocated name. Being a strong advocate of using the SSIS toolbox, I experimented with an alternative solution. We are going to construct THIS SOLUTION in today’s get together.

几个月前,我在南非约翰内斯堡SQL Saturday 327上发表了一篇论文。 上个月下旬,我收到了一位与会者的电子邮件。 他的问题很有趣,我决定与您分享。 这位绅士想要一个SSIS脚本,该脚本允许他从SQL Server数据库表中提取数据并将其放置在具有动态分配名称的CSV文件中。 作为使用SSIS工具箱的坚定拥护者,我尝试了一种替代解决方案。 我们将在今天的聚会中构建此解决方案。

Let’s get started.

让我们开始吧。

入门 ( Getting Started )

As our point of departure, we shall once again utilize our ‘SQLShackFinancial” database. We shall utilize data from the FASB table (see below).

作为出发点,我们将再次使用我们的“ SQLShackFinancial”数据库。 我们将利用FASB表中的数据(见下文)。

Our end goal is to obtain the following output:

我们的最终目标是获得以下输出:

The astute reader will note that the csv file name contains the date and time on which the file was created.

精明的读者会注意到,csv文件名包含创建文件的日期和时间。

Opening Visual Studio we begin by creating a new Integration Services project.

打开Visual Studio,我们首先创建一个新的Integration Services项目。

We select New and Project (see above).

我们选择“新建”和“项目”(请参见上文)。

We select an Integration Services project and give our project a name. We click OK to create the project.

我们选择一个Integration Services项目,并给我们的项目起一个名字。 我们单击确定以创建项目。

We find ourselves on our Integration Services project workspace (see above).

我们发现自己在我们的Integration Services项目工作区中(请参见上文)。

Our first task is to create a data connection to our SQLShackFinancial database.

我们的首要任务是创建与SQLShackFinancial数据库的数据连接。

创建数据库连接 ( Creating the database connection )

We begin by right clicking in the “Connection Manager” box and selecting a “New OLE DB Connection” (see above).

首先,右键单击“连接管理器”框,然后选择“新建OLE DB连接”(请参见上文)。

The “Configure OLE DB Connection Manager” dialogue box is brought up. We select “New” (see above).

出现“配置OLE DB连接管理器”对话框。 我们选择“新建”(见上文)。

The “Connection Manager” dialogue box is now brought up and we configure this box as shown above.

现在出现“连接管理器”对话框,我们如上所述配置此对话框。

Testing the connection, we find that we are ready to go.

测试连接,我们发现我们已经准备好了。

We now add a Data Flow Task to our work surface (see below).

现在,我们将一个数据流任务添加到我们的工作表中(见下文)。

Double clicking on the “Data Flow Task” we are brought into the “Data Flow Task” designer (see below).

双击“数据流任务”,我们进入“数据流任务”设计器(见下文)。

We now add an “OLE DB Data Source” to our work surface (see below).

现在,我们将一个“ OLE DB数据源”添加到工作表中(见下文)。

Double clicking on the “OLE DB Source” brings up the “OLE DB Source Editor” dialogue box.

双击“ OLE DB Source”,将弹出“ OLE DB Source Editor”对话框。

We configure our connection manager to point to the FASB table (see above).

我们将连接管理器配置为指向FASB表(请参见上文)。

The columns tab shows the data columns within the table (see above).

列选项卡显示表中的数据列(请参见上文)。

添加和配置目标CSV文件 ( Adding and configuring the destination csv file )

We are now in a position to add the destination flat file which will contain the table data in a csv format.

现在,我们可以添加目标平面文件,该文件将包含csv格式的表数据。

We drag a “Flat File Destination” control onto our work surface (see above) and join the “OLE DB” data source to the “Flat File” destination (see below).

我们将“平面文件目标”控件拖到工作表面上(请参见上文),并将“ OLE DB”数据源连接到“平面文件”目标(请参见下文)。

Double clicking the control brings up the “Flat File Destination” editor (see above). We click “New” to create a new connection.

双击控件将打开“平面文件目标”编辑器(请参见上文)。 我们单击“新建”以创建新的连接。

The “Flat File Format” dialogue box is brought into view (see above and to the left). We accept the “Delimited” radio button. Click OK.

进入“平面文件格式”对话框(请参见上方和左侧)。 我们接受“定界”单选按钮。 单击确定。

The “Flat File Connection Manager Editor” is then brought up. We are asked for a “Description” (which is optional) but more importantly we are asked for an output file name.

然后出现“平面文件连接管理器编辑器”。 我们要求提供“描述”(这是可选的),但更重要的是,我们要求提供输出文件名。

We give our output file the name “FASB_” and set its type to csv (see above). We click “Open”.

我们将输出文件命名为“ FASB_”,并将其类型设置为csv(请参见上文)。 我们点击“打开”。

Clicking the “Columns” tab, we see that the fields from our table are visible. We click OK to exit this dialogue box and then click on the “Mappings” tab to configure the source to the destination (see below).

单击“列”选项卡,我们看到表中的字段可见。 我们单击“确定”退出此对话框,然后单击“映射”选项卡以将源配置为目标(请参见下文)。

We click OK to exist the “Flat File Destination Editor”.

我们单击“确定”以存在“平面文件目标编辑器”。

We now find ourselves back on our working surface. THE ISSUE is that in creating the flat csv file we have hard-wired the file name is this is NOT what we want.

现在,我们回到工作表面。 问题是,在创建平面csv文件时,我们已经硬连线了文件名,这不是我们想要的

为我们的输出文件创建一个动态文件名 ( Creating a dynamic file name for our output file )

We begin by right clicking on our output file connection and bringing up its “Properties” box (see above and to the bottom right).

首先,右键单击输出文件连接,并打开其“属性”框(请参见上方和右下方)。

We scroll down to find the “Expressions” property (see above and to the bottom right).

我们向下滚动以找到“表达式”属性(请参见上方和右下方)。

Clicking on the “Expressions” ellipsis the “Property Expresssion Editor” is brought up(see above).

单击“表达式”省略号,将弹出“属性表达式编辑器”(请参见上文)。

We select the “Connectionstring” Property from the “Property” dropdown list (see above) and click upon the “Expression” box.

我们从“属性”下拉列表中选择“连接字符串”属性(见上文),然后单击“表达式”框。

The “Expression Builder” dialogue box is brought into view (see above).

出现“表达式生成器”对话框(请参见上文)。

In the “Expression” box, we enter the following code snippet. Adding a time component (to the string) enables us to produce multiple daily extracts.

在“表达式”框中,输入以下代码段。 在字符串中添加时间成分使我们能够产生多个每日摘录。


"C:\\SQLShack\\FASB_" +
Right("0" + (DT_STR,4,1252) DatePart("m",getdate()),2) +
Right("0" + (DT_STR,4,1252) DatePart("d",getdate()),2) +
(DT_STR,4,1252) DatePart("yyyy",getdate()) + "_"
+ (DT_STR,2,1252) DatePart("hh",getdate()) +"_"
+  (DT_STR,2,1252) DatePart("n",getdate())
+ ".csv"

The astute reader will note that when we “Evaluate” the expression (see above) that the fully qualified file name appears in the “Evaluated value” box. Further we must note the usage of two “\\” for every one that we wish to appear in the file “Evaluated” path and file name. FORTRAN and COBOL programmers will remember this one!

精明的读者会注意到,当我们“评估”表达式(见上文)时,完全限定的文件名将出现在“评估值”框中。 此外,我们必须注意每个希望出现在文件“ Evaluated”路径和文件名中的两个“ \\”的用法。 FORTRAN和COBOL程序员会记住这一点!

We click OK to leave the “Expression Builder” and OK to leave the “Property Expressions Editor” (see above).

我们单击“确定”离开“表达式生成器”,然后单击“确定”离开“属性表达式编辑器”(请参见上文)。

We find ourselves back on our work surface (see above).

我们发现自己回到了工作表面(见上文)。

让我们给我们的包裹一个“旋转” ( Let us give our package a “whirl” )

We click the “Debug” tab on the top ribbon and select “Start Debugging” (see above).

我们单击顶部功能区上的“调试”选项卡,然后选择“开始调试”(请参见上文)。

The process completes successfully.

该过程成功完成。

Our extract file may be seen in the screen dump above. Note that the file name contains the run date and time of the process.

我们的提取文件可以在上面的屏幕转储中看到。 请注意, 文件 包含进程的运行日期和时间。

Opening the comma delimited file in Notepad, we see the data in a comma delimited form.

在记事本中打开逗号分隔的文件,我们将以逗号分隔的形式查看数据。

结论 ( Conclusion )

Oft times we find that we have external processes that require extracts of data from our tables. In some cases the final format must be in CSV format.

通常,我们发现我们有一些外部过程需要从表中提取数据。 在某些情况下,最终格式必须为CSV格式。

In this “get together” we have constructed a quick and dirty process to pull data from a database table and to place the data into a flat csv file IN ADDITION to providing a mechanism to run the process throughout the day.

在此“聚在一起”中,我们构建了一个快速而肮脏的过程,以从数据库表中提取数据并将数据放入另外的平面csv文件中,以提供一种机制来全天运行该过程。

Should you wish the code for this article, please feel free to contact either the SQLShack editor or me.

如果您希望使用本文的代码,请随时与SQLShack编辑器或我联系。

In the interim, happy programming.

在此期间,编程愉快。

翻译自: https://www.sqlshack.com/creating-dynamically-generated-csv-files-containing-sql-server-data/

sql 生成csv数据

sql 生成csv数据_创建包含SQL Server数据的动态生成的CSV文件相关推荐

  1. mysql创建sql文件命令_创建包含sql命令的sql脚本文件

    sql脚本是包含一到多个sql命令的sql语句,我们可以将这些sql脚本放在一个文本文件中(我们称之为"sql脚本文件"),然后通过相关的命令执行这个sql脚本文件.基本步骤如下: ...

  2. sql 删除数据_从零开始学SQL:是什么、如何安装、基本语法、表格(创建、删除、更新)、数据(插入、删除、更新)...

    一.学习知识的黄金圈思维 用黄金圈思维分析自己学习SQL,能增加自己的效率和坚持下去的动力. 二.SQL基础知识 1.数据库概念: database ,按照数据结构来组织.存储和管理数据的仓库. 2. ...

  3. sql 不同数据库同步数据_什么是SQL数据同步

    sql 不同数据库同步数据 SQL Data Sync is a service that allows synchronizing data across multiple Azure SQL da ...

  4. 造成sql注入的功能点_创建一个SQL注入保护功能

    造成sql注入的功能点 .SQLCode { font-size: 13px; font-weight: bold; font-family: monospace;; white-space: pre ...

  5. sql同时修改两个表的数据_如何用SQL做数据透视表?

    - 点击上方"中国统计网"订阅我吧!- 讲过很多次,数据分组和数据透视表很像,Sql 中的数据分组大家应该都很熟悉了,用的就是 group by. 数据透视表是作为一个数据分析师最 ...

  6. sql 表变量 临时表_何时使用SQL临时表与表变量

    sql 表变量 临时表 It is very beneficial to store data in SQL Server temp tables rather than manipulate or ...

  7. sql查询禁用缓存_如何在SQL Server 2017中启用和禁用身份缓存

    sql查询禁用缓存 Every data warehouse developer is likely to appreciate the significance of having surrogat ...

  8. sql 视图不排序_算法工程师SQL进阶:神奇的自连接与子查询

    自连接与子查询是SQL中非常重要的两项技术,自连接是针对相同的表的联结方法,是SQL语言面向集合编程思想的集中体现,而子查询有很多变式,关联子查询技术是在实际中进行行与行之间的比较时非常重要的方法,特 ...

  9. sql 倒数第二个_小白初探SQL(一)

    SQL(结构化查询语言)是目前使用最广泛的数据库语言,可以同各种数据库建立联系.进行沟通,应用十分的广泛. ◆ 结构化查询语言(Structured Query Language)简称SQL,是结构 ...

最新文章

  1. linux 网卡配置详情
  2. 连接池你用对了吗?一次Unexpected end of stream异常的排查
  3. python画图代码彩虹-python绘制彩虹图
  4. maven java 目录结构_java – 为maven目录结构生成的源文件的位置
  5. CF788B Weird journey
  6. java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
  7. halcon自动对焦算法
  8. Silverligth API for ArcGIS应用程序IIS发布
  9. net framework 4.0安装未成功,原因是?
  10. 网络营销-渠道、公关
  11. 传奇 定时任务(泡点、时间触发、任务活动等)
  12. md+邮件服务器+334错误,邮件发送,无尽的501错误。TCP发送邮件解决方案
  13. LibGDX QQ群建立,欢迎对libGDX有兴趣的程序员加入。
  14. Mysq大小比较EQ、NE、GE、GT、LE、LT
  15. 电商总结-日志监控系统的解决方案
  16. [原创]华为战略管理培训体会
  17. yum 有趣的linux命令,Centos6中yum方法安装sl(linux有趣命令之一sl跑火车)
  18. 粮食行业视频监控系统互联互通技术规范
  19. linux运维 职友集,为你介绍一下Linux运维到底是干啥的?
  20. 原来还能这么干——观罗胖2022年《时间的朋友》后感

热门文章

  1. rms 公式 有效值_有效值是电流电压的均方根值吗?
  2. CentOS环境Tomcat配置JDK的另一种方式
  3. CCF CSP 201703-1 分蛋糕
  4. HTML li标签排列有空白间隙
  5. RS485 RS232
  6. 企业级docker私有仓库的配置与使用
  7. 17011301(UE4的AnimDynamic)
  8. 查看Linux连接数
  9. TypeScript声明文件
  10. LeetCode(821)——字符的最短距离(JavaScript)