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.
| Author |
Topic |
|
liran-e
Starting Member
8 Posts |
Posted - 2010-12-27 : 20:26:53
|
| Hi,I've got a table that consists of two column: pid & sidthe table looks like this:pid cid1 21 33 43 55 6I need to create another table that will contain parents & children all along the chain ... any suggestions ?this should be the output:pid cid1 21 31 41 51 63 43 53 65 6 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-27 : 20:38:51
|
| with cte as(select strtpid=pid, pid, cid, seq=1 from tblunion allselect cte.strtpid, t.pid, t.cid from cte join tbl t on cte.cid=t.pid)select distinct pid=strtpid, cidfrom cte==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
liran-e
Starting Member
8 Posts |
Posted - 2010-12-27 : 20:43:11
|
| my table name is r1 I've tried your query:with cte as(select strtpid=pid, pid, cid, seq=1 from r1union allselect cte.strtpid, t.pid, t.cid from cte join r1 t on cte.cid=t.pid)select distinct pid=strtpid, cidfrom ctebut I'm getting this error msg:All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-27 : 20:53:09
|
| Sorry - thought I would need seq but didn'tIf this one doesn't work i'll try testing itwith cte as(select strtpid=pid, pid, cid from r1union allselect cte.strtpid, t.pid, t.cid from cte join r1 t on cte.cid=t.pid)select distinct pid=strtpid, cidfrom cte==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
liran-e
Starting Member
8 Posts |
Posted - 2010-12-27 : 20:57:14
|
| perfect :)thank you very much |
 |
|
|
|
|
|
|
|