Best way to import txt-file to MS-SQL ?

Hi!

Have a textfile that locks like:

1,"Kelly 2","Reynold","kelly@reynold.com"
22,"John 2","Smith","bill@smith.com"
37,"Sara 2","Parker","sara@parker.com"

Have a example code, that remove Double quotes (lock below)
But cant figer out how to use the same sourcecode to use the abow txt-file
The sourcode use today this txt-file:

"Kelly 2","Reynold","kelly@reynold.com"
"John 2","Smith","bill@smith.com"
"Sara 2","Parker","sara@parker.com"



Need help to make the modification

ALTER PROCEDURE "dbo"."ps_StudentList_Import"
@PathFileName varchar(100),
@OrderID integer,
@FileType tinyint
AS


DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
  
  SET @SQL = "BULK INSERT TmpStList FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = '"",""') "
 END
ELSE
 BEGIN
  
  SET @SQL = "BULK INSERT TmpStList FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = ',') "
 END


EXEC (@SQL)


INSERT StudentList (StFName,StLName,StEmail,OrderID)
SELECT  CASE WHEN @FileType = 1 THEN SUBSTRING(StFName,2,DATALENGTH(StFName)-1)
             ELSE StFName
        END,
        SUBSTRING(StLName,1,DATALENGTH(StLName)-0),
        CASE WHEN @FileType = 1 THEN SUBSTRING(StEmail,1,DATALENGTH(StEmail)-1)
             ELSE StEmail
        END,
        @OrderID
FROM tmpStList


TRUNCATE TABLE TmpStList

Open in new window

LVL 2
team2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eugene ZCommented:
create new or recreate TmpStList table with just 3 columns
instead of 4
0
team2005Author Commented:
Hi!

Tryed that, but test1 field is emty ?
0
team2005Author Commented:
Hi!

Need help solving this issue ASAP.

I am stuck...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

lcohanDatabase AnalystCommented:
"Tryed that, but test1 field is emty "

What is test1? Is it a column of tmpStList table? can you post tmpStList table structure?
0
team2005Author Commented:
Hi!

test1 is a colum in table tmpStlist

tmpStlist

stFName  varchar(50)
stLName varchar(50)
stEmail varchar(100)

Other table > StudentList

StID int identity
test1 int
stFName  varchar(50)
stLName varchar(50)
stEmail varchar(100)
OrderID int
0
Eugene ZCommented:
if you need to keep
test1 is a colum in table tmpStlist
create new table and do bulk insert there
if not delete test1 column from tmpStlist table

tmpStlist

stFName  varchar(50)
stLName varchar(50)
stEmail varchar(100)
0
team2005Author Commented:
Hi!

Can't delete test1. Have a textfile like:

1,"Kelly 2","Reynold","kelly@reynold.com"
22,"John 2","Smith","bill@smith.com"
37,"Sara 2","Parker","sara@parker.com"

Maby better to import textfile to tmbtable
and loop and insert data into a new table. And use
 SUBSTRING in the loop.

Another problem is that if i import another textfile.
That contains of same test1 value. The BULK insert wil fail...

Must be able to yust update, and not insert.

Realy need to solve this issue
0
Eugene ZCommented:
ok
I got it now

you have no problems with

"Kelly 2","Reynold","kelly@reynold.com"
"John 2","Smith","bill@smith.com"
"Sara 2","Parker","sara@parker.com"

but have problem to bulk this

1,"Kelly 2","Reynold","kelly@reynold.com"
22,"John 2","Smith","bill@smith.com"
37,"Sara 2","Parker","sara@parker.com"
--

please confirm that TmpStList has 3 columns?

also can you please post the error when you import the "bad "file data?
0
Eugene ZCommented:
looks like it is the source
Using BULK INSERT to Load a Text File
http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eugene ZCommented:
it means due to import 4 parts file data

you need to create new table TmpStList and use it

CREATE TABLE TmpStList
(
ID varchar (50) NOT NULL,   ----<<<<<new
 stFName varchar (50) NOT NULL,
 stLName varchar (50) NOT NULL,
 stEmail varchar (100) NOT NULL
)
go
0
team2005Author Commented:
Hi!

Tryed to make this Stored procedure now:

ALTER PROCEDURE "dbo"."ps_StudentList_Import6"
@PathFileName varchar(100),
@OrderID integer,
@FileType tinyint
AS


DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
  
  SET @SQL = "BULK INSERT TmpStList6 FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = '"",""') "
 END
ELSE
 BEGIN
  
  SET @SQL = "BULK INSERT TmpStList6 FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = ',') "
 END


EXEC (@SQL)


INSERT StudentList6 (test1,StFName,StLName,StEmail,OrderID)
SELECT  CASE WHEN @FileType = 1 THEN test1 END,
        CASE WHEN @FileType = 1 THEN SUBSTRING(StFName,2,DATALENGTH(StFName)-1)
             ELSE StFName
        END,
        SUBSTRING(StLName,1,DATALENGTH(StLName)-0),
        CASE WHEN @FileType = 1 THEN SUBSTRING(StEmail,1,DATALENGTH(StEmail)-1)
             ELSE StEmail
        END,
        @OrderID
FROM tmpStList6


TRUNCATE TABLE TmpStList6

Open in new window


Get this error message:

10:13:58  [ALTER - 0 row(s), 0.000 secs]  1) [Error Code: 207, SQL State: 42S22]  Invalid column name 'BULK INSERT TmpStList6 FROM ''. 2) [Error Code: 207, SQL State: 42S22]  Invalid column name '' WITH (FIELDTERMINATOR = '","') '. 3) [Error Code: 207, SQL State: 42S22]  Invalid column name 'BULK INSERT TmpStList6 FROM ''. 4) [Error Code: 207, SQL State: 42S22]  Invalid column name '' WITH (FIELDTERMINATOR = ',') '.
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]


test1 is a int field, i used this code to make tables:

CREATE TABLE StudentList
(
 StID int IDENTITY NOT NULL,
 test1 int,
 StFName varchar(50) NOT NULL,
 StLName varchar(50) NOT NULL,
 StEmail varchar(100) NOT NULL,
 OrderID int NOT NULL
)
go
CREATE TABLE TmpStList
(
 test1 int,
 stFName varchar (50) NOT NULL,
 stLName varchar (50) NOT NULL,
 stEmail varchar (100) NOT NULL
)
go

Open in new window

0
team2005Author Commented:
Hi!

Make this stored procedure (create procedure):

SET QUOTED_IDENTIFIER OFF
go
CREATE PROCEDURE ps_StudentList_Import6
@PathFileName varchar(100),
@OrderID integer,
@FileType tinyint
AS

--Step 1: Build Valid BULK INSERT Statement
DECLARE @SQL varchar(2000)
IF @FileType = 1
 BEGIN
  -- Valid format: "John","Smith","john@smith.com"
  SET @SQL = "BULK INSERT TmpStList6 FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = '"",""') "
 END
ELSE
 BEGIN
  -- Valid format: John,Smith,john@smith.com
  SET @SQL = "BULK INSERT TmpStList6 FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = ',') "
 END

--Step 2: Execute BULK INSERT statement
EXEC (@SQL)

--Step 3: INSERT data into final table
INSERT StudentList6 (Stteste,StFName,StLName,StEmail,OrderID)
SELECT  CASE WHEN @FileType = 1 THEN Stteste
            ELSE Stteste
        END,
        CASE WHEN @FileType = 1 THEN SUBSTRING(StFName,2,DATALENGTH(StFName)-1)
             ELSE StFName
        END,
        SUBSTRING(StLName,1,DATALENGTH(StLName)-0),
        CASE WHEN @FileType = 1 THEN SUBSTRING(StEmail,1,DATALENGTH(StEmail)-1)
             ELSE StEmail
        END,
        @OrderID
FROM tmpStList6

--Step 4: Empty temporary table
TRUNCATE TABLE TmpStList6
go

Open in new window


But when i run the stored procedure, (the new created procedure)
i get this error message:

10:39:43  [@CALL - 0 row(s), 0.000 secs]  1) [Error Code: 4864, SQL State: S1000]  Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (Stteste). 2) [Error Code: 4864, SQL State: S1000]  Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (Stteste). 3) [Error Code: 4832, SQL State: S1000]  Bulk load: An unexpected end of file was encountered in the data file. 4) [Error Code: 7399, SQL State: S1000]  The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error. 5) [Error Code: 7330, SQL State: S1000]  Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
0
lcohanDatabase AnalystCommented:
You said
"Hi!

test1 is a colum in table tmpStlist"

but then you said

"tmpStlist

stFName  varchar(50)
stLName varchar(50)
stEmail varchar(100"

so my question remains (just check above) - "Tryed that, but test1 field is emty "

What is test1? Is it a column of tmpStList table? If not it should be as you try to get data from the text field like 1,22,37 ... values right? Should those go into StudentList.test1 int??

If yes add that column to your tmpStlist and change its structure like below:

test1 int,
stFName  varchar(50),
stLName varchar(50),
stEmail varchar(100)
0
team2005Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.