Hi Gary,
On consultation with others here is an explanation of what the code is doing:
It just expands out the Location to get all Location including those in the Hierarchy below it. It is working downwards through the hierarchy.
Location keys (from the XML parameter and can be many ) go into the temporary table in both the Location and Parent field.
Main Loop
INNER JOIN #Locations Tinc on L.Parent_Key=Tinc.Location_Key
This line finds the children of the Locations in the temporary table (ie it is finding the locations in the database which have as their parents Locations in the temporary table) The parent key is picked up from the parent of the temporary table, so in effect the original location key is put in the parent field for all its children.
LEFT JOIN #Locations Texc ON Texc.Location_Key=L.Location_Key WHERE Texc.Location_Key IS NULL
This Line ensure that we are only processing Location not already done by checking that they are not in the table
IF @@ROWCOUNT=0
BREAK
The Locations found are added to the table and the loop goes round until no new Locations are found
The final temporary table has the originally specified Location keys plus
all the children of these Locations. The parent key column contains the
originally 'seed' location key, not the true parent of the location. This
allows some flexibility in using the temporary table. For example you can
exclude the 'seed' locations by ignoring anything where the Location and the
parent are the same.
Hope this helps,
Lynn