Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:19:20+00:00 2026-05-18T05:19:20+00:00

I am trying to figure out a stored procedure that can get a the

  • 0

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!

Section1

|JobID — FK

|SectID — PK

Section2

|Sect1ID — FK

|Sect2ID — PK

Section3

|Sect2ID — FK

|Sect3ID — PK

Section4

|Sect3ID — FK

|Sect4ID — PK

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:19:20+00:00Added an answer on May 18, 2026 at 5:19 am

    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.

    SELECT COALESCE(Section4.SecName
                    ,Section3.SecName
                    ,Section2.SecName
                    ,Section1.SecName) AS DeepestSection
    FROM (SELECT 'Section1' AS SecName, * FROM Section1) AS Section1
    LEFT JOIN (SELECT 'Section2' AS SecName, * FROM Section2) AS Section2
        ON Section2.Sect1ID = Section1.SectID
    LEFT JOIN (SELECT 'Section3' AS SecName, * FROM Section3) AS Section3
        ON Section3.Sect2ID = Section2.Sect2ID
    LEFT JOIN (SELECT 'Section4' AS SecName, * FROM Section4) AS Section4
        ON Section4.Sect3ID = Section3.Sec3tID
    WHERE Section1.JobID = whatever
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the data structure that is stored in 'buffer' after
I can't for the life of me figure out how this stored procedure works
A stored procedure that runs a SELECT on a large table is timing out.
I'm trying to update a table via a stored procedure from asp.net. I get
I'm trying out stored procedures for the first time, and I can't figure out
I'm trying to figure out why a SQL Server stored procedure is executing slowly,
Im trying to figure out how i can solve an equation that has been
I'm trying to figure out a way to create a stored procedure for deleting
Trying to figure out how I can do this properly. The print_r looks like
I have created a method that can be called using a stored procedure name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.