Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2005
    Location
    Auckland, New Zealand
    Posts
    11

    Unanswered: Nested Set Model

    hi guys,

    i have the following category table...

    CREATE TABLE [dbo].[categories] (
    [cat_id] [int] IDENTITY (1, 1) NOT NULL ,
    [dModified] [datetime] NULL ,
    [dCreated] [datetime] NULL ,
    [cat_parent_id] [int] NULL ,
    [cat_lft] [int] NULL ,
    [cat_rgt] [int] NULL ,
    [cat_image] [varchar] (255) COLLATE Latin1_General_CI_AS NULL ,
    [cat_sort_order] [int] NULL
    ) ON [PRIMARY]
    GO

    i now wish to add a levels column which indicates (funnily enough ) which level each record is on starting from 1.

    is there a query i can use which will output for me the levels based on the child/parent relationship?

    regards
    mike

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Code:
    update categories set level = null
    
    update categories set level = 1 where cat_parent_id is null
    
    while @@ROWCOUNT > 0
    update categores
    set	level = parents.level + 1
    from	categories
    	inner join categories parents on categories.cat_parent_id = parents.cat_id
    where	categories.level is null
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    ...and by the way, what you have is a binary tree, not a nested set model.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    what you have is a blind dude who will lead the way...
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •