I use SQL Server 2008 and have a table with 5 char typed columns.
CREATE TABLE [dbo].[deviceDataBulk](
[f1] [char](9) NULL,
[f2] [char](5) NULL,
[f3] [char](7) NULL,
[f4] [char](7) NULL,
[f5] [char](6) NULL)
I also have a bcp format file ;
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="9" COLLATION="Turkish_CI_AS"/>
<FIELD ID="2" xsi:type="CharFixed" LENGTH="5" COLLATION="Turkish_CI_AS"/>
<FIELD ID="3" xsi:type="CharFixed" LENGTH="7" COLLATION="Turkish_CI_AS"/>
<FIELD ID="4" xsi:type="CharFixed" LENGTH="7" COLLATION="Turkish_CI_AS"/>
<FIELD ID="5" xsi:type="CharFixed" LENGTH="6" COLLATION="Turkish_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="f1" NULLABLE="YES" xsi:type="SQLCHAR"/>
<COLUMN SOURCE="2" NAME="f2" NULLABLE="YES" xsi:type="SQLCHAR"/>
<COLUMN SOURCE="3" NAME="f3" NULLABLE="YES" xsi:type="SQLCHAR"/>
<COLUMN SOURCE="4" NAME="f4" NULLABLE="YES" xsi:type="SQLCHAR"/>
<COLUMN SOURCE="5" NAME="f5" NULLABLE="YES" xsi:type="SQLCHAR"/>
</ROW>
My data file contains fixed length char data with no field terminators in each line. So, a full line will be 34 characters long.
My problem is field 4 and field 5 may not be present for each row. I may have 21 characters long line or 28 characters long line in that file.
There is no case that field 5 exists and field 4 not.
Possible scenarios for text file are ;
f1 f2 f3 f4 f5
f1 f2 f3 f4
f1 f2 f3
I couldn’t insert this file with BULK INSERT. I want BULK INSERT to insert nulls when it doesn’t have those fields, if the tool reaches end of line, just insert nulls for the rest of the fields.
How about a 2-step approach ? First load the data into a staging table as ‘big rows’, then use a second query to split the raw lines into their corresponding fields and handle the “missing f5 and/or f4 columns”-situation accordingly ?
Would look (more or less) like this : (untested!)
The Staging file would then look like :
The [rowid] is there to keep the order identical to the order originally in the file, you might not need it but IMHO the overhead is minimal and MSSQL isn’t too keen on HEAP tables anyway so having it there is “A good thing [Tm]”