Sql-server – Upload multiple csv file to multiple tables sqlServer

csvrestoresql servertable

I have over than 200 csv files that each of them is diffrent table.

I create now a new DB and want to upload them all so I will have all the table.

I not accessable to old DB to backup in diffrent way.

I looking for a simple way to do it fast (script/gui/ 3pary tool)

I found I can do it with sqlServer gui but I cant upload there multiple files, only 1 by 1, so it will takes hours.

I saw this post How to upload multiple csv files to sql server without constrains but it not help me, not found there any solution.

Best Answer

I you want to use powershell to upload these CSV files, without having to code/script, take a look dbatools.io. Look for the cmdlet Import-DbaCsv

All the documentation is available on the site. Just download the tools from github or the powershell gallery.

You can use Get-ChildItem cmdlet to get all the CSV files in a folder and use the pipeline to send the files to Import-DbaCsv (optionally specifying the -AutoCreateTable flag). For example:

Get-ChildItem -Path "C:\Path\To\your\CSVs" | ForEach-Object {
    Import-DbaCsv -Csv $_.FullName -SQLInstance "localhost" -Database "yourdb" -AutoCreateTable
}

i ❤️ pwsh