12

I'm trying to import the below excel file present in the azure blob storage into sql server

EXCEL File

enter image description here

Query

SELECT * 
    FROM OPENROWSET(
        BULK 'container/testfile.xlsx', 
        DATA_SOURCE = 'ExternalSrcImport',
        FORMATFILE='container/test.fmt', FORMATFILE_DATA_SOURCE = 'ExternalSrcImport',
        codepage = 1252,
        FIRSTROW = 1
        ) as data

Format file

10.0  
4  
1       SQLCHAR       0       7       "\t"     1     DepartmentID     ""  
2       SQLCHAR       0       100     "\t"     2     Name             SQL_Latin1_General_CP1_CI_AS  
3       SQLCHAR       0       100     "\t"     3     GroupName        SQL_Latin1_General_CP1_CI_AS  
4       SQLCHAR       0       24      "\r\n"   4     ModifiedDate     ""  

Illustration of Format File

enter image description here

when I execute the query, I'm getting the below error

Msg 4863, Level 16, State 1, Line 210 Bulk load data conversion error (truncation) for row 1, column 1 (DepartmentID).

looks like field terminator in the format file is not working, any ideas to import the file ?

4

1 回答 1

4

您的格式文件表示导入制表符分隔值文件,但在源路径中您指的是 xslx 文件。
Xslx 文件是多个 XML 文件的 ZIP 存档,批量导入将无法处理它。要打开它,您需要使用 Microsoft Jet 或 ACE 驱动程序,这里有一些示例: using-openrowset-to-read-excel。在处理文件之前,您需要将文件从 Blob 存储下载到本地磁盘。您可以使用 SQL 代理或 SSIS 来下载它。

其他选项是将您的数据保存为 CSV 或制表符分隔文件,并直接从 blob 存储加载。

于 2019-05-21T17:39:09.880 回答