Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions public/Export-DbaCsv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,20 @@ function Export-DbaCsv {
if ($PSBoundParameters.Query) {
$sqlToExecute = $Query
} elseif ($PSBoundParameters.Table) {
# Parse table name for schema using Get-ObjectNameParts to correctly handle bracketed names like [Gross.Table.Name]
$nameParts = Get-ObjectNameParts -ObjectName $Table
if ($nameParts.Schema) {
$schemaName = $nameParts.Schema
# Parse table name using Get-ObjectNameParts so that bracketed names
# (e.g. [My.Table]) and two-part names (e.g. schema.[My.Table]) are
# handled correctly instead of relying on a naive dot-split regex.
$parsedTable = Get-ObjectNameParts -ObjectName $Table
if ($parsedTable.Parsed -and $parsedTable.Schema) {
$schemaName = $parsedTable.Schema
$tableName = $parsedTable.Name
} elseif ($parsedTable.Parsed) {
$schemaName = "dbo"
$tableName = $parsedTable.Name
} else {
$schemaName = "dbo"
$tableName = $Table
}
$tableName = $nameParts.Name
$sqlToExecute = "SELECT * FROM [$schemaName].[$tableName]"
}

Expand Down
19 changes: 19 additions & 0 deletions public/Import-DbaCsv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,25 @@ WHERE c.object_id = OBJECT_ID(@tableName)
}
}

# Normalize table and schema names using Get-ObjectNameParts.
# This handles bracketed names (e.g. [My.Table]) and two-part names (e.g. schema.[My.Table]).
# sys.tables/sys.schemas store bare names without brackets, so we must strip them
# before using the values in parameterized metadata queries.
$parsedTable = Get-ObjectNameParts -ObjectName $table
if ($parsedTable.Parsed) {
if ($parsedTable.Schema -and -not $PSBoundParameters.Schema) {
$schema = $parsedTable.Schema
Write-Message -Level Verbose -Message "Schema extracted from table name, using $schema"
}
if ($parsedTable.Name) {
$table = $parsedTable.Name
}
}
# Strip surrounding brackets from schema in case the user passed -Schema "[dbo]"
if ($schema -like "[[]*[]]") {
$schema = $schema.Substring(1, $schema.Length - 2)
}

foreach ($instance in $SqlInstance) {
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
# Open Connection to SQL Server
Expand Down
Loading