Skip to content

Import-DbaCsv, Export-DbaCsv - Normalize table/schema names via Get-ObjectNameParts#10306

Merged
potatoqualitee merged 3 commits intodevelopmentfrom
claude/issue-9010-20260403-1523
Apr 9, 2026
Merged

Import-DbaCsv, Export-DbaCsv - Normalize table/schema names via Get-ObjectNameParts#10306
potatoqualitee merged 3 commits intodevelopmentfrom
claude/issue-9010-20260403-1523

Conversation

@andreasjordan
Copy link
Copy Markdown
Collaborator

@andreasjordan andreasjordan commented Apr 3, 2026

Fixes inconsistency where commands with bracketed table names (e.g. [My.Table]) failed because sys.tables stores bare names without brackets.

  • Import-DbaCsv: normalize $table/$schema with Get-ObjectNameParts before using them in parameterized metadata queries
  • Export-DbaCsv: replace naive dot-split regex with Get-ObjectNameParts

Generated with Claude Code

Edit of andreasjordan:
Fixes part of #9010 - more PRs need to follow. So we keep the issue open but limit this PR to the DbaCsv commands. After this is merged we can find other commands that need the same fix.

IMPORTANT: This is a breaking change so maybe merge later together with other breaking changes and release a new version 2.8.0

…bjectNameParts

Fixes inconsistency where commands with bracketed table names (e.g. [My.Table])
failed because sys.tables stores bare names without brackets.

- Import-DbaCsv: normalize $table/$schema with Get-ObjectNameParts before using
  them in parameterized metadata queries (sys.tables, sys.schemas,
  INFORMATION_SCHEMA.COLUMNS). Also handles two-part schema.table input.
- Export-DbaCsv: replace naive dot-split regex with Get-ObjectNameParts so that
  names like [Gross.Table.Name] are correctly parsed instead of being split on
  the dot inside the brackets.

Closes #9010

(do *Csv*)

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Copy Markdown
Collaborator Author

@claude please review this PR carefully as a lot of tests fail.

Test-Output:

Running C:\github\dbatools\tests\Export-DbaCsv.Tests.ps1 ...[07:33:24][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:27][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:27][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:27][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:27][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:28][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:28][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:29][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:30][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:31][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:31][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
[07:33:31][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
Completed C:\github\dbatools\tests\Export-DbaCsv.Tests.ps1 in 17255ms
Uploading artifact TestFailureSummary_Pester5_1.json (1,573,538 bytes)...100%
appveyor.pester: Test failed with 12 failed tests. Retrying (attempt 2 of 3)...


Failed tests summary:
Path           : Export-DbaCsv/Compression options (issue #8646)/exports with each compression level
Name           : It exports with each compression level
Result         : Failed
Message        : ErrorRecord: Expected 3, because compression level Fastest should export all rows, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, because compression level Fastest should export all rows, but got $null.
Path           : Export-DbaCsv/Compression options (issue #8646)/exports with each compression type
Name           : It exports with each compression type
Result         : Failed
Message        : ErrorRecord: Expected 3, because compression type None should export all rows, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, because compression type None should export all rows, but got $null.
Path           : Export-DbaCsv/Works as expected/auto-detects compression from file extension
Name           : It auto-detects compression from file extension
Result         : Failed
Message        : ErrorRecord: Expected 'GZip', but got $null.
StackTrace     : 
RawErrorRecord : Expected 'GZip', but got $null.
Path           : Export-DbaCsv/Works as expected/exports table data to CSV file
Name           : It exports table data to CSV file
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/exports with Always quoting behavior
Name           : It exports with Always quoting behavior
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/exports with custom date format
Name           : It exports with custom date format
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/exports with custom delimiter
Name           : It exports with custom delimiter
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/exports with Deflate compression
Name           : It exports with Deflate compression
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/exports with GZip compression (issue #8646)
Name           : It exports with GZip compression (issue #8646)
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/exports without header when -NoHeader is specified
Name           : It exports without header when -NoHeader is specified
Result         : Failed
Message        : ErrorRecord: Expected 3, but got $null.
StackTrace     : 
RawErrorRecord : Expected 3, but got $null.
Path           : Export-DbaCsv/Works as expected/prevents overwriting with -NoClobber
Name           : It prevents overwriting with -NoClobber
Result         : Failed
Message        : ErrorRecord: Expected like wildcard '*already exists*' to match [07:34:30][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO 
                 statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name., but it did not match.
StackTrace     : 
RawErrorRecord : Expected like wildcard '*already exists*' to match [07:34:30][Export-DbaCsv] Failed to export data from APPVEYOR-VM\sql2022 | An object or column name is missing or empty. For SELECT INTO statements, verify each 
                 column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name., but it did not match.
Path           : Export-DbaCsv/Works as expected/returns proper result object with performance metrics
Name           : It returns proper result object with performance metrics
Result         : Failed
Message        : ErrorRecord: Expected 'C:\Temp\Export-DbaCsv-59059615\metrics.csv', but got $null.
StackTrace     : 
RawErrorRecord : Expected 'C:\Temp\Export-DbaCsv-59059615\metrics.csv', but got $null.

@claude
Copy link
Copy Markdown

claude bot commented Apr 9, 2026

Claude finished @andreasjordan's task in 1m 38s —— View job


Review & Fix Complete

  • Read changed files (Export-DbaCsv.ps1, Import-DbaCsv.ps1)
  • Investigate Get-ObjectNameParts behavior
  • Analyze test failures and root cause
  • Fix the bug in Export-DbaCsv.ps1 and push

Root Cause

Export-DbaCsv.ps1, line 305 — stale variable reference left over from an incomplete rename:

# After the if/elseif/else block that sets $tableName via $parsedTable...
$tableName = $nameParts.Name   # ← $nameParts no longer exists → evaluates to $null
$sqlToExecute = "SELECT * FROM [$schemaName].[$tableName]"
# Result: SELECT * FROM [dbo].[]  ← SQL Server rejects this

When the variable was renamed from $nameParts$parsedTable, this one trailing line was missed. Since $nameParts is undefined, $nameParts.Name is $null, so $tableName gets silently overwritten with $null. Every query then becomes SELECT * FROM [schema].[], producing the SQL Server error:

"An object or column name is missing or empty ... Aliases defined as "" or [] are not allowed."

This affected all 12 test cases because every Export-DbaCsv call using -Table hit this path.

Fix Applied

Removed the stale line — $tableName is already correctly set inside each branch of the if/elseif/else block. Commit: 2507ad5

Import-DbaCsv.ps1 changes

Those changes look correct — $parsedTable is used consistently throughout, no stale references.
| Branch

…y table name

Rename was incomplete: \$nameParts.Name on the line after the if/elseif/else
block was not updated to \$parsedTable, overwriting \$tableName with \$null and
producing SQL error "An object or column name is missing or empty".

(do Export-DbaCsv)

Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
@andreasjordan
Copy link
Copy Markdown
Collaborator Author

Ok, tests pass now.

@andreasjordan andreasjordan marked this pull request as ready for review April 9, 2026 13:03
@potatoqualitee potatoqualitee merged commit 74a2d1a into development Apr 9, 2026
14 checks passed
@potatoqualitee potatoqualitee deleted the claude/issue-9010-20260403-1523 branch April 9, 2026 22:03
@potatoqualitee
Copy link
Copy Markdown
Member

🔥 🔥 🔥 🔥 🔥 🔥 🔥 🔥 thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants