View Single Post
  #1 (permalink)  
Old November 22nd, 2006, 01:10 AM
Jeff Moden Jeff Moden is offline
Friend of Wrox
Points: 1,536, Level: 15
Points: 1,536, Level: 15 Points: 1,536, Level: 15 Points: 1,536, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default Fetching fields from flat file

Praveen,

Dunno what's wrong with the other thread but couldn't get the reply window to work correctly...

Anyway, using the data you posted, here's what you need to do...

Create a format file and store it somewhere where the SQL Server can see it... here's what the file should look like... the file is called "C:\Test\TestImport.bcp" in the sql code that follows that...

Code:
8.0
9
1  SQLCHAR 0 16   ""   0  Junk1          ""
2  SQLCHAR 0 12   ""   2  DocumentID     ""
3  SQLCHAR 0 6    ""   0  Junk2          ""
4  SQLCHAR 0 8    ""   3  ReceivedDate   ""
5  SQLCHAR 0 8    ""   0  Junk3          ""
6  SQLCHAR 0 5    ""   4  Verifier       ""
7  SQLCHAR 0 843  ""   0  Junk4          ""
8  SQLCHAR 0 10   ""   5  MemberID       ""
9  SQLCHAR 0 1434 "\n" 0  Junk5          ""
...and here's the code that uses it... read the comments and compare what I've done with the format file against the BCP format file example in Books Online... Bulk Insert can uses the same type of format file...

Code:
--===== If the test table exists, drop it
     IF OBJECT_ID('TempDB..#ImportTest') IS NOT NULL
        DROP TABLE #ImportTest

--===== Create the test table
 CREATE TABLE #ImportTest
        (
        RowNum INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
        DocumentID VARCHAR(12),
        ReceivedDate DATETIME,
        Verifier VARCHAR(5),
        MemberID INT
        )

--===== Do the import 
   BULK INSERT #ImportTest
   FROM 'C:\Test\TestImport.txt' --DATA FILE!!!
   WITH ( 
        BATCHSIZE    = 300000, --Basically, import the whole file all at once
        CODEPAGE     = 'RAW',  --No special language requirement
        DATAFILETYPE = 'CHAR', --Normal text input
        FIRSTROW     = 1,      --No header
        FORMATFILE   = 'C:\Test\TestImport.bcp', --FORMAT FILE!!!
        TABLOCK                --Uses a single lock on whole table for max speed
        )


...and yes, it's been tested...

Code:
 SELECT *
   FROM #ImportTest

RowNum      DocumentID   ReceivedDate             Verifier MemberID    
----------- ------------ ------------------------ -------- ----------- 
1           06276100011  2006-10-03 00:00:00.000  CSUCH    373944015
2           06276100012  2006-10-03 00:00:00.000  CSUCH    395468826
3           06276100013  2006-10-03 00:00:00.000  CSUCH    397600221

(3 row(s) affected)


--Jeff Moden
__________________
--Jeff Moden
Reply With Quote