473,385 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to BCP IN to table with IDENTITY column

JJA
I have fixed length records (167 bytes) in a .txt file and want to load
this data exactly as is into a staging table where I hope to be able to
later get at selected columns using the SUBSTRING function. Here is my
target table:
CREATE TABLE JJA_BCP_NHO_DAT (
RecID int IDENTITY(1,1) NOT NULL
, Data VARCHAR(167) NOT NULL
)
Here is my bcp command:
bcp DevMDW.dbo.JJA_BCP_NHO_DAT in
N:\RawData_MDS\Purchase\UnzipArea\2005_08_Aug_0149 7407.txt -f bcp.fmt

Here is the .fmt file:
8.0
1
1 SQLCHAR 0 167 "" 2
Data SQL_Latin1_General_CP1_CI_AS
Here are the first 3 lines of data in the .txt file (note how 1st 2
bytes are blank; last 2 bytes of each record are 78, 79, 80):

02160168C013CMA20050819 328
UUU AGAWAM 36422005072901001 BERKSHIRE
BANK 25013 78
01940155C001 MA20050805 254
UUU AGAWAM 28072005071801001 WEBSTER
BANK 25013 79
02350188C014CMA20050729 067
UUU AGAWAM 24812005070701001 FIRST
PIONEER FARM CR 25013 80

The bcp command runs OK but the output is "shifted" in the column
called DATA in the table. The IDENTITY column looks good in all rows,
but only the first row is OK in the DATA column. Starting in the 2nd
row, all bytes are truncated or shifted by what appears to be 2
characters. This is hard to show with pasting results but I ran this
query:

SET ROWCOUNT 10
SELECT RecID
, SUBSTRING(Data,1,10) as FirstTen
, SUBSTRING(Data,158,10) as LastTen
, DATALENGTH(DATA) AS LEN_Data FROM JJA_BCP_NHO_DAT

1 02160168 25013 78 167
2 019401 25013 167
3 79 0235 CR 2501 167
4 3 80 01 CO 25 167
5 013 80 CORP 167
6 25013 80 REG SYS 167
7 25013 98 ANK 167
8 25013 VGS BK 167
9 2501 21 MTG CO 167
10 RP 25 RYWIDE HOM 167

Every line in the .txt file ends after column 167 with x'0d0a'. I've
tried a field terminator of \r\n and that produces this error:

SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC SQL Server Driver]Unexpected EOF encountered
in BCP data-file

Thanks in advance for some help on this.

Sep 8 '05 #1
2 15018
Hi

Try a format file such as
8.0
2
1 SQLINT 0 0 "" 1 RecID
""
2 SQLCHAR 0 167 "\r\n" 2 Data
Latin1_General_CI_AS

John

"JJA" <jo***@cbmiweb.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have fixed length records (167 bytes) in a .txt file and want to load
this data exactly as is into a staging table where I hope to be able to
later get at selected columns using the SUBSTRING function. Here is my
target table:
CREATE TABLE JJA_BCP_NHO_DAT (
RecID int IDENTITY(1,1) NOT NULL
, Data VARCHAR(167) NOT NULL
)
Here is my bcp command:
bcp DevMDW.dbo.JJA_BCP_NHO_DAT in
N:\RawData_MDS\Purchase\UnzipArea\2005_08_Aug_0149 7407.txt -f bcp.fmt

Here is the .fmt file:
8.0
1
1 SQLCHAR 0 167 "" 2
Data SQL_Latin1_General_CP1_CI_AS
Here are the first 3 lines of data in the .txt file (note how 1st 2
bytes are blank; last 2 bytes of each record are 78, 79, 80):

02160168C013CMA20050819 328
UUU AGAWAM 36422005072901001 BERKSHIRE
BANK 25013 78
01940155C001 MA20050805 254
UUU AGAWAM 28072005071801001 WEBSTER
BANK 25013 79
02350188C014CMA20050729 067
UUU AGAWAM 24812005070701001 FIRST
PIONEER FARM CR 25013 80

The bcp command runs OK but the output is "shifted" in the column
called DATA in the table. The IDENTITY column looks good in all rows,
but only the first row is OK in the DATA column. Starting in the 2nd
row, all bytes are truncated or shifted by what appears to be 2
characters. This is hard to show with pasting results but I ran this
query:

SET ROWCOUNT 10
SELECT RecID
, SUBSTRING(Data,1,10) as FirstTen
, SUBSTRING(Data,158,10) as LastTen
, DATALENGTH(DATA) AS LEN_Data FROM JJA_BCP_NHO_DAT

1 02160168 25013 78 167
2 019401 25013 167
3 79 0235 CR 2501 167
4 3 80 01 CO 25 167
5 013 80 CORP 167
6 25013 80 REG SYS 167
7 25013 98 ANK 167
8 25013 VGS BK 167
9 2501 21 MTG CO 167
10 RP 25 RYWIDE HOM 167

Every line in the .txt file ends after column 167 with x'0d0a'. I've
tried a field terminator of \r\n and that produces this error:

SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC SQL Server Driver]Unexpected EOF encountered
in BCP data-file

Thanks in advance for some help on this.

Sep 8 '05 #2
JJA
Thank you so much! I had not placed \r\n within double-quotes in my
previous attempts. Your suggestion worked great.

Sep 8 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gunnar Vøyenli | last post by:
Hi (SQL Server 2000) I have an existing table (t) with a column that is NOT an identity column (t.ID), but it has manually inserted "row numbers". I want to make this column become an identity...
2
by: me | last post by:
I would like to add an Identity to an existing column in a table using a stored procedure then add records to the table and then remove the identity after the records have been added or something...
7
by: Sam | last post by:
Hi, I would like a piece of advice. I have 3 foreign keys in a table used as primary keys for this table. Is it useful in that case to have just one identity column that would be used as the...
2
by: valerio | last post by:
Hi all, I've some problem to import data to DB2 using the db2move and db2look tools. Follow the problem : I have exported data from db2 v. 7 database on windows server, using the db2look and...
4
by: UDBDBA | last post by:
Hi: we have column with GENERATED ALWAYS AS DEFAULT. So, we can insert into this column manually and also let db2 generate a value for this column. Given a scenario, how can i find the NEXTVAL...
3
by: mal_k100 | last post by:
Hi All, 1. Created table in SQL Server 2K with Primary key as int Identity 2. Link to table in MS Access 2K 3. Using form in MSAccess to update the linked table I want SQL server to...
2
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name)...
2
by: .Net Newbie | last post by:
Hello, I am somewhat new to .Net and currently working on an intranet site using C# going against SQL Server 2k. I am accepting personal information on a single webform and trying to insert the...
1
by: smauldin | last post by:
Creating a table with an identity column works fine create table test(a integer, id integer generated always as identity ) When I attempt to add an identity column it fails. create table...
5
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.