keif888 / SQLServerMetadata

SQL Server Metadata Toolkit
Microsoft Public License
83 stars 32 forks source link

Update statements with table alias are not parsed correctly #11

Closed fmms closed 6 years ago

fmms commented 6 years ago

For updating dimensions we have a lot of update tasks that are similar to:

 update a
       set 
a.UnitofMeasurement=b.UnitofMeasurement,
a.UnitofMeasurementText=b.UnitofMeasurementText
 from [Ods].[DimUnit] a
 join  [Ods].[DimUnitUpdate] b
 on a.UnitKey=b.UnitKey

The are displayed as follows in the Dependency Viewer:

image

However, for sure there is no table [dbo].[a]...

keif888 commented 6 years ago

The alias after the update statement, and the actual tables are being detected as the following type: Microsoft.SqlServer.TransactSql.ScriptDom.NamedTableReference As such there is no simple way of excluding this particular style of alias from being detected as a table. Please note that the b was detected as an alias, as it's after a table, so that is handled. Theoretically I could capture all the alias' used elsewhere in the query, and then try and remove the pseudo table. Tricky, as I will have to add the capability of removing items from the repository, which doesn't exist yet, or some how scan the rest of the statement for alias' and check if the object identified matches a named alias, and not add it to the repository. Code that detects the table in question is in SqlStatement.cs, line 2007.

keif888 commented 6 years ago

Fixed in code base. All alias' and CTE's are detected and collected. Any table that has the same name as an alias or CTE is removed from the results before returning from the parser. Passes all the unit tests.

keif888 commented 6 years ago

Fixed (as far as possible) in V0.22.0.0 release.