 |
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

December 11th, 2007, 02:24 PM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Bulk Insert
I have difficulties with bulk insert.
If I have a .txt file with 1 element per row it work fine, see below:
SP:
Code:
CREATE procedure MyTest
as
CREATE TABLE Test
(
jobnumber char(15)
)
DECLARE @SQL varchar(2000),
@PathFileName varchar(200)
set @PathFileName = 'c:\yak.txt'
BULK INSERT Test
FROM 'c:\yak.txt' WITH ( FIELDTERMINATOR = ',', DATAFILETYPE =
'char')
EXEC (@SQL)
select * from Test
GO
.TXT Table:
ABCDEFGHIJKLMN
CCCCCCCCC
BBBB
MMMMMMMMM
Now I add a 2nd field to my table (jobphase) and change my .txt table as well, it does not work:
SP:
Code:
CREATE procedure MyTest
as
CREATE TABLE Test
(
jobnumber char(15),
jobphase char(15)
)
DECLARE @SQL varchar(2000),
@PathFileName varchar(200)
set @PathFileName = 'c:\yak.txt'
BULK INSERT Test
FROM 'c:\yak.txt' WITH ( FIELDTERMINATOR = ',', DATAFILETYPE =
'char')
EXEC (@SQL)
select * from Test
GO
.TXT table:
ABCDEFGHIJKLMN,AAAAAAAA,
CCCCCCCCC,GGGGGGGGG,
BBBB,TTTTTTTTT,
MMMMMMMMM,YYYYYYYY,
Get error:
Server: Msg 4832, Level 16, State 1, Line 1
Bulk Insert: Unexpected end-of-file (EOF) encountered in data file.
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'STREAM' reported an error. The provider did not give any information about the error.
The statement has been terminated.
|

December 11th, 2007, 04:25 PM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You have THREE columns in the textfile!
The last column is empty but still there.
|

December 11th, 2007, 04:42 PM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
I've tried both with 2 columns (one comma) and 3 colums (two commas). No matter what I do I get the error described above...
ABCDEFGHIJKLMN,AAAAAAAA
CCCCCCCCC,GGGGGGGGG
BBBB,TTTTTTTTT
MMMMMMMMM,YYYYYYYY
but it still does not fly.....
|

December 11th, 2007, 05:08 PM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Here is a simple test that does not work and gives error (run i qry analyzer)
CREATE TABLE Names
(
Firstname varchar(20),
Surname varchar(20)
)
BULK INSERT Names FROM 'c:\names.txt'
File:
John Greed
Kevin Heath
Server: Msg 4832, Level 16, State 1, Line 1
Bulk Insert: Unexpected end-of-file (EOF) encountered in data file.
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'STREAM' reported an error. The provider did not give any information about the error.
The statement has been terminated.
|

December 11th, 2007, 05:19 PM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
You can't do it that way. Your table has two columns and you are deliniating the text in the file at all. For example, try this, it works:
File:
JohnGreed
KevinHeath
CREATE TABLE Names
(
Firstname varchar(20)
)
BULK INSERT Names FROM 'c:\names.txt'
select * from Names
drop table Names
If you are specifying more than one column, you need to provide it MUCH more information that just "hey, here's a file for my bulk insert".
Look in BOL for Bulk Insert. That, plus the above example, should get you started.
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
|

December 11th, 2007, 05:23 PM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Here is your answer:
File:
John Greed
Kevin Heath
CREATE TABLE Names
(
Firstname varchar(20),
LastName varchar(20)
)
BULK INSERT Names FROM 'c:\names.txt' WITH (FIELDTERMINATOR = ' ')
select * from Names
drop table Names
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
|

December 11th, 2007, 05:45 PM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Scott:
Using your first example it created this (using 1 data element). I just have 3 records in the text file so why did it code insert a "Null" in line 4 and 5"?
JohnGreed
KevinHeath
TimBarker
NULL
NULL
After this I tried with 2 fields and field terminator = ' ', gives me the same error, for some reason it does not like fieldterminators......
|

December 11th, 2007, 08:45 PM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
OK, first things first. You are getting the NULL lines because you have extra lines AFTER TimBarker. Open up your text document and press CTRL+END. I'll bet you $$ that your cursor is now 2 or 3 lines BELOW TimBarker. This means that you probably pressed the ENTER key (a hard return) after you entered the TimBarker entry.
So, press the Backspace or Delete key until the cursor is now blinking next to the last "r" in TimBarker. Now rerun the T-SQL code and you'll only get the three names with no NULL values.
Now, the FIELDTERMINATOR parameter looks for exactly the value you pass (a space, comma, etc). If you use the FIELDTERMINATOR parameter with the following:
JohnGreed
KevinHeath
TimBarker
and then use the FIELDTERMINATOR parameter with a Space as a delimiter, your results will be very wrong.
Try this:
File:
John,Greed
Kevin,Heath
CREATE TABLE Names
(
Firstname varchar(20),
LastName varchar(20)
)
BULK INSERT Names FROM 'c:\names.txt' WITH (FIELDTERMINATOR = ',')
select * from Names
drop table Names
Make sense? Experiment some more, such as the following:
File:
JohnGreed,
KevinHeath
CREATE TABLE Names
(
Firstname varchar(20)
)
BULK INSERT Names FROM 'c:\names.txt' WITH (FIELDTERMINATOR = ',')
select * from Names
drop table Names
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
|

December 11th, 2007, 10:20 PM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Thank you Scott, it worked.
|

December 17th, 2007, 07:53 AM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
BULK INSERT FOR ARRAY |
MathLearner |
Beginning VB 6 |
4 |
September 28th, 2007 07:18 AM |
bulk insert |
hymavathy_kr |
VB Databases Basics |
1 |
May 17th, 2007 07:47 AM |
BULK INSERT NULL(s) |
nathansevugan |
SQL Server 2000 |
2 |
November 29th, 2005 01:11 AM |
Bulk Insert |
luma |
SQL Server DTS |
1 |
July 13th, 2005 01:48 AM |
Bulk insert |
deyakhatib |
SQL Server 2000 |
6 |
March 4th, 2004 04:09 AM |
|
 |