microsoft / sqlmanagementobjects

Sql Management Objects, an API for scripting and managing SQL Server and Azure SQL Database
Other
132 stars 22 forks source link

DependencyWalker.DiscoverDependencies fails with System.ArgumentException: Item has already been added. #148

Open alitas opened 1 year ago

alitas commented 1 year ago

We hit this when we had a few old stored procedures referring to the same dropped table with different casing for database name. I see commented out code at https://github.com/microsoft/sqlmanagementobjects/blob/main/src/Microsoft/SqlServer/Management/Smo/DependencyTree.cs#L198 that might have fixed this issue, not sure why it's been commented out though.

Microsoft.SqlServer.Management.Smo.FailedOperationException: Discover dependencies failed. 
 ---> System.ArgumentException: Item has already been added. Key in dictionary: 'Server[@Name='SERVER']/Database[@Name='database']/UnresolvedEntity[@Name='nonexisting_table' and @Schema='dbo']'  Key being added: 'Server[@Name='SERVER']/Database[@Name='DATABASE']/UnresolvedEntity[@Name='nonexisting_table' and @Schema='dbo']'
   at Microsoft.SqlServer.Management.Smo.DependencyTree..ctor(Urn[] urns, DependencyChainCollection dependencies, Boolean fParents, Server server)
   at Microsoft.SqlServer.Management.Smo.DependencyWalker.DiscoverDependencies(Urn[] urns, Boolean parents)
shueybubbles commented 1 year ago

thx for the report! That line has been commented out since before 2013 so it predates my time on SQL by a fair amount.

Can you provide a simple TSQL script that creates the duplicate key with different casing so I can add it to some tests? I happen to be working on updating this dependency discovery code right now.