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 3224150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:11:22+00:00 2026-05-17T16:11:22+00:00

I have a simple self referencing table as depicted here: CREATE TABLE [dbo].[Project]( [ProjectId]

  • 0

I have a simple self referencing table as depicted here:

CREATE TABLE [dbo].[Project](
    [ProjectId] [int] NOT NULL,
    [ProjectName] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [ParentProjectId] [int] NULL
 CONSTRAINT [PK_Project] PRIMARY KEY CLUSTERED 
(
    [ProjectId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Project]  WITH CHECK
ADD  CONSTRAINT [FK_Project_Project] FOREIGN KEY([ParentProjectId])
REFERENCES [dbo].[Project] ([ProjectId])

I am trying to get a stored proc that will output the records in a tree view type way with a generated field that will index the level of each of the items like a table of contents like so:

1.0       parent
1.1       1st child
1.2       2nd child
1.2.1    1st grandchild of 2nd child

i have this proc that returns lvl but that’s a flat representation and i want a field such as the numbers displayed above (1.1.1, 1.2 and so on)

CREATE PROCEDURE [dbo].[rpt_ExpandProjectList_stefano]    
( @ProjectId int = null)    
AS    
BEGIN    
WITH ProjectList(ProjectId, ParentProjectId, ProjectName,
                 ParentPath, Fullpath, Level)  
AS  
(  
-- Anchor member definition  
    SELECT p.ProjectId, p.ParentProjectId, p.ProjectName, CONVERT(nvarchar(1000), ''), CONVERT(nvarchar(1000), p.ProjectName) AS FullPath, 0 AS Level  
    FROM Project AS p     
    WHERE p.ProjectId = @ProjectId  
    UNION ALL  
-- Recursive member definition  
    SELECT p.ProjectId, p.ParentProjectId, p.ProjectName, CONVERT(nvarchar(1000), d.FullPath), CONVERT(nvarchar(1000), d.FullPath + '|' + p.ProjectName) as FullPath, Level + 1  
    FROM Project AS p  
    INNER JOIN ProjectList AS d  
       ON p.ParentProjectId = d.ProjectId  
)  
-- Statement that executes the CTE  
SELECT pl.ProjectId, pl.ParentProjectId, pl.ProjectName, pl.ParentPath, pl.FullPath, pl.Level  
FROM ProjectList pl  
ORDER BY pl.FullPath  
END
  • 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-17T16:11:22+00:00Added an answer on May 17, 2026 at 4:11 pm

    Here’s a version that may help

    CREATE PROCEDURE [dbo].[rpt_ExpandProjectList_stefano]    
    ( @ProjectId int = null)    
    AS    
    BEGIN    
    
    WITH ProjectNode (ProjectId, ParentProjectId, ProjectName, ProjectNode)
    AS
    (
    -- Derive project node ID
        SELECT ProjectId, ParentProjectId, ProjectName,
            CAST(ROW_NUMBER() OVER (PARTITION BY ParentProjectId ORDER BY ProjectId) AS VARCHAR) AS ProjectNode
        FROM Project
    ),
    
        ProjectList(ProjectId, ParentProjectId, ProjectName,
                     ParentPath, Fullpath, Level, FullNodePath)  
    AS  
    (
    -- Anchor member definition  
        SELECT p.ProjectId, p.ParentProjectId, p.ProjectName, CONVERT(nvarchar(1000), ''), 
            CONVERT(nvarchar(1000), p.ProjectName) AS FullPath, 0 AS Level,
            CONVERT(nvarchar(1000), p.ProjectNode) AS FullNodePath
        FROM ProjectNode AS p     
        WHERE p.ProjectId = @ProjectId  
        UNION ALL  
    -- Recursive member definition  
        SELECT p.ProjectId, p.ParentProjectId, p.ProjectName, CONVERT(nvarchar(1000), d.FullPath), 
            CONVERT(nvarchar(1000), d.FullPath + '|' + p.ProjectName) as FullPath, Level + 1,
            CONVERT(nvarchar(1000), d.FullNodePath + '.' + p.ProjectNode) AS FullNodePath       
        FROM ProjectNode AS p  
        INNER JOIN ProjectList AS d  
           ON p.ParentProjectId = d.ProjectId  
    )
    
    -- Statement that executes the CTE  
    SELECT pl.ProjectId, pl.ParentProjectId, pl.ProjectName, pl.ParentPath, pl.FullPath, pl.Level, pl.FullNodePath
    FROM ProjectList pl  
    ORDER BY pl.FullPath  
    END
    

    You’ll find an additional CTE (before your original CTE) that derives a row number value for each group of ParentProjectId values. The row number is used as the numeric ID element.

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

Sidebar

Related Questions

I have a simple self hosted WCF Console windows app, and I can connect
I have simple xmlrpc server code: from SimpleXMLRPCServer import SimpleXMLRPCServer port = 9999 def
All this might look too trivial but read it through - I have simple
I have a simple recursive function that calculates a simple pendulum swing decay, using
I have a simple question. I use a singleton which implements an abstract class.
I have a simple class that looks a bit like this: @protocol Recorder @property(BOOL)
I have a simple tabBarController application where the user can navigate around 3 views.
I have a simple model: class MediaLink(models.Model): title = models.CharField(max_length=200) subtitle = models.TextField(max_length=2000, unique=False,
first i must admit that i'm not very familiar with sql server's recursive CTE's

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.