Skip to content

Commit 7d12a86

Browse files
Clarify type inference and column optimization in docs
Updated documentation for AutoCreateTable, SampleRows, and DetectColumnTypes parameters to clarify how column sizes and types are inferred and optimized. Added details about nvarchar/varchar usage and recommendations for production scenarios. (do Import-DbaCsv)
1 parent 4efc802 commit 7d12a86

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

public/Import-DbaCsv.ps1

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ function Import-DbaCsv {
6565
6666
.PARAMETER AutoCreateTable
6767
Creates the destination table automatically if it doesn't exist, using nvarchar(max) for all columns.
68-
Convenient for quick imports or testing, but for production use, create tables manually with appropriate data types, indexes, and constraints.
68+
After import, column sizes are automatically optimized based on actual data lengths (nvarchar(MAX) -> nvarchar(16/32/64/etc.)).
69+
70+
For proper type inference (int, decimal, datetime2, varchar vs nvarchar), use -SampleRows or -DetectColumnTypes instead.
71+
For production use with specific constraints, create tables manually with appropriate data types, indexes, and constraints.
6972
7073
.PARAMETER Truncate
7174
Removes all existing data from the destination table before importing. The truncate operation is part of the transaction.
@@ -232,7 +235,8 @@ function Import-DbaCsv {
232235
233236
Example: -SampleRows 10000 samples the first 10,000 rows to determine types like
234237
int, bigint, decimal(p,s), datetime2, bit, uniqueidentifier, or varchar(n)/nvarchar(n)
235-
with appropriate lengths.
238+
with appropriate lengths. Unlike plain -AutoCreateTable, this can infer varchar for
239+
ASCII-only string data, saving storage space.
236240
237241
.PARAMETER DetectColumnTypes
238242
Enables smart type detection by scanning the entire CSV file before import.
@@ -246,7 +250,8 @@ function Import-DbaCsv {
246250
Implies -AutoCreateTable behavior for type detection.
247251
248252
Detected types include: int, bigint, decimal(p,s), datetime2, bit,
249-
uniqueidentifier, varchar(n), and nvarchar(n) when Unicode is detected.
253+
uniqueidentifier, varchar(n) for ASCII-only strings, and nvarchar(n) when Unicode is detected.
254+
This provides optimal storage by using varchar where possible.
250255
251256
.PARAMETER Parallel
252257
Enables parallel processing for improved performance on large files.
@@ -478,8 +483,9 @@ function Import-DbaCsv {
478483
PS C:\> Import-DbaCsv -Path C:\temp\quickload.csv -SqlInstance sql001 -Database tempdb -Table QuickData -AutoCreateTable
479484
480485
Imports quickload.csv with AutoCreateTable. After import completes, column sizes are automatically
481-
optimized by querying actual max lengths and altering columns from nvarchar(MAX) to appropriate sizes.
482-
This "just works" approach imports first, then optimizes - no risk of import failures.
486+
optimized by querying actual max lengths and altering columns from nvarchar(MAX) to padded sizes
487+
like nvarchar(16), nvarchar(32), nvarchar(64), etc. The nvarchar type is preserved to avoid any
488+
risk of data loss from Unicode conversion. For ASCII->varchar conversion, use -SampleRows or -DetectColumnTypes.
483489
#>
484490
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
485491
param (

0 commit comments

Comments
 (0)