February 10, 2015 at 7:22 am
I have written bulk insert and doing this manully but need to automate it and file location changes evertime .
BULK INSERT dbo.alpha
FROM 'C:\posted 2015-01-16\HOSPITAL\HOSPFY2010\hosp_2010_ALPHA.CSV'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '',
ROWS_PER_BATCH = 10000,
TABLOCK
)
BULK INSERT dbo.RPT
FROM 'C:\posted 2015-01-16\HOSPITAL\HOSPFY2010\hosp_2010_RPT.CSV'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '',
ROWS_PER_BATCH = 10000,
TABLOCK
)
Please let me know how to proceed
February 10, 2015 at 8:15 am
well, what is the pattern that you can use to find the file/filepath if it's dynamic? is it int he same folder + different file name?
you can use xp_cmdshell to run the dir command, and use the results + dynamic SQL, would that work?
Lowell
February 10, 2015 at 8:27 am
The folder name and file name changes everytime
Folder name would be - posted 2015-01-16
Sub Folder - Hospital
sub sub folder- HOSPFY2010
Files -
hosp_2010_A.CSV
hosp_2010_R.CSV
hosp_2010_V.CSV
hosp_2010_D.CSV
All file go to different tables
like
dbo.a
dbo.r
dbo.v
dbo.D
everytime need to create new database and new tables
February 10, 2015 at 8:30 am
so, what is the pattern then? any file that ends in [_A.csv] goes into the [A] table, for example?
Lowell
February 10, 2015 at 8:37 am
a quick example adapted form my snippets:
declare @filename varchar(255),
@path varchar(255),
@sql varchar(8000),
@cmd varchar(1000)
--get the list of files to process:
--#########################################
SET @path = 'C:\Hospital\'
SET @cmd = 'dir C:\Hospital\*_A.csv /b/s'
INSERT INTO ALLFILENAMES(WHICHFILE)
EXEC Master..xp_cmdShell @cmd
UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null
--#########################################
--cursor loop to process all "_A.csv" files
declare c1 cursor for SELECT WHICHPATH,WHICHFILE FROM ALLFILENAMES where WHICHFILE like '%_A.csv%'
open c1
fetch next from c1 into @path,@filename
While @@fetch_status <> -1
begin
--bulk insert won't take a variable name, so make a sql and execute it instead:
set @sql = 'BULK INSERT dbo.[A] FROM ''' @filename + ''' '
+ ' WITH (
DATAFILETYPE = ''char'',
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n'',
FIRSTROW = 2
) '
print @sql
exec (@sql)
fetch next from c1 into @path,@filename
end
close c1
deallocate c1
Lowell
February 10, 2015 at 9:20 am
While I'm a great lover of xp_CmdShell, there's no need for it just to get the file names. You can use xp_DirTree "pathname",1,1 to get a list of the file names, if that's all you need to get.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 10, 2015 at 11:18 am
Thank you , This really helped me for loading
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply