Avatar of team2005
team2005
 asked on

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

Microsoft DevelopmentMicrosoft SQL Server

Avatar of undefined
Last Comment
team2005

8/22/2022 - Mon
EugeneZ

create new or recreate TmpStList table with just 3 columns
instead of 4
team2005

ASKER
Hi!

Tryed that, but test1 field is emty ?
team2005

ASKER
Hi!

Need help solving this issue ASAP.

I am stuck...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
lcohan

"Tryed that, but test1 field is emty "

What is test1? Is it a column of tmpStList table? can you post tmpStList table structure?
team2005

ASKER
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
EugeneZ

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)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
team2005

ASKER
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
EugeneZ

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?
ASKER CERTIFIED SOLUTION
EugeneZ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
EugeneZ

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
team2005

ASKER
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

team2005

ASKER
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)".
lcohan

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)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
team2005

ASKER
thanks