Import Multiple CSV Files to SQL Server from a Folder -


i have folder called "dump." folder consists of variouos .csv files. folder location 'c:\dump'

i want import contents of these files sql server. want rough code along proper comments understand it.

i have tried few codes found on net. haven't quite worked out me strange reason.


the steps have are

step 1: copy file names in folder table

step 2: iterate through table , copy data files using bulk insert.


someone please me out on one. lot in advance :)

    --bulk insert multiple files folder       --a table loop thru filenames drop table allfilenames     create table allfilenames(whichpath varchar(255),whichfile varchar(255))      --some variables     declare @filename varchar(255),             @path     varchar(255),             @sql      varchar(8000),             @cmd      varchar(1000)       --get list of files process:     set @path = 'c:\dump\'     set @cmd = 'dir ' + @path + '*.csv /b'     insert  allfilenames(whichfile)     exec master..xp_cmdshell @cmd     update allfilenames set whichpath = @path whichpath null       --cursor loop     declare c1 cursor select whichpath,whichfile allfilenames whichfile '%.csv%'     open c1     fetch next c1 @path,@filename     while @@fetch_status <> -1       begin       --bulk insert won't take variable name, make sql , execute instead:        set @sql = 'bulk insert temp ''' + @path + @filename + ''' '            + '     (                     fieldterminator = '','',                     rowterminator = ''\n'',                     firstrow = 2                  ) '     print @sql     exec (@sql)        fetch next c1 @path,@filename       end     close c1     deallocate c1       --extras      --delete allfilenames whichfile null     --select * allfilenames     --drop table allfilenames 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -