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)
 problem in query parent & children within a table

Author  Topic 

slen
Starting Member

2 Posts

Posted - 2009-08-26 : 23:35:12
I am having a problem to query the proper data ...

my table looks like this :
------------------
id | name | parentid
1 Jane 0
2 BOB 1
3 Sharon 2
4 Karen 1
5 Tippy 2

--------------
How do I write a SQL that can query all the children from the parent ...

example, I pass in parent id 0 ...and Bob, karen will be queried as well

Thanks

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-08-27 : 00:05:09
Hi, try this once

DECLARE @ParentID int,@id INT
select @parentID = 0
select @id = id from urtable where parentid = @parentID

select * from urtable where parentid = @id
Go to Top of Page

slen
Starting Member

2 Posts

Posted - 2009-08-27 : 02:40:42
Thanks for the reply.

It didn't work... It didn't display all the records I want although it did display the child. (it didn't include the parent) ...??

I need a child and parent list together ...

Thanks
Go to Top of Page
   

- Advertisement -