File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -288,14 +288,20 @@ function Export-DbaCsv {
288288 if ($PSBoundParameters.Query ) {
289289 $sqlToExecute = $Query
290290 } elseif ($PSBoundParameters.Table ) {
291- # Parse table name for schema using Get-ObjectNameParts to correctly handle bracketed names like [Gross.Table.Name]
292- $nameParts = Get-ObjectNameParts - ObjectName $Table
293- if ($nameParts.Schema ) {
294- $schemaName = $nameParts.Schema
291+ # Parse table name using Get-ObjectNameParts so that bracketed names
292+ # (e.g. [My.Table]) and two-part names (e.g. schema.[My.Table]) are
293+ # handled correctly instead of relying on a naive dot-split regex.
294+ $parsedTable = Get-ObjectNameParts - ObjectName $Table
295+ if ($parsedTable.Parsed -and $parsedTable.Schema ) {
296+ $schemaName = $parsedTable.Schema
297+ $tableName = $parsedTable.Name
298+ } elseif ($parsedTable.Parsed ) {
299+ $schemaName = " dbo"
300+ $tableName = $parsedTable.Name
295301 } else {
296302 $schemaName = " dbo"
303+ $tableName = $Table
297304 }
298- $tableName = $nameParts.Name
299305 $sqlToExecute = " SELECT * FROM [$schemaName ].[$tableName ]"
300306 }
301307
Original file line number Diff line number Diff line change @@ -1022,6 +1022,25 @@ WHERE c.object_id = OBJECT_ID(@tableName)
10221022 }
10231023 }
10241024
1025+ # Normalize table and schema names using Get-ObjectNameParts.
1026+ # This handles bracketed names (e.g. [My.Table]) and two-part names (e.g. schema.[My.Table]).
1027+ # sys.tables/sys.schemas store bare names without brackets, so we must strip them
1028+ # before using the values in parameterized metadata queries.
1029+ $parsedTable = Get-ObjectNameParts - ObjectName $table
1030+ if ($parsedTable.Parsed ) {
1031+ if ($parsedTable.Schema -and -not $PSBoundParameters.Schema ) {
1032+ $schema = $parsedTable.Schema
1033+ Write-Message - Level Verbose - Message " Schema extracted from table name, using $schema "
1034+ }
1035+ if ($parsedTable.Name ) {
1036+ $table = $parsedTable.Name
1037+ }
1038+ }
1039+ # Strip surrounding brackets from schema in case the user passed -Schema "[dbo]"
1040+ if ($schema -like " [[]*[]]" ) {
1041+ $schema = $schema.Substring (1 , $schema.Length - 2 )
1042+ }
1043+
10251044 foreach ($instance in $SqlInstance ) {
10261045 $elapsed = [System.Diagnostics.Stopwatch ]::StartNew()
10271046 # Open Connection to SQL Server
You can’t perform that action at this time.
0 commit comments