Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 11th, 2007, 02:24 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #2 (permalink)  
Old December 11th, 2007, 04:25 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You have THREE columns in the textfile!
The last column is empty but still there.


Reply With Quote
  #3 (permalink)  
Old December 11th, 2007, 04:42 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

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.....

Reply With Quote
  #4 (permalink)  
Old December 11th, 2007, 05:08 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #5 (permalink)  
Old December 11th, 2007, 05:19 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
========================
Reply With Quote
  #6 (permalink)  
Old December 11th, 2007, 05:23 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
========================
Reply With Quote
  #7 (permalink)  
Old December 11th, 2007, 05:45 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

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......
Reply With Quote
  #8 (permalink)  
Old December 11th, 2007, 08:45 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
========================
Reply With Quote
  #9 (permalink)  
Old December 11th, 2007, 10:20 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: West Palm Beach, Florida, USA.
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thank you Scott, it worked.

Reply With Quote
  #10 (permalink)  
Old December 17th, 2007, 07:53 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also see this detaild answer.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=93981


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:46 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.