I am trying to figure out a stored procedure that can get a the depth of a table structure. Bear in mind the table design was before my time and cannot be altered.
When I say depth (to ellaborate), I mean the highest section table that has a record originating from a JobID.
I am really rusty with my SQL so I am brain farting a clever way to to return the depth of a given jobID record.
Any help is appreciated!
If the schema is fully known to you (as above), then you could simply do (for a single JobID). Note that I have augmented each table to facilitate the COALESCE.
If would be changed up slightly to do an analysis over all jobs.
If the number of tables with references trickling back up to JobID is not yet fully known to you at this time, you would use the Information Schema views http://msdn.microsoft.com/en-us/library/ms186778.aspx – use this to traverse the defined constraints and determine dependencies – creating necessary SQL to do the above along the way in dynamic form and then executing it.