如何从SQL Server 2012中的XLSX文件扩展名批量插入?

时间:2015-06-17 17:02:39

标签: sql-server bulkinsert

有人可以建议如何批量插入.xlsx文件吗?

我无法操作excel文件,所以我无法将其更改为.txt或.csv我必须按原样执行xlsx文件,最后一个查询是:

 CREATE TABLE Table_Name(
    [ID_EQUIPMENT] [INT] NULL,
    [EQUIPMENTNAME] [NVARCHAR](50) NULL,
    [NAME] [NVARCHAR](100) NULL,
    [NAME_DESC] [NVARCHAR](100) NULL,
    [STARTDATE] [DATETIME] NULL DEFAULT (GETDATE()),
    [ENDDATE] [DATETIME] NULL DEFAULT (GETDATE()),
    [VOLUME] [INT] NULL
) ON [PRIMARY]
GO

SET @PATH = 'C:\testes\file.xlsx'

DECLARE @FILE_RESULTS TABLE
        (FILE_EXISTS INT,
     FILE_IS_A_DIRECTORY INT,
         PARENT_DIRECTORY_EXISTS INT
        )

INSERT INTO @FILE_RESULTS
(FILE_EXISTS, FILE_IS_A_DIRECTORY, PARENT_DIRECTORY_EXISTS)
EXEC MASTER.DBO.XP_FILEEXIST @PATH
SELECT @VALIDATION = FILE_EXISTS FROM @FILE_RESULTS

IF @VALIDATION = 1
    BEGIN
        PRINT 'FILE EXISTS'
        SET @SQL = "BULK 
                    INSERT Table_Name
                    FROM '"+@PATH+"' 
                    WITH (ROWTERMINATOR = '0x0a', FIRSTROW = 2); "
        SELECT @SQL
        EXEC (@SQL)
    END

执行后我收到以下错误:

(1 row(s) affected)
FILE EXISTS

(1 row(s) affected)
Msg 4832, Level 16, State 1, Line 1
Bulk load: An unexpected end of file was encountered in the data file.
Msg 7301, Level 16, State 2, Line 1
Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB `provider >"BULK" for linked server "(null)".`

0 个答案:

没有答案