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)
 Noob question for query within query

Author  Topic 

tenderfoot
Starting Member

4 Posts

Posted - 2007-10-31 : 05:51:52
Hiya,

I will try to explain this as clearly as possible:

I have inhereted a database and website, I am trying to create a breadcrumb facility for this website, and thus I am trying to grab all the data I need by running a simple query that pulls out a row of information detailing what the name of a page is, it's category and the parent category of that cagetory.

For example I run this simple query:


SELECT
con.Headline As PageName,
cat.Name As Category,
cat.Parent As ParentCategory

FROM Content con, Category cat
WHERE cat.ID = con.CatID And con.ThreadID = @PageID



and it gives me this result:

PageName Category ParentCategory
-------- -------- --------------
Listings Events 2


Now, to find the Parent category name to create the breadcrumb, I need to query the same table, so it is almost like I need to use the output for "ParentCategory" of this query, to perform another query on the same table.

Conceptually, I have no idea what I need to do to achieve this. I have heard of inserting values into a temporary table and requerying, and creating OUTPUT parameters, but I cannot find a tutorial for my level/experience that explains what I need to do.

Can anyone help please?



Cheers,


Tenderfoot

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2007-10-31 : 07:36:53
refer the below stuff... u can achieve this way as well...

Create Table Mahesh
(Roll Int,
Emp VarChar(10),
Mgr Int)
Go

Insert Into Mahesh Values (1, 'AAA', 2)
Insert Into Mahesh Values (2, 'BBB', 3)
Insert Into Mahesh Values (3, 'CCC', 1)
Go

Select Roll
,Emp
,Mgr
,(Select M2.Emp From Mahesh As M2 Where M2.Roll = M1.Mgr) As Mgr_Name
From Mahesh As M1
Go

hopes it will solve ur prob.

Mahesh
Go to Top of Page

tenderfoot
Starting Member

4 Posts

Posted - 2007-10-31 : 11:19:33
Thanks for your solution Mahesh!

It took me a while to work out what was going on and use it for my own query, but I think I have got it now.


Cheers, I am very happy now!



Tenderfoot
Go to Top of Page
   

- Advertisement -