SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Importing Multiple pipe delimiter files at once


Importing Multiple pipe delimiter files at once

Author
Message
jeff.cohen 8758
jeff.cohen 8758
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 2

Hello everyone,

I am able to import one pipe delimited file at a time with the script below but can't get it to import multiple files at once. I tried to use a wildcard in the file name but it won't allow it (ie. *.txt or eJ*.text).

Any assistance that you can provide would be greatly appreciated.

Thank you,

BULK INSERT databasename.tablename
FROM 'C:\temp\EJ_Detail_file.txt'
WITH
(
FIELDTERMINATOR ='|',
FIRSTROW = 2,
ROWTERMINATOR ='\n'
)


Joe Torre
Joe Torre
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3126 Visits: 840
Try:

DECLARE
@DirTree table (subdirectory varchar(255), depth tinyint, is_file bit);
DECLARE
@path varchar(255)='C:\temp'
, @bs varchar(1)='\'
, @fullpath varchar(255)
, @sql nvarchar(MAX);
INSERT @DirTree
( subdirectory
, depth
, is_file )
EXEC sys.xp_dirtree @path, 1, 1 ;
DECLARE FILES CURSOR FOR
SELECT @path+@bs+dt.subdirectory
FROM @DirTree dt;
OPEN FILES
FETCH FILES INTO @fullpath;
WHILE @@fetch_Status = 0
BEGIN
SET @sql =
'BULK INSERT databasename.tablename
FROM '+@fullpath+'
WITH
(
FIELDTERMINATOR =''|'',
FIRSTROW = 2,
ROWTERMINATOR =''\n''
)'
EXEC(@sql);
FETCH FILES INTO @fullpath;
END
CLOSE FILES;
DEALLOCATE FILES;


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search