How do I go about link web content in a database with a nested set model?

Posted by wb on Stack Overflow See other posts from Stack Overflow or by wb
Published on 2010-06-17T19:00:29Z Indexed on 2010/06/17 19:03 UTC
Read the original article Hit count: 187

My nested set table is as follows.

 create table depts (
  id int identity(0, 1) primary key
  , lft int
  , rgt int
  , name nvarchar(60)
        , abbrv nvarchar(20)
 );

Test departments.

insert into depts (lft, rgt, name, abbrv) values (1, 14, 'root', 'r');
insert into depts (lft, rgt, name, abbrv) values (2, 3, 'department 1', 'd1');
insert into depts (lft, rgt, name, abbrv) values (4, 5, 'department 2', 'd2');
insert into depts (lft, rgt, name, abbrv) values (6, 13, 'department 3', 'd3');
insert into depts (lft, rgt, name, abbrv) values (7, 8, 'sub department 3.1', 'd3.1');
insert into depts (lft, rgt, name, abbrv) values (9, 12, 'sub department 3.2', 'd3.2');
insert into depts (lft, rgt, name, abbrv) values (10, 11, 'sub sub department 3.2.1', 'd3.2.1');

My web content table is as follows.

 create table content (
  id int identity(0, 1)
  , dept_id int
  , page_name nvarchar(60)
  , content ntext
 );

Test content.

insert into content (dept_id, page_name, content) 
 values (3, 'index', '<h2>welcome to department 3!</h2>');
insert into content (dept_id, page_name, content) 
 values (4, 'index', '<h2>welcome to department 3.1!</h2>');
insert into content (dept_id, page_name, content) 
 values (6, 'index', '<h2>welcome to department 3.2.1!</h2>');
insert into content (dept_id, page_name, content) 
 values (2, 'what-doing', '<h2>what is department 2 doing?/h2>');

I'm trying to query the correct page content (from the content table) based on the url given. I can easily accomplish this task with a root department. However, querying a department with multiple depths is proving to be a little harder. For example:

http://localhost/departments.asp?d3/ (Should return <h2>welcome to department 3!</h2>)
http://localhost/departments.asp?d2/what-doing (Should return <h2>what is department 2 doing?</h2>)

I'm not sure if this can be create in one query or if there will need to be a recursive function of some sort. Also, if there is nothing after the last / then assume we want the index page.

How can this be accomplished?

Thank you.

© Stack Overflow or respective owner

Related posts about asp-classic

Related posts about sql-server-2000