Search code examples
sql-serverpowershellcsvbulkinsertsqlbulkcopy

Importing CSV to SQL Server using bulkcopy


I'm trying to import my CSV files into SQL Server. I found this code and it works perfectly and very fast:

# Database variables
$sqlserver = "servername"
$database = "datebasename"
$table = "tablename"

# CSV variables
$csvfile = "F:\TestNA\fin_product4.csv"
$csvdelimiter = ";"
$firstRowColumnNames = $true

################### No need to modify anything below ###################
Write-Host "Script started..."
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
[void][Reflection.Assembly]::LoadWithPartialName("System.Data")
[void][Reflection.Assembly]::LoadWithPartialName("System.Data.SqlClient")

# 50k worked fastest and kept memory usage to a minimum
$batchsize = 50000

# Build the sqlbulkcopy connection, and set the timeout to infinite
$connectionstring = "Data Source=$sqlserver;Integrated Security=true;Initial Catalog=$database;"
$bulkcopy = New-Object Data.SqlClient.SqlBulkCopy($connectionstring, [System.Data.SqlClient.SqlBulkCopyOptions]::TableLock)
$bulkcopy.DestinationTableName = $table
$bulkcopy.bulkcopyTimeout = 0
$bulkcopy.batchsize = $batchsize

# Create the datatable, and autogenerate the columns.
$datatable = New-Object System.Data.DataTable

# Open the text file from disk
$reader = New-Object System.IO.StreamReader($csvfile)
$columns = (Get-Content $csvfile -First 1).Split($csvdelimiter)
if ($firstRowColumnNames -eq $true) { $null = $reader.readLine() }

#foreach ($column in $columns) {
# $null = $datatable.Columns.Add()
#}

$col1 = New-Object system.Data.DataColumn fin_product_rk,([datetime])
$col2 = New-Object system.Data.DataColumn fin_product_id,([datetime])
$datatable.columns.add($col1)
$datatable.columns.add($col2)

# Read in the data, line by line
while (($line = $reader.ReadLine()) -ne $null) {
$null = $datatable.Rows.Add($line.Split($csvdelimiter))
$i++; if (($i % $batchsize) -eq 0) {
$bulkcopy.WriteToServer($datatable)
Write-Host "$i rows have been inserted in $($elapsed.Elapsed.ToString())."
$datatable.Clear()
}
}

# Add in all the remaining rows since the last clear
if($datatable.Rows.Count -gt 0) {
$bulkcopy.WriteToServer($datatable)
$datatable.Clear()
}

# Clean Up
$reader.Close(); $reader.Dispose()
$bulkcopy.Close(); $bulkcopy.Dispose()
$datatable.Dispose()

Write-Host "Script complete. $i rows have been inserted into the database."
Write-Host "Total Elapsed Time: $($elapsed.Elapsed.ToString())"
# Sometimes the Garbage Collector takes too long to clear the huge datatable.
[System.GC]::Collect()

The problem is: it works for the standard latin encoding, but I have CSVs in UTF-8 and Windows-1251 encodings.

What and where should I add to change the encoding in this code?

I don't know the programming language that used to write this code so I can't do it myself, I would be happy if someone can help!

Thank you!

Update: CSV example:

product;product_id;product_nm;dttm
220;text;некоторый текст;12JAN2021:18:03:41.000000
220;text;некоторый текст;1JAN2021:18:03:41.000000
564;text;некоторый текст;16JAN2021:18:03:41.000000

Solution

  • Here is a solution in T-SQL.

    It is very concise, one single statement, in comparison with powershell.

    Notable points:

    • BULK INSERT parameter CODEPAGE = '65001' specifies UTF-8.
    • product_nm NVARCHAR(100) column holds UNICODE characters from the file.

    SQL

    USE tempdb;
    GO
    
    DROP TABLE IF EXISTS dbo.tbl;
    
    CREATE TABLE dbo.tbl (
        product VARCHAR(10),
        product_id VARCHAR(30),
        product_nm NVARCHAR(100),
        dttm VARCHAR(50)
    );
    
    BULK INSERT dbo.tbl
    FROM 'e:\Temp\Faenno_2.csv'
    WITH (FORMAT='CSV'
       , DATAFILETYPE = 'char' -- { 'char' | 'native' | 'widechar' | 'widenative' } 
       , FIELDTERMINATOR = ';'
       , ROWTERMINATOR = '\n'
       , FIRSTROW = 2 
       , CODEPAGE = '65001');
    
    -- test
    SELECT * FROM dbo.tbl;
    

    Output

    +---------+------------+-----------------+---------------------------+
    | product | product_id |   product_nm    |           dttm            |
    +---------+------------+-----------------+---------------------------+
    |     220 | text       | некоторый текст | 12JAN2021:18:03:41.000000 |
    |     220 | text       | некоторый текст | 1JAN2021:18:03:41.000000  |
    |     564 | text       | некоторый текст | 16JAN2021:18:03:41.000000 |
    +---------+------------+-----------------+---------------------------+