CSV文件的OpenRowset返回空白或有错误

时间:2016-11-23 08:41:40

标签: sql sql-server-2008 csv

所以早些时候,我即将创建一个将机器生成的CSV文件导入我们数据库的代码。我在Excel中创建了一个,我使用了这段代码

select *from openrowset('MSDASQL','Driver={Microsoft Access Text Driver (*.txt, *.csv)}'
,'select * from D:\Test.CSV')

它工作正常。 但是当我处理实际数据时。上面的代码不起作用。

因此,CSV文件包含一个前导的18行数据,这些数据可以被删除(它只是机器的名称),所需的数据位于第19行。

搜索之后,我发现了一段代码,然后我尝试了CSV文件,这是

    SELECT *
        FROM OPENROWSET(BULK 'D:\Data\sample\device1_2016-08-03_15-24-58.csv',
        FORMATFILE='D:\Data\sample\BCPFormat.xml',
        FIRSTROW = 19) AS a

但数据是空白的!

我也试过这段代码

select * from OpenRowset('MSDASQL', 'Driver={Microsoft Access Text Driver (*.txt, *.csv)};DefaultDir=D:\Data\sample\;','select * from device1_2016-08-03_15-24-58.csv')

错误说明

An error occurred while preparing the query "select * from device1_2016-08-03_15-24-58.csv" for execution against OLE DB provider "MSDASQL" for linked server "(null)". 

我们使用的服务器DBMS是SQL Server 2008 R2。我们还使用MS Office 2010来创建电子表格。

欢迎任何想法,提前谢谢!

编辑:

我将包含CSV文件的屏幕截图。 screenshot

我还将包含XML文件(因为我已经读过FMT文件是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=',' />
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR=','/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="6" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="7" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="8" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="9" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="10" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="11" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="12" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="13" xsi:type="CharTerm" TERMINATOR=',' />
  <FIELD ID="14" xsi:type="CharTerm" TERMINATOR='\n' />
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="NO." xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="2" NAME="Time" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="3" NAME="ms" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="4" NAME="degC1" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="5" NAME="degC2" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="6" NAME="degC3" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="7" NAME="degC4" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="8" NAME="degC5" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="9" NAME="degC6" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="10" NAME="A12345678901" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="11" NAME="A12345678902" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="12" NAME="A12345678903" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="13" NAME="A12345678904" xsi:type="SQLVARYCHAR" />
  <COLUMN SOURCE="14" NAME="A1234" xsi:type="SQLVARYCHAR" />
 </ROW>
</BCPFORMAT>

我已将CSV文件的屏幕截图包含在textfile

Vendor,GUARDIAN
Model,ZR-RX45
Version,Ver1.04
Sampling,10s
Total data points,0           
Start time,2016-08-03,15:25:01
End time,2016-08-03,15:24:59
Trigger time,2016-07-30,08:21:50
AMP Settings
CH,Signal name,Input,Range,Filter,Span
CH34, "PC-2",TEMP,TC_K,Off,250.000000,0.000000,degC
CH35, "PC-11",TEMP,TC_K,Off,250.000000,0.000000,degC
CH36, "PC-19",TEMP,TC_K,Off,250.000000,0.000000,degC
CH37, "PC-16",TEMP,TC_K,Off,250.000000,0.000000,degC
CH38, "PC-08",TEMP,TC_K,Off,250.000000,0.000000,degC
CH39, "PC-18",TEMP,TC_K,Off,250.000000,0.000000,degC
Logic/Pulse,Off
Data
Number,Date&Time,ms,CH34,CH35,CH36,CH37,CH38,CH39,Alarm1-10,Alarm11-20,Alarm21-30,Alarm31-40,AlarmOut
NO.,Time,ms,degC,degC,degC,degC,degC,degC,A1234567890,A1234567890,A1234567890,A1234567890,A1234
1,2016-07-30 08:21:50,000,+0.0,+0.0,+0.0,+0.0,+0.0,+0.0,LLLLLLLLLL,LLLLLLLLLL,LLLLLLLLLL,LLLLLLLLLL,LLLL

1 个答案:

答案 0 :(得分:2)

这是因为CSV中的每一行都没有正确的字段数,因此无法解析。即使你只是要求第19行(或第21行),整个文件仍然会被解析。

您可以通过修改.CSV文件在每行上有14个字段(即13个逗号)来解决此问题:

Vendor,GUARDIAN,,,,,,,,,,,,
Model,ZR-RX45,,,,,,,,,,,,
Version,Ver1.04,,,,,,,,,,,,
Sampling,10s,,,,,,,,,,,,
Total data points,0,,,,,,,,,,,,      
Start time,2016-08-03,15:25:01,,,,,,,,,,,
End time,2016-08-03,15:24:59,,,,,,,,,,,
Trigger time,2016-07-30,08:21:50,,,,,,,,,,,
AMP Settings,,,,,,,,,,,,,
CH,Signal name,Input,Range,Filter,Span,,,,,,,,,,,,
CH34, "PC-2",TEMP,TC_K,Off,250.000000,0.000000,degC,,,,,,,
CH35, "PC-11",TEMP,TC_K,Off,250.000000,0.000000,degC,,,,,,,
CH36, "PC-19",TEMP,TC_K,Off,250.000000,0.000000,degC,,,,,,,
CH37, "PC-16",TEMP,TC_K,Off,250.000000,0.000000,degC,,,,,,,
CH38, "PC-08",TEMP,TC_K,Off,250.000000,0.000000,degC,,,,,,,
CH39, "PC-18",TEMP,TC_K,Off,250.000000,0.000000,degC,,,,,,,
Logic/Pulse,Off,,,,,,,,,,,,
Data,,,,,,,,,,,,,
Number,Date&Time,ms,CH34,CH35,CH36,CH37,CH38,CH39,Alarm1-10,Alarm11-20,Alarm21-30,Alarm31-40,AlarmOut
NO.,Time,ms,degC,degC,degC,degC,degC,degC,A1234567890,A1234567890,A1234567890,A1234567890,A1234
1,2016-07-30 08:21:50,000,+0.0,+0.0,+0.0,+0.0,+0.0,+0.0,LLLLLLLLLL,LLLLLLLLLL,LLLLLLLLLL,LLLLLLLLLL,LLLL

然后你的命令有效:

SELECT a.*
FROM OPENROWSET(BULK 'D:\some_location\device1_2016-08-03_15-24-58.csv',
                FORMATFILE='D:\some_location\BCPFormat.xml',
                FIRSTROW = 21) AS a