如何将数据从SQL Server导出到平面文件

In this article, we will illustrate how to export SQL Server data into a Flat file, by using the SQL Server Import and Export Wizard. We will also explain some of the problems that may appear during the process of exporting SQL Server data into a Flat file.

在本文中,我们将说明如何使用SQL Server导入和导出向导将SQL Server数据导出到平面文件中。 我们还将解释在将SQL Server数据导出到平面文件的过程中可能出现的一些问题。

To start the SQL Server Import and Export Wizard, use one of the following ways form the How to import/export data to SQL Server using the SQL Server Import and Export Wizard page.

若要启动SQL Server导入和导出向导,请使用“ 如何使用SQL Server导入和导出向导”页面将数据导入/导出到SQL Server中的以下方法之一。

汇出资料 (Exporting data)

After choosing how the SQL Server Import and Export Wizard will be started, the first page will be the Welcome page:

选择启动SQL Server导入和导出向导的方式之后,第一页将成为“ 欢迎”页面:

Press the Next button to proceed.

下一步按钮继续。

The Choose a Data Source page will be shown:

将显示“选择数据源”页面:

On this page, the source from which will be copied data can be chosen.

在此页面上,可以选择从中复制数据的源。

In the Data source drop-down, select the provider that can connect to a data source, in this case, to a SQL Server database.

在“ 数据源”下拉列表中,选择可以连接到数据源(在本例中为SQL Server数据库)的提供程序。

From the list, pick the SQL Server Native Client 11.0 provider.

从列表中,选择“ SQL Server Native Client 11.0提供程序”。

Under the Server name drop-down box, choose SQL Server where a desired database is located from which want to export data.

在“ 服务器名称”下拉框中,选择要从中导出数据的所需数据库所在SQL Server。

In the Authentication section, choose how a connection to a data source will be established, by using Windows or SQL Server authentication and under the Database drop-down box, choose a database from which will be exported data.

在“ 身份验证”部分中,选择使用Windows或SQL Server身份验证建立与数据源的连接的方式,并在“ 数据库”下拉框下,选择将从中导出数据的数据库。

Also, check a minimum permissions in order to use the SQL Server Import and Export Wizard successfully.

另外,请检查最低权限 ,以便成功使用SQL Server导入和导出向导。

When all that is set press the Next button. The next of the pages in the SQL Server Import and Export Wizard is the following one Choose a Destination page:

完成所有设置后,按“ 下一步”按钮。 SQL Server导入和导出向导中的下一页是以下“选择目标”页:

On this page, specify where data from the SQL Server database will be copied. In the Destination drop-down box, select the adequate data provider that can copy (import) data to the destination (.txt) file. That will be the Flat File Destination provider:

在此页面上,指定将从SQL Server数据库复制数据的位置。 在“ 目标”下拉框中,选择可以将数据复制(导入)到目标(.txt)文件的适当数据提供程序。 那将是平面文件目标提供者:

As soon as the Flat File Destination provider is chosen, additional options for settings a destination file under the Destination drop-down box will show:

选择“ 平面文件目标”提供程序后,将在“ 目标”下拉框下显示用于设置目标文件的其他选项:

In the File name box, enter location of the flat file. Also, the Browse button can be used to locate the flat file.

在“ 文件名”框中,输入平面文件的位置。 同样, 浏览按钮可用于定位平面文件。

From the Locale drop-down box, pick the language for sorting and for date and time formats.

从“ 区域设置”下拉框中,选择排序语言以及日期和时间格式。

Check the Unicode check box if want to use Unicode, but if this check box is checked then the Code page drop-down box is disabled:

如果要使用Unicode,请选中Unicode复选框,但是如果选中此复选框,则“ 代码页”下拉框将被禁用:

When the language is chosen, be sure that language is available on your system:

选择语言后,请确保系统上提供该语言:

Otherwise, when trying to export data from a SQL Server to a flat file, the following error may appear:

否则,当尝试将数据从SQL Server导出到平面文件时,可能会出现以下错误:

– Validating (Error)
Messages

–验证(错误)
留言内容

  • Error 0xc00470b6: Data Flow Task 1: The LocaleID 4096 is not installed on this system.
    (SQL Server Import and Export Wizard)
  • 错误0xc00470b6:数据流任务1:此系统上未安装LocaleID 4096。
    (SQL Server导入和导出向导)
  • Error 0xc004706b: Data Flow Task 1: “Destination – Expot_Import_Data_txt” failed validation and returned validation status “VS_ISBROKEN”.
    (SQL Server Import and Export Wizard)
  • 错误0xc004706b:数据流任务1:“目标– Expot_Import_Data_txt”验证失败,并返回验证状态“ VS_ISBROKEN”。
    (SQL Server导入和导出向导)
  • Error 0xc004700c: Data Flow Task 1: One or more component failed validation.
    (SQL Server Import and Export Wizard)
  • 错误0xc004700c:数据流任务1:一个或多个组件验证失败。
    (SQL Server导入和导出向导)

Error 0xc0024107: Data Flow Task 1: There were errors during task validation.
(SQL Server Import and Export Wizard)

错误0xc0024107:数据流任务1:任务验证期间发生错误。
(SQL Server导入和导出向导)

From the Code page drop-down box, select the code page for non-Unicode text:

从“ 代码页”下拉框中, 为非Unicode文本选择代码页:

In this step, a common error that may appear when exporting a SQL Server data to a Flat file is this:

在此步骤中,将SQL Server数据导出到平面文件时可能出现的常见错误是:

– Validating (Error)
Messages

–验证(错误)
留言内容

  • Error 0xc00470d4: Data Flow Task 1: The code page on Destination – Expot_Import_Data_txt.Inputs[Flat File Destination Input].Columns[Name] is 1252 and is required to be 65001.
    (SQL Server Import and Export Wizard)
  • 错误0xc00470d4:数据流任务1:“目标”上的代码页– Expot_Import_Data_txt.Inputs [平面文件目标输入] .Columns [名称]为1252,必须为65001。
    (SQL Server导入和导出向导)
  • Error 0xc00470d4: Data Flow Task 1: The code page on Destination – Expot_Import_Data_txt.Inputs[Flat File Destination Input].Columns[LastName] is 1252 and is required to be 65001.
    (SQL Server Import and Export Wizard)
  • 错误0xc00470d4:数据流任务1:“目标”上的代码页– Expot_Import_Data_txt.Inputs [平面文件目标输入] .Columns [姓]为1252,必须为65001。
    (SQL Server导入和导出向导)
  • Error 0xc004706b: Data Flow Task 1: “Destination – Expot_Import_Data_txt” failed validation and returned validation status “VS_ISBROKEN”.
    (SQL Server Import and Export Wizard)
  • 错误0xc004706b:数据流任务1:“目标– Expot_Import_Data_txt”验证失败,并返回验证状态“ VS_ISBROKEN”。
    (SQL Server导入和导出向导)
  • Error 0xc004700c: Data Flow Task 1: One or more component failed validation.
    (SQL Server Import and Export Wizard)
  • 错误0xc004700c:数据流任务1:一个或多个组件验证失败。
    (SQL Server导入和导出向导)
  • Error 0xc0024107: Data Flow Task 1: There were errors during task validation.
    (SQL Server Import and Export Wizard)
  • 错误0xc0024107:数据流任务1:任务验证期间发生错误。
    (SQL Server导入和导出向导)

One of the solutions is to change the code page of the destination file in the Code page drop-down box from e.g. 65001 (UTF-8) to 1252 (ANSI – Latin I) and then try again to export data:

解决方案之一是将“代码页下拉框中的目标文件的代码页例如65001(UTF-8)更改为1252(ANSI-Latin I) ,然后再次尝试导出数据:

Or, for all SQL Server data that have char and varchar data type to convert into nchar and nvarchar data type using CAST function:

或者,对于具有charvarchar数据类型的所有SQL Server数据,可以使用CAST函数将其转换为ncharnvarchar数据类型:

On this link, service errors, warnings and information messages can be found that may appear when exporting/importing data via the SQL Server Import and Export Wizard.

在此链接上 ,可以找到通过SQL Server导入和导出向导导出/导入数据时可能出现的服务错误,警告和信息消息。

Under the Format drop-down box, one of the three options can be chosen for the formatting of the columns that will be exported in the destination file:

格式下拉框下,可以选择三个选项之一来格式化将在目标文件中导出的列:

Delimited: when this option is selected, the column delimiter which delimits the columns in the destination file can be specified.

分隔符 :选择此选项时,可以指定用于分隔目标文件中各列的列分隔符。

Note: The column separator can be set on the Column delimiter drop- down box:

注意 :可以在“ 列分隔符 ”下拉框中设置列分隔符

Fixed width: When this option is selected, the columns in the destination file will have a fixed width.

固定宽度:选择此选项后,目标文件中的列将具有固定宽度。

Ragged right: Every column in the destination file will have fixed width except for the last column. The delimiter for the last column can be set in the Row delimiter drop-down box:

右对齐:目标文件中的每一列都将具有固定宽度,但最后一列除外。 可以在“ 行分隔符”下拉框中设置最后一列的分隔符

The next option on the Choose a Destination page of the SQL Server Import and Export Wizard is the Text qualifier text box where can be specified the text qualifier for the data in the destination file:

SQL Server导入和导出向导的“选择目标”页面上的下一个选项是“ 文本限定符”文本框,可以在其中指定目标文件中数据的文本限定符:

Note: This option is only available when the Delimited option in the Format drop-down box is selected, otherwise it will be disabled:

注意 :仅当在“ 格式”下拉框中选择“ 分隔”选项时,此选项才可用,否则将被禁用:

The last option on the Choose a Destination page of the SQL Server Import and Export Wizard is the Column names in the first data row check box:

SQL Server导入和导出向导的“选择目标”页面上的最后一个选项是第一个数据行中的列名复选框:

When this option is checked, then the names of the columns for the chosen table will be exported to the destination file:

选中此选项后,所选表的列名将被导出到目标文件:

Otherwise, when this option is unchecked then the names of the columns for the chosen table will be omitted from the destination file:

否则,当取消选中此选项时,目标文件中将省略所选表的列名:

When the all options are set in the Choose a Destination, click the Next button to proceed.

在“选择目标”中设置所有选项后,单击“ 下一步”按钮继续。

The next page of the SQL Server Import and Export Wizard is the Specify Table Copy or Query page:

SQL Server导入和导出向导的下一页是“ 指定表副本或查询”页面:

On this page, it can be specified whether to copy data from one or more table/view to a destination file by checking the Copy data from one or more tables or views radio button or to copy data from an SQL query by using the Write a query to specify the data to transfer radio button.

在此页面上,可以通过选中“ 从一个或多个表或视图复制数据”单选按钮来指定是将数据从一个或多个表/视图复制到目标文件, 还是使用Write a从SQL查询复制数据。 查询以指定要传输的数据单选按钮。

For the purpose of the article, the first option will be used. More about these two options can be found on How to import/export data to SQL Server using the SQL Server Import and Export Wizard page.

出于本文的目的,将使用第一个选项。 有关这两个选项的更多信息,请参见如何使用“ SQL Server导入和导出向导”页面将数据导入/导出到SQL Server

The next page is Configure Flat File Destination:

下一页是配置平面文件目标

On this page, options for formatting the destination flat file can be set. Additionally, you can preview the data that will be exported to a destination flat file or review/change mapping for the columns.

在此页面上,可以设置用于格式化目标平面文件的选项。 此外,您可以预览将导出到目标平面文件的数据或查看/更改列的映射。

The first option on this page is the Source table or view drop-down box. In this box, all available tables and views form the SQL Server database will be in the list that is specified in the Choose a Data Source page:

此页面上的第一个选项是“ 源表”或“视图”下拉框。 在此框中,SQL Server数据库中所有可用的表和视图将位于“ 选择数据源”页面中指定的列表中:

But, if the Write a query to specify the data to transfer radio button on the Specify Table Copy or Query page is checked, then the word “Query” will appear as only option in the Source table or view drop-down box:

但是,如果选中了“在指定表副本或查询”页面上的“ 编写查询以指定要传输的数据的查询”单选按钮,则“查询”一词将作为“ 源表”或“视图”下拉框中的唯一选项出现:

The next option is the Row delimiter drop-down box. In this drop-down box can be specified how each row in a destination file ends with. By default, the carriage return-line feed combination is set:

下一个选项是“ 行定界符”下拉框。 在此下拉框中,可以指定目标文件中每一行的结尾位置。 默认情况下,设置回车换行组合:

More about available row delimiters can be found from this link.

有关可用行定界符的更多信息,请参见此链接

Under the Column delimiter drop-down box, the delimiter can be chosen which will separate columns of data for each row:

在“ 列定界符”下拉框中,可以选择定界符,它将为每一行分隔数据列:

To see which data will be copied to a destination file, click the Preview button. The Preview Data dialog will appear that shows up to 200 rows from the data source

要查看哪些数据将被复制到目标文件,请单击“ 预览”按钮。 将出现“ 预览数据”对话框,最多显示来自数据源的200行

When click the Edit mappings button, the Column Mappings dialog will appear which shows columns mapping between the source and destination under the Mappings section. Also, can be set which subset of columns will not be copied from source to destination by setting the <ignore> option under the Destination column:

单击“ 编辑映射”按钮时,将出现“ 列映射”对话框,该对话框在“ 映射”部分下显示源和目标之间的列映射。 另外,可以通过设置“ 目标”列下的<ignore>选项来设置哪些列子集不会从源复制到目标:

The Source shows from which source data will be copied.

显示将要复制的源数据。

The Destination shows where copied data from the source will be placed

目标显示从源复制的数据将放置的位置

The Create destination file radio button creates a destination flat file and this option is enabled only when a destination file does not exist in the specified destination path:

创建目标文件”单选按钮将创建目标平面文件,并且仅当目标文件在指定的目标路径中不存在时才启用此选项:

When this option is checked, you will be able to change the names under the Destination column:

选中此选项后,您将能够在“ 目标”列下更改名称:

The Delete rows in destination file radio button, when is checked, deletes data that exist in a destination file when a new one need to be inserted.

选中“ 删除目标文件中的行”单选按钮时,当需要插入新文件时,将删除目标文件中存在的数据。

The Append rows to the destination file radio button, when is checked, placed a new data from a data source under the existing data in a destination file

选中“ 将行追加到目标文件”单选按钮时,会将来自数据源的新数据放置在目标文件中现有数据下

Under the Type column, the Integration Services data types are shown in which source data types will be converted.

在“ 类型”列下,显示了Integration Services数据类型 ,其中将转换源数据类型。

The SQL Server Import and Export Wizard uses the available XML mapping files from this location for SQL Server 2017:

SQL Server导入和导出向导使用此位置的可用XML映射文件用于SQL Server 2017:

  • C:\Program Files\Microsoft SQL Server\140\DTS\MappingFiles\ for 64-bit version

    C:\ Program Files \ Microsoft SQL Server \ 140 \ DTS \ MappingFiles \(用于64位版本)
  • C:\Program Files (x86)\Microsoft SQL Server\140\DTS\MappingFiles\ for 32-bit version

    C:\ Program Files(x86)\ Microsoft SQL Server \ 140 \ DTS \ MappingFiles \(用于32位版本)

Existing mapping files can be edited or new ones can be added:

可以编辑现有的映射文件或添加新的:

Note, when you do that, the SQL Server Import and Export Wizard needs to be closed and reopened in order to load the new or changed mapping file.

注意 ,执行此操作时,需要关闭SQL Server导入和导出向导,然后重新打开向导以加载新的或更改的映射文件。

After everything is set on the Column Mappings dialog, click the OK button and on the Configure Flat File Destination page, click the Next button to proceed with exporting data from a SQL Server database to a Flat file.

在“ 列映射”对话框中设置所有内容之后,单击“ 确定”按钮,然后在“ 配置平面文件目标”页面上,单击“ 下一步”按钮以继续将数据从SQL Server数据库导出到平面文件。

The next page of the SQL Server Import and Export Wizard is the Save and Run Package page:

SQL Server导入和导出向导的下一页是“ 保存并运行包”页面:

On this page, it can be set whether to exporting data from a SQL Server database to a Flat file immediate by checking the Run immediately check box or to save the copied data from source as SSIS package by checking the Save SSIS Package check box.

在此页面上,可以设置是通过选中“立即运行”复选框立即将数据从SQL Server数据库导出到平面文件,还是通过选中“ 保存SSIS包”复选框将从源复制的数据保存为SSIS包

In this case, we will check both options to export data from a SQL Server database to a Flat file.

在这种情况下,我们将选中两个选项以将数据从SQL Server数据库导出到平面文件。

As soon as the Save SSIS Package check box is checked, the additional options for setting the SSIS package will be available:

一旦选中“保存SSIS包”复选框,设置SSIS包的其他选项将可用:

When the SQL Server radio button is checked, the SSIS package will be saved in SQL Server under the msdb database in the sysssispackages table:

选中SQL Server单选按钮后,SSIS包将保存在sysssispackages表中msdb数据库下SQL Server中:

When the File system radio button is checked, then the SSIS package will be saved as a file with the .dtsx extension:

选中文件系统单选按钮时,SSIS包将另存为扩展名为.dtsx的文件:

From the Package protection level drop-down box, it can be chosen how the data in the SSIS package will be protected:

从“ 包保护级别”下拉框中,可以选择如何保护SSIS包中的数据:

More about these options under the Package protection level drop-down box can be found on the Access Control for Sensitive Data in Packages page.

在“ 程序包保护级别”下拉框下的有关这些选项的更多信息,可在“ 程序包的敏感数据访问控制”页面上找到。

The Password and Retype password boxes are available only if the Encrypt all data with password or Encrypt sensitive data with password options from the Package protection level drop-down box is chosen:

仅在从“ 包装保护级别”下拉框中选择“ 使用密码加密所有数据” 使用密码 加密敏感数据”选项时,“ 密码”和“ 重新输入密码”框才可

The next page of the SQL Server Import and Export Wizard is Save SSIS Package page:

SQL Server导入和导出向导的下一页是“ 保存SSIS包”页面:

On this page, additional information can be set for the SSIS package like name of the SSIS package in the Name box, add description for the SSIS package in the Description box.

在此页面上,可以为SSIS包设置其他信息,例如在“ 名称”框中输入SSIS包的名称 ,在“ 描述”框中添加SSIS包的描述

Target indicates the destination for the SSIS package, whether it will be File system or SQL Server destination. If it’s needed the destination to be changed, go to the Save and Run Package page and pick a different destination:

目标指示SSIS包的目标,无论是文件系统目标还是SQL Server目标。 如果需要更改目标,请转到“ 保存并运行程序包”页面,然后选择其他目标:

Depending on which destination is chosen SQL Server or File system, the options in the Save SSIS Package page will be a little bit different.

根据选择的目标是SQL Server还是文件系统 ,“ 保存SSIS包”页面中的选项会有所不同。

If the SQL Server radio button is selected, then the Save SSIS Package page will look like this:

如果选择了SQL Server单选按钮,则“ 保存SSIS包”页面将如下所示:

In the Server name drop-down box, type or select from the drop-down list destination SQL Server for SSIS package and use Windows or SQL Server authentication to connect to SQL Server.

在“ 服务器名称”下拉框中,键入或从下拉列表中选择目标SQL Server for SSIS包,然后使用Windows或SQL Server身份验证连接到SQL Server。

If the File system radio button, on the Save and Run Package page is chosen then the Save SSIS Package page will look like this:

如果在“ 保存并运行包”页面上选择了“ 文件系统”单选按钮,则“ 保存SSIS包”页面将如下所示:

In the File name, the path and filename of the destination file can be entered or click the Browse button to select the destination file:

文件名中,可以输入目标文件的路径和文件名,或单击浏览按钮以选择目标文件:

When the path for SSIS package is set, press the Next button in order to continue with exporting SQL Server data to a flat file.

设置SSIS包的路径后,请按“ 下一步”按钮以继续将SQL Server数据导出到平面文件。

On the Complete the Wizard page can be reviewed all choses that are made in the wizard

在“ 完成向导”页面上,可以查看向导中所做的所有选择

This page is just for verifying settings, any changes cannot be performed here.

此页面仅用于验证设置,此处无法执行任何更改。

Click the Finish button to start coping SQL Server data to a flat file.

单击“ 完成”按钮以开始将SQL Server数据复制到平面文件。

The Performing Operation page shows progress and results of the operations that are configured on the previously pages:

执行操作”页面显示了在先前页面上配置的操作的进度和结果:

翻译自: https://www.sqlshack.com/export-data-sql-server-flat-file/

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值