批量插入错误MSG 241的SQL存储过程

时间:2016-03-04 13:22:31

标签: sql sql-server tsql stored-procedures

背景 尝试通过批量插入将数据从平面文件源批量插入数据库。给予以下。

错误

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.

数据的示例行:

2016-01-14 00:00:00         GTM         clocktic                        

格式文件(.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="\t" MAX_LENGTH="20"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="6" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="7" xsi:type="CharTerm" TERMINATOR="\t" MAX_LEN GTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="8" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="9" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="10" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="11" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="12" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
  <FIELD ID="13" xsi:type="CharTerm" TERMINATOR="\n" MAX_LENGTH="#" COLLATION="Finnish_Swedish_CI_AS"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="TStamp" xsi:type="SQLDATETIME"/>
  <COLUMN SOURCE="2" NAME="var2" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="3" NAME="var3" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="4" NAME="var4" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="5" NAME="var5" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="6" NAME="var6" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="7" NAME="var7" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="8" NAME="var8" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="9" NAME="var9" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="10" NAME="var10" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="11" NAME="var11" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="12" NAME="var12" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="13" NAME="var13" xsi:type="SQLVARYCHAR"/>
 </ROW>
</BCPFORMAT>

2 个答案:

答案 0 :(得分:0)

在导入之前尝试SET DATEFORMAT ymd;

如果不起作用:

  • 让导出过程以SQL Server可以解析的格式导出日期/时间:YYYY-MM-DDThh:mm:ss[.mmm](中间的文字字符T)或YYYYMMDD[ hh:mm:ss[.mmm]](cf DATETIME
  • 将数据导入临时表,然后通过将varchar转换为日期时间来插入到最终表。
  • 使用不同的方式导入数据,例如SQL Server Integration Services。

答案 1 :(得分:0)

通过创建.fmt文件(在它之前有一个XML格式文件)并将数据导入适当的列来解决它。解决方案并不总是很难。只需要从开始就把事情做好!