Saturday, May 17, 2008

sql With Command and recursive queries

Wıth used to query table on itself as recursive
for example you may select products form products table group by producer and also you may want to get total count of product this task may accomplish with the "with command"
here is a code snippet...


with ReyonCTE(reyonKod,reyonAd,AnaReyonKod)
as
(
select reyonKod,reyonAd,AnaReyonKod
from tblReyon
where anaReyonKod is null
union all
select cocuk.reyonKod,cocuk.reyonAd,cocuk.AnaReyonKod
from tblReyon as cocuk Join ReyonCte as ana
On cocuk.anaReyonKod=ana.ReyonKod
)
select * from ReyonCTE

No comments: