@@ -70,6 +70,12 @@ function Import-DbaCsv {
7070 For proper type inference (int, decimal, datetime2, varchar vs nvarchar), use -SampleRows or -DetectColumnTypes instead.
7171 For production use with specific constraints, create tables manually with appropriate data types, indexes, and constraints.
7272
73+ . PARAMETER NoColumnOptimize
74+ Skips the automatic column size optimization that runs after AutoCreateTable imports.
75+ By default, AutoCreateTable creates nvarchar(MAX) columns and then shrinks them to fit the imported data.
76+ Use this switch when importing multiple CSV files into the same auto-created table, so that later files
77+ with longer values are not rejected due to columns being shrunk to fit only the first file's data.
78+
7379 . PARAMETER Truncate
7480 Removes all existing data from the destination table before importing. The truncate operation is part of the transaction.
7581 Use this for full data refreshes where you want to replace all existing data with the CSV contents.
@@ -537,6 +543,7 @@ function Import-DbaCsv {
537543 [hashtable ]$ColumnMap ,
538544 [switch ]$KeepOrdinalOrder ,
539545 [switch ]$AutoCreateTable ,
546+ [switch ]$NoColumnOptimize ,
540547 [switch ]$NoProgress ,
541548 [switch ]$NoHeaderRow ,
542549 [switch ]$UseFileNameForSchema ,
@@ -1436,7 +1443,7 @@ WHERE c.object_id = OBJECT_ID(@tableName)
14361443 }
14371444
14381445 # Optimize column sizes after commit if we created a fat table
1439- if ($createdFatTable ) {
1446+ if ($createdFatTable -and -not $NoColumnOptimize ) {
14401447 try {
14411448 Optimize-ColumnSize - SqlConn $sqlconn - Schema $schema - Table $table
14421449 } catch {
@@ -1449,7 +1456,7 @@ WHERE c.object_id = OBJECT_ID(@tableName)
14491456 } catch {
14501457 }
14511458 }
1452- } elseif ($completed -and $createdFatTable ) {
1459+ } elseif ($completed -and $createdFatTable -and -not $NoColumnOptimize ) {
14531460 # NoTransaction mode - still optimize if we created a fat table
14541461 try {
14551462 Optimize-ColumnSize - SqlConn $sqlconn - Schema $schema - Table $table
0 commit comments