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

  • SEARCH
  • Home
  • 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 8876735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:16:59+00:00 2026-06-14T19:16:59+00:00

I found this nice example of SQL recursion with a CTE, but fail to

  • 0

I found this nice example of SQL recursion with a CTE, but fail to apply it to my table:

  • http://walkingoncoals.blogspot.de/2009/12/fun-with-recursive-sql-part-1.html

I have the following table (ObjectStates):

ID    Title    ParentID
1     Draft    null
2     Green    null
3     Red      null
4     Foo      1
5     Bar      4

I am trying to create a function which returns the “main” state when queried. Example:

GetMainState(5)
-- Shall return 1
GetMainState(4)
-- Shall return 1
GetMainState(2)
-- Shall return 2

I have so far:

CREATE FUNCTION [dbo].[GetMainObjectState] (@ObjectStateID INT)
RETURNS TABLE
AS
RETURN 
(
 WITH StateRecurcsion(ID, ParentID, Level) AS
       (
           SELECT ID, ParentID, 0
           FROM ObjectStates
           WHERE ID = @ObjectStateID
           UNION ALL
           SELECT uOS.ID, uOS.ParentID, sOS.Level+1
           FROM ObjectStates uOS, StateRecurcsion sOS
           WHERE uOS.ParentID= sOS.ID
       )
 SELECT os.ID, os.Title, sos.Level
 FROM ObjectStates os, StateRecurcsion sos
 WHERE os.ID = sos.ID
)
GO

I tried to create the function just as in the tutorial shown above, but somehow I’m not getting the correct results.

  • 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-06-14T19:17:00+00:00Added an answer on June 14, 2026 at 7:17 pm

    You could create a CTE containing a “root” value and then query it within your function e.g.:

    ;WITH CTEHierarchy
    AS (
        SELECT 
        ID
            ,0 AS LEVEL
            ,ID AS root
    
        FROM ObjectStates
        WHERE  ParentID IS NULL
    
        UNION ALL
    
        SELECT 
        ObjectStates.ID
            ,LEVEL + 1 AS LEVEL
            ,[root]
    
        FROM ObjectStates
        INNER JOIN CTEHierarchy uh ON uh.id = ObjectStates.ParentID
        )    
        SELECT [root]
        FROM CTEHierarchy
        WHERE ID = @ObjectStateID 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this nice example of doing paging in SQL Server, however, I need
I found this example: http://www.yaml.org/YAML_for_ruby.html#extending_kernel::hash It's nice to see how easy YAML can serialize
I found this nice example of a Python C Module, where a single integer
Following this nice example I found, I was trying to create a function that
I found a nice example on how to use AJAX in jQuery at this
found this little code snippet that seems to do what i want, but im
Found this Multimap containing pairs? , but it is not much help How would
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
Found this is some example CSS I was reading and I'm having a hard
I have found a nice example how to find the center point of a

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.