Author | Chrissy LeMaire (@cl), netnerds.net |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out Import-DbaCsv on GitHub.
Want to see the Bill Of Health for this command? Check out Import-DbaCsv.
Efficiently imports very large (and small) CSV files into SQL Server.
Import-DbaCsv takes advantage of .NET's super fast SqlBulkCopy class to import CSV files into SQL Server.
The entire import is performed within a transaction, so if a failure occurs or the script is aborted, no changes will persist.
If the table or view specified does not exist and -AutoCreateTable, it will be automatically created using slow and inefficient but accommodating data types.
This importer supports fields spanning multiple lines. The only restriction is that they must be quoted, otherwise it would not be possible to distinguish between malformed data and multi-line values.
Able to read gzip compressed CSV files if the filename ends with ".csv.gz"
Import-DbaCsv
[[-Path] <Object[]>]
[-SqlInstance] <DbaInstanceParameter[]>
[[-SqlCredential] <PSCredential>]
[-Database] <String>
[[-Table] <String>]
[[-Schema] <String>]
[-Truncate]
[[-Delimiter] <Char>]
[-SingleColumn]
[[-BatchSize] <Int32>]
[[-NotifyAfter] <Int32>]
[-TableLock]
[-CheckConstraints]
[-FireTriggers]
[-KeepIdentity]
[-KeepNulls]
[[-Column] <String[]>]
[[-ColumnMap] <Hashtable>]
[-KeepOrdinalOrder]
[-AutoCreateTable]
[-NoProgress]
[-NoHeaderRow]
[-UseFileNameForSchema]
[[-Quote] <Char>]
[[-Escape] <Char>]
[[-Comment] <Char>]
[[-TrimmingOption] <String>]
[[-BufferSize] <Int32>]
[[-ParseErrorAction] <String>]
[[-Encoding] <String>]
[[-NullValue] <String>]
[[-MaxQuotedFieldLength] <Int32>]
[-SkipEmptyLine]
[-SupportsMultiline]
[-UseColumnDefault]
[-NoTransaction]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> Import-DbaCsv -Path C:\temp\housing.csv -SqlInstance sql001 -Database markets
Imports the entire comma-delimited housing.csv to the SQL "markets" database on a SQL Server named sql001, using the first row as column names.
Since a table name was not specified, the table name is automatically determined from filename as "housing".
PS C:\> Import-DbaCsv -Path .\housing.csv -SqlInstance sql001 -Database markets -Table housing -Delimiter "`t" -NoHeaderRow
Imports the entire tab-delimited housing.csv, including the first row which is not used for colum names, to the SQL markets database, into the housing table, on a SQL Server named sql001.
PS C:\> Import-DbaCsv -Path C:\temp\huge.txt -SqlInstance sqlcluster -Database locations -Table latitudes -Delimiter "|"
Imports the entire pipe-delimited huge.txt to the locations database, into the latitudes table on a SQL Server named sqlcluster.
PS C:\> Import-DbaCsv -Path c:\temp\SingleColumn.csv -SqlInstance sql001 -Database markets -Table TempTable -SingleColumn
Imports the single column CSV into TempTable
PS C:\> Get-ChildItem -Path \\FileServer\csvs | Import-DbaCsv -SqlInstance sql001, sql002 -Database tempdb -AutoCreateTable
Imports every CSV in the \FileServer\csvs path into both sql001 and sql002's tempdb database. Each CSV will be imported into an automatically determined table name.
PS C:\> Get-ChildItem -Path \\FileServer\csvs | Import-DbaCsv -SqlInstance sql001, sql002 -Database tempdb -AutoCreateTable -WhatIf
Shows what would happen if the command were to be executed
PS C:\> Import-DbaCsv -Path c:\temp\dataset.csv -SqlInstance sql2016 -Database tempdb -Column Name, Address, Mobile
Import only Name, Address and Mobile even if other columns exist. All other columns are ignored and therefore null or default values.
PS C:\> Import-DbaCsv -Path C:\temp\schema.data.csv -SqlInstance sql2016 -database tempdb -UseFileNameForSchema
Will import the contents of C:\temp\schema.data.csv to table 'data' in schema 'schema'.
PS C:\> Import-DbaCsv -Path C:\temp\schema.data.csv -SqlInstance sql2016 -database tempdb -UseFileNameForSchema -Table testtable
Will import the contents of C:\temp\schema.data.csv to table 'testtable' in schema 'schema'.
PS C:\> $columns = @{
>> Text = 'FirstName'
>> Number = 'PhoneNumber'
>> }
PS C:\> Import-DbaCsv -Path c:\temp\supersmall.csv -SqlInstance sql2016 -Database tempdb -ColumnMap $columns
The CSV field 'Text' is inserted into SQL column 'FirstName' and CSV field Number is inserted into the SQL Column 'PhoneNumber'. All other columns are ignored and therefore null or default values.
PS C:\> $columns = @{
>> 0 = 'FirstName'
>> 1 = 'PhoneNumber'
>> }
PS C:\> Import-DbaCsv -Path c:\temp\supersmall.csv -SqlInstance sql2016 -Database tempdb -NoHeaderRow -ColumnMap $columns
If the CSV has no headers, passing a ColumnMap works when you have as the key the ordinal of the column (0-based).
In this example the first CSV field is inserted into SQL column 'FirstName' and the second CSV field is inserted into the SQL Column 'PhoneNumber'.
The SQL Server Instance to import data into.
Alias | |
Required | True |
Pipeline | false |
Default Value |
Specifies the name of the database the CSV will be imported into. Options for this this parameter are auto-populated from the server.
Alias | |
Required | True |
Pipeline | false |
Default Value |
Specifies path to the CSV file(s) to be imported. Multiple files may be imported at once.
Alias | Csv,FullPath |
Required | False |
Pipeline | true (ByValue) |
Default Value |
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies the SQL table or view where CSV will be imported into.
If a table name is not specified, the table name will be automatically determined from the filename.
If the table specified does not exist and -AutoCreateTable, it will be automatically created using slow and inefficient but accommodating data types.
If the automatically generated table datatypes do not work for you, please create the table prior to import.
If you want to import specific columns from a CSV, create a view with corresponding columns.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies the schema in which the SQL table or view where CSV will be imported into resides. Default is dbo.
If a schema does not currently exist, it will be created, after a prompt to confirm this. Authorization will be set to dbo by default.
This parameter overrides -UseFileNameForSchema.
Alias | |
Required | False |
Pipeline | false |
Default Value |
If this switch is enabled, the destination table will be truncated prior to import.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Specifies the delimiter used in the imported file(s). If no delimiter is specified, comma is assumed.
Valid delimiters are 't
, '|', ';',' ' and ',' (tab, pipe, semicolon, space, and comma).
Alias | |
Required | False |
Pipeline | false |
Default Value | , |
Specifies that the file contains a single column of data. Otherwise, the delimiter check bombs.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Specifies the batch size for the import. Defaults to 50000.
Alias | |
Required | False |
Pipeline | false |
Default Value | 50000 |
Specifies the import row count interval for reporting progress. A notification will be shown after each group of this many rows has been imported.
Alias | |
Required | False |
Pipeline | false |
Default Value | 50000 |
If this switch is enabled, the SqlBulkCopy option to acquire a table lock will be used.
Per Microsoft "Obtain a bulk update lock for the duration of the bulk copy operation. When not
specified, row locks are used."
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If this switch is enabled, the SqlBulkCopy option to check constraints will be used.
Per Microsoft "Check constraints while data is being inserted. By default, constraints are not checked."
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If this switch is enabled, the SqlBulkCopy option to allow insert triggers to be executed will be used.
Per Microsoft "When specified, cause the server to fire the insert triggers for the rows being inserted into the database."
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If this switch is enabled, the SqlBulkCopy option to keep identity values from the source will be used.
Per Microsoft "Preserve source identity values. When not specified, identity values are assigned by the destination."
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If this switch is enabled, the SqlBulkCopy option to keep NULL values in the table will be used.
Per Microsoft "Preserve null values in the destination table regardless of the settings for default values. When not specified, null values are replaced by default values where applicable."
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Import only specific columns. To remap column names, use the ColumnMap.
Alias | |
Required | False |
Pipeline | false |
Default Value |
By default, the bulk copy tries to automap columns. When it doesn't work as desired, this parameter will help. Check out the examples for more information.
Alias | |
Required | False |
Pipeline | false |
Default Value |
By default, the importer will attempt to map exact-match columns names from the source document to the target table. Using this parameter will keep the ordinal order instead.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Creates a table if it does not already exist. The table will be created with sub-optimal data types such as nvarchar(max)
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
The progress bar is pretty but can slow down imports. Use this parameter to quietly import.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
By default, the first row is used to determine column names for the data being imported.
Use this switch if the first row contains data and not column names.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
If this switch is enabled, the script will try to find the schema name in the input file by looking for a period (.) in the file name.
If used with the -Table parameter you may still specify the target table name. If -Table is not used the file name after the first period will
be used for the table name.
For example test.data.csv will import the csv contents to a table in the test schema.
If it finds one it will use the file name up to the first period as the schema. If there is no period in the filename it will default to dbo.
If a schema does not currently exist, it will be created, after a prompt to confirm this. Authorization will be set to dbo by default.
This behaviour will be overridden if the -Schema parameter is specified.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Defines the default quote character wrapping every field.
Default: double-quotes
Alias | |
Required | False |
Pipeline | false |
Default Value | " |
Defines the default escape character letting insert quotation characters inside a quoted field.
The escape character can be the same as the quote character.
Default: double-quotes
Alias | |
Required | False |
Pipeline | false |
Default Value | " |
Defines the default comment character indicating that a line is commented out.
Default: hashtag
Alias | |
Required | False |
Pipeline | false |
Default Value | # |
Determines which values should be trimmed. Default is "None". Options are All, None, UnquotedOnly and QuotedOnly.
Alias | |
Required | False |
Pipeline | false |
Default Value | None |
Accepted Values | All,None,UnquotedOnly,QuotedOnly |
Defines the default buffer size. The default BufferSize is 4096.
Alias | |
Required | False |
Pipeline | false |
Default Value | 4096 |
By default, the parse error action throws an exception and ends the import.
You can also choose AdvanceToNextLine which basically ignores parse errors.
Alias | |
Required | False |
Pipeline | false |
Default Value | ThrowException |
Accepted Values | AdvanceToNextLine,ThrowException |
By default, set to UTF-8.
The encoding of the file.
Alias | |
Required | False |
Pipeline | false |
Default Value | UTF8 |
Accepted Values | ASCII,BigEndianUnicode,Byte,String,Unicode,UTF7,UTF8,Unknown |
The value which denotes a DbNull-value.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The maximum length (in bytes) for any quoted field.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
Skip empty lines.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Indicates if the importer should support multiline fields.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Use the column default values if the field is not in the record.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Do not use a transaction when performing the import.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Shows what would happen if the command were to run. No actions are actually performed.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
Prompts you for confirmation before executing any changing operations within the command.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |