Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 selecting up hierarchy

Author  Topic 

SuzyB_24
Starting Member

11 Posts

Posted - 2008-11-25 : 16:56:36
I have two tables, one table lists pages hierarchically, the second contains quotes that will appear on these pages. Some of the pages will have their own quotes but others won't so I would like them to take their quotes from the first page up the hierarchy that has one set.

PageID | ParentID | Name
0 | null | 'home'
1 | 0 | 'page a'
2 | 1 | 'page a child'
3 | 0 | 'page b'

QuoteID | PageID | QuoteText
1 | 0 | quote one
2 | 3 | quote two

I tried this
;With CTE (PageID, ImageID, ImagePath)
AS
(SELECT b.PageID, b.ImageID, b.ImagePath
FROM tbl_Pages a LEFT JOIN tbl_Pages_Images b ON a.PageID = b.PageID
WHERE ParentID = 0
UNION ALL
SELECT b.PageID, b.ImageID, b.ImagePath
FROM tbl_Pages a LEFT JOIN tbl_Pages_Images b ON a.PageID = b.PageID
INNER JOIN CTE c ON c.PageID = a.ParentID
)

SELECT ImagePath from CTE

but get the error Outer join is not allowed in the recursive part of a recursive common table expression 'CTE'. I'm new to SQL Server 2005 and have never used CTE before so any help is appreciated.

Thanks.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-11-25 : 17:37:18
Your sample data doesn't match up to your query. If you can supply proper data, I',m sure we can help. Untill then here a CTE for your first table:
DECLARE @tbl_Pages TABLE(PageID INT, ParentID INT, Name VARCHAR(50))

INSERT @tbl_Pages
SELECT 0 , null , 'home'
UNION ALL SELECT 1 , 0 , 'page a'
UNION ALL SELECT 2 , 1 , 'page a child'
UNION ALL SELECT 3 , 0 , 'page b'

DECLARE @tbl_Pages_Images TABLE (QuoteID INT, PageID INT, QuoteText VARCHAR(50))

INSERT @tbl_Pages_Images
SELECT 1 , 0 , 'quote one'
UNION ALL SELECT 2 , 3 , 'quote two'



;With CTE (PageID, ParentID, Name, PageLevel)
AS
(
SELECT
a.PageID,
a.ParentID,
a.Name,
0 AS PageLevel
FROM
@tbl_Pages a
WHERE
ParentID IS NULL

UNION ALL

SELECT
a.PageID,
a.ParentID,
a.Name,
PageLevel + 1
FROM
@tbl_Pages a
INNER JOIN
CTE AS C
ON a.ParentID = c.PageID
)

SELECT *
FROM CTE

-- This will get you quotes on a page:
SELECT
*
FROM
CTE AS C
LEFT OUTER JOIN
@tbl_Pages_Images AS T
ON C.PageID = T.PageId
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-26 : 00:43:45
Lamprey, the CTE will be avialble only for first select query after definition.So second select wont work.
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2008-11-26 : 00:54:19
quote:
Originally posted by visakh16

Lamprey, the CTE will be avialble only for first select query after definition.So second select wont work.


I think he was giving that as a way to actually use the rows from the CTE, not meaning that you could execute the whole thing as is. :-)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-26 : 00:57:31
quote:
Originally posted by snSQL

quote:
Originally posted by visakh16

Lamprey, the CTE will be avialble only for first select query after definition.So second select wont work.


I think he was giving that as a way to actually use the rows from the CTE, not meaning that you could execute the whole thing as is. :-)


yup...i guessed that. but was pointing it out as OP might try and feel solution wont work
Go to Top of Page

SuzyB_24
Starting Member

11 Posts

Posted - 2008-11-26 : 03:56:34
Yeah sorry Lamprey I just noticed I'm doing basically the the same thing with images as well and got myself mixed up, guess that's what I get for posting tired.

The images is basically the same table structure but with ImagePath instead of QuoteText.

PageID | ParentID | Name
0 | null | 'home'
1 | 0 | 'page a'
2 | 1 | 'page a child'
3 | 0 | 'page b'

ImageID | PageID | ImagePath
1 | 0 | one.jpg
2 | 3 | two.jpg

So if I do a select for the image for page 3 it will give two.jpg. But if I do a select for page 2 it will give one.jpg
Go to Top of Page

SuzyB_24
Starting Member

11 Posts

Posted - 2008-11-26 : 04:59:47
Turns out this is a bit of a mute point. My client is still using SQL Server 2000 so CTE isn't available.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-26 : 05:20:09
This explains a 2000 approach

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109492
Go to Top of Page
   

- Advertisement -