使用BULK INSERT格式文件和双引号的问题

时间:2017-06-01 10:02:47

标签: sql-server csv bulkinsert

我正在使用SQL Server 2014和BULK INSERT命令引用.csv文件和格式文件。我之前已经成功完成了所有字段(包括列标题),但现在数据格式略有改变,第一列heade rand数据没有包含在双重quoutes中,并且在创建格式文件时遇到问题。

示例数据......

Number of Employees;"Employee - First Name (Key)";"Employee - Last Name   (Key)";"Employee - E-Mail Address (Key)"
1;"Joe";"Bloggs";"joebloggs@msn.com"

注意,分号是字段分隔符,第一列标题和数据没有引号,所有后续标题和数据都是一致的

示例fmt格式文件...

12.0
5
1       SQLCHAR             0       0      "\""    0     ""                                                          ""
2       SQLCHAR             2       50      ";"   1  "Number of   Employees"                                        Latin1_General_CI_AS
3       SQLNCHAR            2       510     "\";\""   2    "Employee First Name"                                        Latin1_General_CI_AS
4       SQLNCHAR            2       510     "\";\""   3     "Employee Last Name"                                        Latin1_General_CI_AS
5       SQLNCHAR            2       510     "\"\r\n"  4     "E-Mail Address"                                             Latin1_General_CI_AS

我已尝试删除第一列的第一列的各种分隔符排列,在第一列之前和之后(上面是最新的)并且我仍然得到相同的错误消息...

  

无法批量加载,因为文件“\ Mypath \ import_staff_dq.fmt”不完整或无法读取。操作系统错误代码(null)。

文件始终关闭。

为简单起见,我已将dest表中的所有字段都设置为VARCHAR

1 个答案:

答案 0 :(得分:0)

我发现xml格式文件更易于理解和创建。

<?xml version="1.0"?>  
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
 <RECORD>  
  <FIELD ID="1" xsi:type="CharTerm" TERMINATOR=';"' MAX_LENGTH="10"/>  
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR='";"' MAX_LENGTH="4000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>  
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR='";"' MAX_LENGTH="4000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>  
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR='"\r\n' MAX_LENGTH="4000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>  
 </RECORD>  
 <ROW>  
  <COLUMN SOURCE="1" NAME="NumberOfEmployees" xsi:type="SQLNVARCHAR"/>  
  <COLUMN SOURCE="2" NAME="EmployeeFirstName" xsi:type="SQLNVARCHAR"/>  
  <COLUMN SOURCE="3" NAME="EmployeeLastName" xsi:type="SQLNVARCHAR"/>  
  <COLUMN SOURCE="4" NAME="EmailAddress" xsi:type="SQLNVARCHAR"/>  
 </ROW>  
</BCPFORMAT>  

使用openrowset(bulk ...)

select *
from openrowset (
    bulk 'c:\tmp.csv'
  , formatfile='c:\tmp.xml'
  , firstrow=2
  ) as t

返回:

+-------------------+-------------------+------------------+-------------------+
| NumberOfEmployees | EmployeeFirstName | EmployeeLastName |   EmailAddress    |
+-------------------+-------------------+------------------+-------------------+
|                 1 | Joe               | Bloggs           | joebloggs@msn.com |
+-------------------+-------------------+------------------+-------------------+

参考:

我也可以使用像这样的非xml格式文件获得相同的结果:

12.0
4
1    SQLCHAR    0    0    ";\""       1    NumberOfEmployees   Latin1_General_CI_AS
2    SQLCHAR    0    0     "\";\""    2    EmployeeFirstName   Latin1_General_CI_AS
3    SQLCHAR    0    0     "\";\""    3    EmployeeLastName    Latin1_General_CI_AS
4    SQLCHAR    0    0     "\"\r\n"   4    EmailAddress        Latin1_General_CI_AS