Hi,
Try this query:
DECLARE @codice nvarchar(35)
Declare @FromDate Datetime
Declare @ToDate Datetime
set @codice = /* select a.itemcode from oitm a where a.itemcode = */ '[%0]'
set @fromdate = /* select min(b.lastpurdat) from oitm b */ '[%1]'
set @Todate = /* select max(c.lastpurdat) from oitm c */ '[%2]' ;
With BOM (Parent,Child,qta,Level) As
(
Select
T0.Father Parent,T0.Code Child,T0.quantity, 0 Level
From
ITT1 T0
inner join oitm B on T0.father = B.itemcode
inner join oitm C on T0.code = C.itemcode
where
t0.code = @codice
Union All
Select
T1.Father,T1.Code,T1.quantity,Level+1
From
Itt1 T1 Inner join BOM T2 on T1.code=T2.parent
)
Select T4.* , t5.lastpurdat, t5.validfor
from BOM T4 inner join oitm T5 on t4.parent = t5.itemcode
where t5.lastpurdat between @fromdate and @Todate
and t5.validfor = 'y'
order by t4.parent
Option (MAXRECURSION 99)
Thanks & Regards,
Nagarajan