Question : BCP with Format.xml file - Trying to understand <COLUMN> elements.

I built a BCP import utility that utilizes a format file, "Format.xml." (See code.)  The utility and the format file worked just fine until I added a primary key column (RTImportID) as the first column in the target table.  After adding the column, the BCP import failed with an error that indicates that the source and target columns are no longer paired correctly:

Starting copy...
SQLState = 22018, NativeError = 0
Error = [Microsoft][SQL Native Client]Invalid character value for cast specification
SQLState = 22018, NativeError = 0
Error = [Microsoft][SQL Native Client]Invalid character value for cast specification
....

I don't understand why the pairing error occurs with the addition of the RTImportID column as the first column.  After all, the element in Format.xml file reference the target columns by name ("Day," "Month," "Year,"...) and not by position.  

If I move RTImportID to the right of the BCP target rows, the import works again.  Additionally, if I scramble the elements in Format.xml, the import works.  Why does the presence of RTImportID as the first column in the table break the import?  
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:


 
  
  
  
  
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
  
  
  
  
 

Open in New Window Select All

Answer : BCP with Format.xml file - Trying to understand <COLUMN> elements.

OK, that is an XML schema / XML format file, are you importing XML ? Can you post a data sample ?

Typically, you should import into a "staging" area, and in so doing, can have all as SQLNVARCHAR and avoid some datatype conversions...

whilst "debugging" do not have to use BCP, can simply use openrowset and get a lot more visibility over your data e.g.

SELECT * FROM OPENROWSET( BULK 'C:\ee\myimportfile.txt', FORMATFILE = 'C:\ee\myformatfile.xml', FIRSTROW = 1) AS a


Random Solutions  
 
programming4us programming4us