Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
id details parent1 this null2 is 13 a sample 24 table 3
if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts
Posted - 2011-02-14 : 23:50:56
If @param=4 Begin Select * from TableName where ID=1EndIs the above what you are looking for? If not then what do you mean by "if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!" Can you explain the logic behind your statement?
bo0tl3ss
Starting Member
20 Posts
Posted - 2011-02-15 : 00:27:53
quote:Originally posted by MIK_2008 If @param=4 Begin Select * from TableName where ID=1EndIs the above what you are looking for? If not then what do you mean by "if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!" Can you explain the logic behind your statement?
sorry for bad english. i mean if the sql parameter is 4 how could i get the row which is row 1 that is the last parent of the list... :D thanks
bo0tl3ss
Starting Member
20 Posts
Posted - 2011-02-15 : 00:32:54
quote:Originally posted by MIK_2008 If @param=4 Begin Select * from TableName where ID=1EndIs the above what you are looking for? If not then what do you mean by "if the param is 4 how will i get the 1 row? thanks guys in advanced! more power!" Can you explain the logic behind your statement?
hi, even if the sql parameter is 3 or 2 it should return the row 1 because this is their last parent. i dont know how to call it. the first hierarchy of the parameter.. :D thanks again
bo0tl3ss
Starting Member
20 Posts
Posted - 2011-02-15 : 01:13:48
how to get the grandparent row... :d
Sachin.Nand
2937 Posts
Posted - 2011-02-15 : 01:37:56
This?
declare @t table (id int ,parent int)insert @tselect 1,null unionselect 2,1 unionselect 3 ,2 unionselect 4,3select * from @t;with cteas(select id,parent,parent p from @t union allselect t1.id,t2.id,t2.p from @t t1 inner join cte t2 on t1.parent=t2.id)select min(p)parent, id from cte where id=4 group by id
PBUH
bo0tl3ss
Starting Member
20 Posts
Posted - 2011-02-15 : 01:44:06
quote:Originally posted by Sachin.Nand This?
declare @t table (id int ,parent int)insert @tselect 1,null unionselect 2,1 unionselect 3 ,2 unionselect 4,3select * from @t;with cteas(select id,parent,parent p from @t union allselect t1.id,t2.id,t2.p from @t t1 inner join cte t2 on t1.parent=t2.id)select min(p)parent, id from cte where id=4 group by id
PBUH
whew... great! this is what i need... thanks a lot! more knowledge to you! will test on my table! thanks thanks!
bo0tl3ss
Starting Member
20 Posts
Posted - 2011-02-15 : 02:00:36
i got this error.. Outer join is not allowed in the recursive part of a recursive common table expression 'cte'.because i have two tables... i have to get the parentid from another table..
Table1id details 1 this 2 is 3 a sample 4 table Table2id parent2 13 24 3
Sorry for not giving the complete details... just new with cte... thanks thanks
bo0tl3ss
Starting Member
20 Posts
Posted - 2011-02-15 : 02:45:20
found it thanks...instead of using table1 to join i used table2! thanks a lot bro! it really helps! thanks