arvindshmicrosoft / SQLScriptDomSamples

Samples showing how to use Microsoft.SqlServer.TransactSql.ScriptDom classes
MIT License
61 stars 16 forks source link

Sample on how to properly process a join clause #9

Open rrozema opened 1 year ago

rrozema commented 1 year ago

Hi Arvind, Thank you very much for your samples, they are very helpful in finding how to work with the sql dom. The only thing so far I have found to be too complex is how to process a join clause: what would be a suitable override that enables me to have access to both the join clause and other references to the columns of the right table of one join clause? In particular: Can you write us a sample where a statement like for example select a.[name], b.[name], c.[name] from dbo.tablea a left outer join (dbo.tableb b left outer join dbo.tablec c on c.id = b.cid ) on b.id = a.bid is parsed to find which columns of each of the table instances are referenced in the search conditions and which columns are referenced otherwise? i.e. I would like to find for each table instance two lists, one of the columns used as a search condition for that table instance, and another for any remaining referenced columns. For my above example the result could look like this (written as json-like): [ { "table" = "dbo.tablea", "alias" = "a", "search condition" = [], "referenced" = ["name", "bid" ]}, { "table" = "dbo.tableb", "alias" = "b", "search condition" = ["id"], "referenced" = ["name", "cid"]}, { "table" = "dbo.tablec", "alias" = "c", "search condition" = ["id"], "referenced" = ["name"]} ]