使用SQL Server

时间:2018-01-31 18:28:03

标签: sql-server csv bulkinsert

我无法使用批量插入从csv文件将日期数据导入SQL Server。

我使用以下存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_ImportTestData]
    @Filepath varchar(500), 
    @Pattern varchar(100), 
    @TableName varchar(128), 
    @ViewName varchar(128), 
    @ResetTable bit = 0 
AS
    SET QUOTED_IDENTIFIER OFF

    DECLARE @query varchar(1000)
    DECLARE @numfiles int
    DECLARE @filename varchar(100)
    DECLARE @files TABLE (Name varchar(200) NULL)

    --Delete the contents of the rawData table and let the user know
    IF @ResetTable = 1
    BEGIN
        PRINT 'Emptying table [' + @TableName + ']...'

        EXEC ('DELETE ' + @TableName)
    END

    --Pull a list of the CSV file names from the folder that they're stored in
    SET @query ='master.dbo.xp_cmdshell "dir '+@filepath+@pattern +' /b"'

    INSERT @files(Name) 
        EXEC (@query)

    DECLARE curs_files CURSOR FOR
         SELECT Name 
         FROM @files 
         WHERE Name IS NOT NULL

    --For each CSV file, execute a query
    SET @numfiles = 0

    OPEN curs_files
    FETCH NEXT FROM curs_files INTO @filename

    WHILE (@@FETCH_STATUS = 0)
    BEGIN
        SET @numfiles += 1

        -- BULK INSERT each CSV file into the rawData view and update the rawData 
        -- table with the file name and the upload datetime
        SET @query = ('BULK INSERT ' + @ViewName + ' FROM ''' + @Filepath+@filename + ''' WITH(FIRSTROW = 2, FIELDTERMINATOR='','', ROWTERMINATOR=''\n'');'
                + ' UPDATE ' + @TableName
                + ' SET [FileName] = ' + '''' + @filename + ''''
                + ' WHERE [FileName] Is Null;'
                + ' UPDATE ' + @TableName
                + ' SET [UploadDatetime] = ' + '''' + CAST(GETDATE() as nvarchar(1000)) + ''''
                + ' WHERE [UploadDatetime] Is Null;'
    )

    PRINT 'Importing [' + @filename + '] into [' + @TableName + ']...'
    EXEC (@query)

    FETCH NEXT FROM curs_files INTO @filename
END

CLOSE curs_files
DEALLOCATE curs_files

请注意我从http://www.decisivedata.net/blog/import-data-from-multiple-csv-files-using-bulk-insert获得了此代码。

这是我的表:

[dbo].[Test]
(
    [Money] [float] NULL,
    [Amount] [varchar](50) NULL,
    [Date] [datetime] NULL,
    [DataID] [int] IDENTITY(1,1) NOT NULL,
    [FileName] [varchar](50) NULL,
    [UploadDatetime] [datetime] NULL
)

这是我的观点

SELECT Money, Amount, Date
FROM dbo.Test

以下是我的一个csv文件示例:

Money, Amount, Date, 
1043333333.5,4, 01/29/2018
57,4, 01/29/2018
604,3,01/29/2018

这是我的问题:

EXEC usp_ImportTestData
         @filepath = 'C:\TestDataCSV\',
         @pattern = '*.csv',
         @TableName = 'Test',
         @ViewName = 'V_Test',
         @ResetTable = 1

当我运行查询时,我收到此错误消息

  

第3行第3行(日期)的批量加载数据转换错误(指定代码页的类型不匹配或无效字符)

我不知道出了什么问题!如果我尝试仅对一个文件进行直接批量插入,那么它似乎与日期字段没有任何问题。如果我摆脱日期列,它似乎也可以正常工作..

1 个答案:

答案 0 :(得分:0)

如果这是您发布的实际数据,请注意日期中包含空格。

此外,不是您的问题的一部分,请避免使用FLOAT和REAL数据类型,除非您计算行星之间的英寸数。这些数据类型仅适用于具有估计值的数字,因为它们非常大。