Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,


SQL
advisor_id  name       parent_id

1          Griff        0(assume)
2          christian    1
3          AArti        2
4          king_fisher  3
5          jason        4
6          smith        5
7          willis       6



This is my table structure, if i gonna ask tree for advisor_id =7 .you guys know the tree is 6,5,4,3,2,1,0
like this,if i gonna ask for advisor_id =4 it must 3,2,1,0

how can i get this ??.

i hope its clear,
Thanks
Posted
Updated 28-Dec-13 0:14am
v6
Comments
Christian Graus 28-Dec-13 4:43am    
Griff's answer is still correct to create a comma separated list. Did you still have a question ?
King Fisher 28-Dec-13 4:46am    
yes.thereby,i updated now
Christian Graus 28-Dec-13 4:48am    
The question is the same. You want to get a list of ids set as @sponsors. Griffs answer tells you how to do that. I am at a loss. What do you want to know ? Perhaps if you told us your schema and gave us some example data to show us what you want ?

Try:
SQL
SELECT CAST(create_by_user_id AS VARCHAR) + ',' FROM tbl_advisor_registration where advisor_id=@intro_id FOR XML PATH('')


[edit]Spurious space removed - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
King Fisher 27-Dec-13 7:42am    
can u explain what is the use of XML Path?
OriginalGriff 27-Dec-13 7:48am    
Not in this text box! :laugh:
Basically it converts a table (which is what the SELECT produces) into a flat string, but without any actual XML surrounding it because no element name was specified in the PATH parameter.
Microsoft overviews it:
http://technet.microsoft.com/en-us/library/ms190922.aspx
But doesn't explain it very well - the longer description gives better detail:
http://technet.microsoft.com/en-us/library/ms189885.aspx
Here it is :

SQL
declare @id int=7;
declare @tmp varchar(max)='';
declare  @t1 table(
advisor_id  int,name       varchar(50),parent_id int)
insert into @t1 values
(1,'Griff',0),(2,'christian',1),(3 ,'AArti',2),
(4,'king_fisher',3),(5,'jason',4),(6,'smith',5),(7,          'willis'       ,6);

with a(id , pid)
as
(select advisor_id,parent_id  from @t1 where advisor_id=@id
union all
select a.id, t.parent_id from a inner join @t1 t on a.pid = t.advisor_id
)
--select pid from a
select @tmp = @tmp + cast(pid as nvarchar(max)) + ', ' from a
select SUBSTRING(@tmp, 0, LEN(@tmp))


Change @id value to see what happens.

Good Luck.
 
Share this answer
 
OK, I've read through all the replies. Your question is HORRIBLE, it's not remotely clear. According to the comments, you want to write a recursive query. You can do that using CTEs, here[^] is my article on how they work. If you then want to get that list in to one cell, you will need to use the XML approach Griff showed you, once you've got the table of data to work with.

I strongly suspect you're not going to read any articles and just want us to do all the work for you. That is impossible. We can't really do more without the schema of your table, example data, and examples of the results you want to get out, so we can see what exactly you want, and where you want it from.
 
Share this answer
 
Comments
King Fisher 28-Dec-13 6:04am    
thanks,i updated,now i hope its clear
Christian Graus 28-Dec-13 14:27pm    
Yes, now it's a clear question, good to see someone answered it. As I suspected, my article on CTEs explained all you needed to know to do this.
While loop not required
check this example
SQL
WITH tbl_advisor_registration as
(
select 1 create_by_user_id, 1 as advisor_id UNION ALL
select 2 create_by_user_id, 1 as advisor_id UNION ALL
select 3 create_by_user_id, 1 as advisor_id UNION ALL
select 4 create_by_user_id, 1 as advisor_id UNION ALL
select 5 create_by_user_id, 1 as advisor_id UNION ALL
select 6 create_by_user_id, 2 as advisor_id UNION ALL
select 7 create_by_user_id, 2 as advisor_id
)
Select Stuff(
                  (
                    Select ', ' + Convert(varchar(5),create_by_user_id)
                        from tbl_advisor_registration
                        where advisor_id = 1 --@intro_id
                        For Xml Path('')
                  ), 1, 2, ''
            )

Happy coding!
:)
 
Share this answer
 
Comments
King Fisher 27-Dec-13 7:40am    
the create_by_use_id may be like this

200,1,120,5,100,500,...
whatever
Aarti Meswania 27-Dec-13 7:47am    
yes
simply use this
Select Stuff(
(
Select ', ' + Convert(varchar(5),create_by_user_id)
from tbl_advisor_registration
where advisor_id = 17 --@intro_id
For Xml Path('')
), 1, 2, ''
)
you will get result
King Fisher 27-Dec-13 7:56am    
her my tree is,37,36,35,34,20,0

if i,
Select Stuff( ( Select ', ' + Convert(varchar(5),create_by_user_id) from tbl_advisor_registration where advisor_id = 37--@intro_id For Xml Path('') ), 1, 2, '' )

i want this 37,36,35,34,20,0
i get only 36
Aarti Meswania 27-Dec-13 7:58am    
because you have only one record against advisor_id = 37 in your table
Christian Graus 28-Dec-13 4:42am    
His query is flawed, because it defines the depth it's willing to go down to, it's not unlimited

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900