我想使用批量插入文件CSV插入到SQL Server 2012中。同一列具有日期时间,但使用批量插入日期时间格式无法工作,并且我不使用SSIS。
示例创建表:
CREATE TABLE [dbo].[scanindex_test](
[request_no] [varchar](13) NOT NULL,
[request_date] [datetime] NULL,
[id_card] [varchar](20) NULL,
[firstname] [varchar](100) NULL,
[surname] [varchar](100) NULL
)
查询 Sql Server 2012:
declare
@path varchar(255),
@sql varchar(5000)
SET @path = 'C:\Test\TESTFILE.csv'
set @sql = 'BULK INSERT [dbo].[scanindex_test] FROM ''' + @path + '''
' + ' WITH (
CODEPAGE=''RAW'',
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n''
) '
print @sql
exec (@sql)
当我运行查询时,出现错误:
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 11, column 2 (request_date).
Msg 4865, Level 16, State 1, Line 1
Cannot bulk load because the maximum number of errors (10) was exceeded.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
CSV文件中的示例数据
request_no | request_date | id_card | firstname | surname
1 | 12/7/2017 | 1122 | AA | BB
2 | 12/7/2017 | 4399 | SS | EE
3 | 13/7/2017 | 5599 | QQ | KK
运行查询时的结果:
request_no | request_date | id_card | firstname | surname
1 | 2017-12-07 00:00:00.000 | 1122 | AA | BB
2 | 2017-12-07 00:00:00.000 | 4399 | SS | EE
3 >> Error, because it's look datetime format: 2017-13-07 (yyyy-mm-dd)
但我希望结果日期时间格式(YYYY-MM-DD)是正确的:
request_no | request_date | id_card | firstname | surname
1 | 2017-07-12 00:00:00.000 | 1122 | AA | BB
2 | 2017-07-12 00:00:00.000 | 4399 | SS | EE
3 | 2017-07-13 00:00:00.000 | 5599 | QQ | KK
请帮我。提前感谢;)