keif888 / SQLServerMetadata

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

sqlStatement.getTableNames() not returning all tables used #30

Closed davidkirberich closed 2 years ago

davidkirberich commented 5 years ago

Hi, as I was comparing the table name extraction from the TSqlParser (which uses Microsoft.SqlServer.TransactSql.ScriptDom) with an extraction I once wrote (which uses Microsoft.SqlServer.Management.SqlParser). I realized one issue, where the TSqlParser is not returning the correct tables names.

Example (see query.txt attached):

Expected result Actual result
#TempTable1 #TempTable1
#TempTable2 #TempTable2
#TempTable3 #TempTable3
#TempTable4 #TempTable4
#TempTable5 #TempTable5
#TempTable6 #TempTable6
Table1 Table1
Table2 Table2
Table3 Table3
Table4 Table4
Table5 Table5
Table6 Table6
Table7 Table7
Table8 Table8
Table9 -

Sample code

SqlStatement sqlStatement = new SqlStatement();
if (sqlStatement.ParseString(mySqlQuery))
{
// Sorry, I am lazy so I aggregate the result and compare it in Excel
string tableNames = sqlStatement.getTableNames().OrderBy(p => p).Aggregate((i, j) => i + "\n" + j);
}

query.txt

(Edit: 14.11.2018, false query.txt was uploaded. Find attached the correct one)

keif888 commented 5 years ago

Issue fixed in source. Wasn't handling ExpressionCallTarget's which are a type of function CallTarget.