-
-
Notifications
You must be signed in to change notification settings - Fork 785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Find-dbadbunusedindex | write-dbadbtabledata - Incorrect syntax near ')' #9357
Comments
The pipeline in PowerShell is a super feature. But to get to the root cause of this problem, we need to save the results of the first command to a variable and have a look at it. Please run this code:
This way, we get to know if there are any unused indexes and how the data looks like. Then we try to pass that data to Write-DbaDbTableData. |
PS C:\Windows\system32> $unusedIndexes = @( )
$unusedIndexes += Find-DbaDbUnusedIndex -SqlInstance sql01
$unusedIndexes.Count
$unusedIndexes[0].GetType()
$unusedIndexes[0] | Format-List
91
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False DataRow System.Object
ComputerName : SQL01
InstanceName : MSSQLSERVER
SqlInstance : SQL01
Database : SbGatewayDatabase
DatabaseId : 5
Schema : dbo
Table : MessageContainerRoleInstanceHistoryTable
ObjectId : 821577965
IndexName : CIX_MessageContainerRoleInstanceHistoryTable
IndexId : 1
TypeDesc : CLUSTERED
UserSeeks : 0
UserScans : 0
UserLookups : 0
UserUpdates : 26
LastUserSeek :
LastUserScan :
LastUserLookup :
LastUserUpdate : 5/12/2024 3:46:33 PM
SystemSeeks : 0
SystemScans : 0
SystemLookup : 0
SystemUpdates : 0
LastSystemSeek :
LastSystemScan :
LastSystemLookup :
LastSystemUpdate :
IndexSizeMB : 0.335937
RowCount : 3240
CompressionDescription : NONE |
Thanks for the feedback. The data looks valid. Next step is to feed the data to Write-DbaDbTableData. From a performance point of view, the best is to use the InputObject parameter:
If that still throws an error, please add |
VERBOSE: [07:13:16][Connect-DbaInstance] String is passed in, will build server object from instance object and other parameters, do some checks and then return the server object
VERBOSE: [07:13:16][Connect-DbaInstance] authentication method is 'local integrated'
VERBOSE: [07:13:16][Write-DbaDbTableData] FQTN processed: [bb].[dbo].[unusedindexes]
VERBOSE: [07:13:16][Write-DbaDbTableData] Creating SqlBulkCopy object
VERBOSE: [07:13:16][New-Table] Creating table for [bb].[dbo].[unusedindexes]
WARNING: [07:13:16][Write-DbaDbTableData] Failed to create table [bb].[dbo].[unusedindexes] | Incorrect syntax near ')'.
So for more info ran debug
To me, this BEGIN CREATE TABLE [bb].[dbo].[unusedindexes] () END looks like it passes in no field names. |
As a side note, if I use the output from the commands you gave above to show 1 record with all the data, I created a table manually, then ran the command with the | and it worked. Find-DbaDbUnusedIndex -SqlInstance sql01| Write-DbaDbTableData -SqlInstance sql01-Database bb -table unusedindexes1 That loaded all the data 91 rows to be precise. |
The -AutoCreateTable parameter needs the table structure that is (normally) part of the DataRow object. But the name of the property is "Table", which in case of the output of Find-DbaDbUnusedIndex is overwritten by the column "Table". |
I will provide a pull request to test if the InputObject has column information and in case it has not, output a better error message. |
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
I've tried two methods to pull unused indexes and write to a table the output.
Incorrect syntax near ')'.
Steps to Reproduce
Both of these fail with same error of Incorrect syntax near ')'
PS C:\Windows\system32> Find-DbaDbUnusedIndex -SqlInstance sql01 | Write-DbaDbTableData -SqlInstance sql01 -Database bb -table unusedindexs -AutoCreateTable
WARNING: [13:05:59][Write-DbaDbTableData] Failed to create table [bb].[dbo].[unusedindexs] | Incorrect syntax near ')'.
PS C:\Windows\system32> Get-DbaDatabase -SqlInstance sql01 |Find-DbaDbUnusedIndex |Write-DbaDbTableData -SqlInstance sql01 -Database bb -Table unusedindexes -AutoCreateTable
WARNING: [13:13:39][Write-DbaDbTableData] Failed to create table [bb].[dbo].[unusedindexes] | Incorrect syntax near ')'.
Please confirm that you are running the most recent version of dbatools
2.1.14
Other details or mentions
If I run the command in debug, this information is shown.
DEBUG: [13:21:06][New-Table] The following query failed: BEGIN CREATE TABLE [bb].[dbo].[unusedindexes] () END | Incorrect syntax near ')'.
The error is because there are no columns in the table.
What PowerShell host was used when producing this error
Windows PowerShell (powershell.exe)
PowerShell Host Version
Name Value
PSVersion 5.1.14393.6343
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.6343
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
SQL Server Edition and Build number
Microsoft SQL Server 2019 (RTM-CU26) (KB5035123) - 15.0.4365.2 (X64) Mar 29 2024 23:02:47 Copyright (C) 2019 Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows Server 2019 Standard 10.0 (Build 17763: ) (Hypervisor)
.NET Framework Version
.NET Framework 4.7.4051.0
The text was updated successfully, but these errors were encountered: