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.
Synopsis
Efficiently imports very large (and small) CSV files into SQL Server.
Description
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"
Syntax
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>]
Examples
Example: 1
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".
Example: 2
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.
Example: 3
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.
Example: 4
PS C:\> Import-DbaCsv -Path c:\temp\SingleColumn.csv -SqlInstance sql001 -Database markets -Table TempTable -SingleColumn
Imports the single column CSV into TempTable
Example: 5
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.
Example: 6
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
Example: 7
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.
Example: 8
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'.
Example: 9
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'.
Example: 10
PS C:\> $columns = @{>> Text = 'FirstName'>> Number = 'PhoneNumber'>> }PS C:\> Import-DbaCsv -Path c:\temp\supersmall.csv -SqlInstance sql2016 -Database tempdb -ColumnMap $columns
The CSV column 'Text' is inserted into SQL column 'FirstName' and CSV column Number is inserted into the SQL Column 'PhoneNumber'. All other columns are ignored and therefore null or default values.
Required Parameters
-SqlInstance
The SQL Server Instance to import data into.
Alias | |
Required | True |
Pipeline | false |
Default Value |
-Database
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 |
Optional Parameters
-Path
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 |
-SqlCredential
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 |
-Table
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 |
-Schema
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 |
-Truncate
If this switch is enabled, the destination table will be truncated prior to import.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-Delimiter
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 | , |
-SingleColumn
Specifies that the file contains a single column of data. Otherwise, the delimiter check bombs.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-BatchSize
Specifies the batch size for the import. Defaults to 50000.
Alias | |
Required | False |
Pipeline | false |
Default Value | 50000 |
-NotifyAfter
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 |
-TableLock
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 |
-CheckConstraints
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 |
-FireTriggers
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 |
-KeepIdentity
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 |
-KeepNulls
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 |
-Column
Import only specific columns. To remap column names, use the ColumnMap.
Alias | |
Required | False |
Pipeline | false |
Default Value |
-ColumnMap
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 |
-KeepOrdinalOrder
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 |
-AutoCreateTable
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 |
-NoProgress
The progress bar is pretty but can slow down imports. Use this parameter to quietly import.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-NoHeaderRow
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 |
-UseFileNameForSchema
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 |
-Quote
Defines the default quote character wrapping every field.
Default: double-quotes
Alias | |
Required | False |
Pipeline | false |
Default Value | " |
-Escape
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 | " |
-Comment
Defines the default comment character indicating that a line is commented out.
Default: hashtag
Alias | |
Required | False |
Pipeline | false |
Default Value | # |
-TrimmingOption
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 |
-BufferSize
Defines the default buffer size. The default BufferSize is 4096.
Alias | |
Required | False |
Pipeline | false |
Default Value | 4096 |
-ParseErrorAction
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 |
-Encoding
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 |
-NullValue
The value which denotes a DbNull-value.
Alias | |
Required | False |
Pipeline | false |
Default Value |
-MaxQuotedFieldLength
The maximum length (in bytes) for any quoted field.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
-SkipEmptyLine
Skip empty lines.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-SupportsMultiline
Indicates if the importer should support multiline fields.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-UseColumnDefault
Use the column default values if the field is not in the record.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-NoTransaction
Do not use a transaction when performing the import.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
-EnableException
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 |
-WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
-Confirm
Prompts you for confirmation before executing any changing operations within the command.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |