Re: BCP format file



JP (JP@xxxxxxxxxxxxxxxxxxxxxxxxx) writes:
I have a data file that varies in no of field for each records

e.g.
field1|field2|field3
field1|field2
field1|field2|field3|field4

The fields are seperated by "|" and the records are seperated by '\r\n'

The field values are also a varying length.

I cannot use TERMINATOR="\r\n" for field2 since the 1st and the 3rd record
have TERMINATOR="|" for field2

I would like a generic solution to this problem. The field are not of any
fixed length.

The problem is somewhat vaguely state it, but I take it that if there
only two fields one line, the remaing fields are to be NULL.

There is no way you can do this with BCP, least of all generically. You
would need to preprocess the file, to add the missing delimiters. Or
use the bulk copy API and bulk from variables. A third option is to
bulk into a staging table which has the minimum number of fields in
in the file + 1. For the example above, that would be three fields,
with the last one being an overflow field that you would have to
split. But this sounds less palatable to me.

BCP is a very squared tool that reads field by field from a file, and
BCP does not really know what a row terminator is - it only knows
field terminators.


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: ISQL is truncating contents of text field sent to text file.
    ... At first we were inserting each FOR XML to a table ... to find a single solution compatible with SQL Server 2k & 2k5. ... After asking on usenet, and being suggested to use BCP, we now ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Bulk insert Bit values
    ... Why not use the native mode of BCP instead? ... fastest way to export out and back into SQL Server tables. ... Bulk Insert in BooksOnLine for more details. ... > reconstruct the database. ...
    (microsoft.public.sqlserver.dts)
  • Re: BCP, Bulk Insert
    ... bcp and bulk insert using the sql server ... > OSQL Query: ... > BCP Query: ... > Bulk Insert: ...
    (microsoft.public.sqlserver.programming)
  • Re: BCP cant export more than MAX_INT rows
    ... Still it's odd in this age of BIGINTs that bcp can only count to 2.1 ... 1000 rows successfully bulk-copied to host-file. ... I really hope that this happened with 32-bit SQL Server? ... Books Online for SQL Server 2005 at ...
    (microsoft.public.sqlserver.tools)
  • BCP, Bulk Insert
    ... bcp and bulk insert using the sql server ... And sql server is located in the machince, ... OSQL Query: ... BCP Query: ...
    (microsoft.public.sqlserver.programming)

Loading