使用bcp导入的分隔符

时间:2014-12-04 02:57:33

标签: sql sql-server

我正在尝试从文本文件导入批量数据。我使用的陈述如下:

DECLARE @s as varchar(300)

SET @s='bcp [hc]..[QRYM_COMPANIES] in ' + char(34) + 'C:\SQL Files\health canada allfiles\comp.txt' + char(34) + ' -C ACP -c -t \", -r\n -e' + char(34) + 'C:\SQL Files\health canada allfiles\err\comp.err.txt' + char(34) + ' -b250 -m50 -SServer -Usa -P' + char(34) + 'mypassword' + char(34) + ' -h' + char(34) + 'TABLOCK' + char(34) 


PRINT @s

EXEC master..xp_cmdshell @s

现在,通常我会放-t,但我的数据在某些数据字段中包含逗号,导致数据被移位。当我尝试使用-t \“我得到错误”字符串数据,右截断“,这就是为什么我必须使用-t的组合,\”

这里的问题是我在传输的数据中得到了引用。以下是我的数据的示例行:

"20042","NSL95","4797","NU SKIN INTERNATIONAL, INC.","DIN OWNER","Y","N","N","N","","75 WEST CENTER STREET","PROVO","UTAH","UNITED STATES","84601",""

表格描述如下。为了避免“字符串截断错误”,我在左侧字段大小中添加了1。

有人可以帮我解决这个问题吗?

感谢

CREATE TABLE [health_canada].[dbo].[QRYM_COMPANIES] (

DRUG_CODE   VARCHAR(18) NOT NULL, --NUMERIC(8) NOT NULL,

MFR_CODE        VARCHAR(15),

COMPANY_CODE        VARCHAR(16), --NUMERIC(6),

COMPANY_NAME        VARCHAR(180),

COMPANY_TYPE        VARCHAR(140),

ADDRESS_MAILING_FLAG        VARCHAR(11),

ADDRESS_BILLING_FLAG        VARCHAR(11),

ADDRESS_NOTIFICATION_FLAG       VARCHAR(11),

ADDRESS_OTHER       VARCHAR(11),

SUITE_NUMERIC       VARCHAR(120),

STREET_NAME         VARCHAR(180),

CITY_NAME       VARCHAR(160),

PROVINCE        VARCHAR(140),

COUNTRY         VARCHAR(140),

POSTAL_CODE         VARCHAR(120),

POST_OFFICE_BOX         VARCHAR(115)

) ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:2)

考虑在bcp调用中使用格式文件。它易于配置,您可以根据文件进行微调。只需添加-f和使用它的路径:http://msdn.microsoft.com/en-us/library/ms162802.aspx

<强>步骤

  • 将格式文件添加到磁盘(确保SQL Server可以到达路径)。
  • 更新查询中的路径。
  • 运行查询。

我使用了这个文件: enter image description here

此代码使用格式文件

导入您的示例数据,不带引号
DECLARE @importTable VARCHAR(128) = 'hc.dbo.QRYM_COMPANIES';
DECLARE @importPath VARCHAR(8000) = '\\YOURPATH\comp.txt';
DECLARE @formatFilePath VARCHAR(8000) = '\\YOURPATH\formatFile.txt';
DECLARE @separator VARCHAR(10) = '';

DECLARE @export VARCHAR(8000) = 'bcp "' + @importTable + '" in "' + @importPath + '" -T -f "' + @formatFilePath + '" -C RAW -S ' + @@SERVERNAME;

PRINT(@export)
EXEC xp_cmdshell @export 

<强>输出:

enter image description here

SELECT * FROM hc.dbo.QRYM_COMPANIES:

enter image description here

将此格式文件添加到您的磁盘:

<?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="0" xsi:type="CharTerm" TERMINATOR='"' MAX_LENGTH="1" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="1" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="18" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="15" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="16" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="180" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="140" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="6" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="11" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="7" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="11" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="8" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="11" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="9" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="11" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="10" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="120" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="11" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="180" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="12" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="160" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="13" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="140" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="14" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="140" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="15" xsi:type="CharTerm" TERMINATOR='","' MAX_LENGTH="120" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="16" xsi:type="CharTerm" TERMINATOR='"\r\n' MAX_LENGTH="115" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="DRUG_CODE" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="2" NAME="MFR_CODE" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="3" NAME="COMPANY_CODE" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="4" NAME="COMPANY_NAME" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="5" NAME="COMPANY_TYPE" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="6" NAME="ADDRESS_MAILING_FLAG" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="7" NAME="ADDRESS_BILLING_FLAG" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="8" NAME="ADDRESS_NOTIFICATION_FLAG" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="9" NAME="ADDRESS_OTHER" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="10" NAME="SUITE_NUMERIC" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="11" NAME="STREET_NAME" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="12" NAME="CITY_NAME" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="13" NAME="PROVINCE" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="14" NAME="COUNTRY" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="15" NAME="POSTAL_CODE" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="16" NAME="POST_OFFICE_BOX" xsi:type="SQLVARYCHAR"/>
 </ROW>
</BCPFORMAT>

如果您需要其他文件/表格,可以使用bcp自动生成新格式文件(如果格式文件有效,则无需立即执行此操作)

重要: 格式文件导出后:

  • TERMINATOR="\&quot;,"替换为TERMINATOR='","'

  • TERMINATOR="\r\n"替换为TERMINATOR='"\r\n' *

  • 添加到<record>(将删除第一个引文):<FIELD ID="0" xsi:type="CharTerm" TERMINATOR='"' MAX_LENGTH="1" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>

自动生成查询

DECLARE @importTable VARCHAR(128) = 'hc.dbo.QRYM_COMPANIES';
DECLARE @formatFilePath VARCHAR(8000) = '\\YourFilePath\formatFile.txt';
DECLARE @bcpCall VARCHAR(8000);

SET @bcpCall = 'bcp "' + @importTable + '" format nul -c -t", -x -f "' + @formatFilePath + '" -T -S ' + @@SERVERNAME;

PRINT @bcpCall 

EXEC xp_cmdshell @bcpCall