all groups > sql server (alternate) > october 2004 >
You're in the

sql server (alternate)

group:

BCP column insert question



BCP column insert question tpthai NO[at]SPAM pepco.com
10/21/2004 8:36:16 AM
sql server (alternate): HI,
I'm trying to insert records to a table using bcp command. The
problem is the input file to the bcp is a text file that looks like
this:

Text file data:
1234 abc def ghi jkl mno

Expected result:
column1 1234
column2 abc def ghi jkl mno
column3 null
column4 N

My table has four columns (column1, column2, column3, column4). I
would like 1234 to go to column1, 'abc def ghi jkl mno' go to column2,
column3 is blank and column4 is always 'N' as shown above. I setup the
column4 to be defaulted to 'N'. However, I'm getting an error
regarding string truncation because, I think, SQL server is trying to
insert the 'abc def ghi jkl mno' into different columns. So the
question is what can I do to tell the bcp utility that 'abc def ghi
jkl mno' belongs to column2?

Appreciated any help.

Thanks,
Re: BCP column insert question Barry Young
10/22/2004 3:16:28 AM
Teresa,

Try delimiting your data with a , (comma) or (pipe) |. I use the pipe
because then if there is a comma in the data it won't hose your bulk insert.

Then you can use the WITH FIELDTERMINATOR = "|" in your Bulk Insert. It
should map correctly once it is delimited.

Here is an example from the Transact SQL online:

BULK INSERT Northwind.dbo.[Order Details]
FROM 'f:\orders\lineitem.tbl'
WITH
(
FIELDTERMINATOR = '|',
)
Hope this helps!

Barry

[quoted text, click to view]

Re: BCP column insert question tpthai NO[at]SPAM pepco.com
10/22/2004 12:15:16 PM
[quoted text, click to view]

Hi Barry,
Appreciated your response. I'll try it and keep you posted. Have a good day.

Thanks,
Re: BCP column insert question tpthai NO[at]SPAM pepco.com
10/22/2004 1:12:06 PM
[quoted text, click to view]
Hi Barry,
I just tried and am getting an error 'Server: Msg 4860, Level 16,
State 1, Line 1
Could not bulk insert. File 'C:\transactions.txt' does not exist.' Is
there any preliminary setup that I have to do to recognize the file?
Here is what I executed in SQL analyzer.

BULK INSERT xxx_TEST.DBO.TRANSACTION_CORRECTION
FROM 'C:\transactions.txt'
with
(FIELDTERMINATOR = '|')

Thanks,
Teresa



[quoted text, click to view]
Re: BCP column insert question Erland Sommarskog
10/23/2004 9:51:40 PM
TThai (tpthai@pepco.com) writes:
[quoted text, click to view]

BULK INSERT operates on the server, so it is looking a C:\ at your server.
If your file is on a client machine, you are better off with BCP. You
can specify field terminator with the -t options. Since | is a meta-
character for the command shell, you need to quote it:

bcp db..tbl in yourfile.txt -c -t "|" -S ....

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
Re: BCP column insert question tpthai NO[at]SPAM pepco.com
10/25/2004 10:56:27 AM
[quoted text, click to view]

Thank you very much. It worked!

AddThis Social Bookmark Button