Hi,
Try this and modify the selection criteria as per your requirement:
declare @poNum int, @VendorCode nvarchar(15), @VendorName nvarchar(100), @ItemCode nvarchar(20)
/*
select p.DocEntry
from opor d
join por1 dr on d.DocEntry = dr.DocEntry
where d.DocNum = '[%0]'
and d.CardCode = '[%1]'
and d.CardName like '[%2]' + '%'
and dr.ItemCode like '[%3]' + '%'
*/
set @poNum = '[%0]'
set @VendorCode = '[%1]'
set @VendorName = '[%2]'
set @ItemCode = '[%3]'
select
por.DocNum as [Purchase Order]
, por.CardCode as [Vendor Code]
, por.CardName as [Vendor Name]
, por1.LineNum as [Row]
, coalesce( i.InvntItem, 'N' ) as [InvntItem]
, por1.ItemCode
, por1.Dscription
, case por.DocType when 'I' then por1.Quantity else por1.LineTotal end as [Ordered]
, case por.DocType when 'I' then por1.OpenCreQty else por1.OpenSum end as [Open (PO)]
, case when por.DocStatus = 'C' then 'C' else por1.LineStatus end as [Status]
, pdn.DocNum as [Purchase Delivery]
, case pdn.DocType when 'I' then pdn1.Quantity else pdn1.LineTotal end as [Reveived]
, case pdn.DocType when 'I' then pdn1.OpenCreQty else pdn1.OpenSum end as [Open (PD)]
, case when pdn.DocStatus = 'C' then 'C' else pdn1.LineStatus end as [Status]
, pch.DocNum as [A/P Invoice]
, case pch.DocType when 'I' then pch1.Quantity else pch1.LineTotal end as [Invoiced]
, case when pch.DocStatus = 'C' then 'C' else pch1.LineStatus end as [Status]
from por1 por1
join opor por on por1.DocEntry = por.DocEntry
left join pdn1 pdn1 on por1.DocEntry = pdn1.BaseEntry and por1.LineNum = pdn1.BaseLine and pdn1.BaseType = 22
left join opdn pdn on pdn1.DocEntry = pdn.DocEntry
left join pch1 pch1 on ( por1.DocEntry = pch1.BaseEntry and por1.LineNum = pch1.BaseLine and pch1.BaseType = 22 )
or ( pdn1.DocEntry = pch1.BaseEntry and pdn1.LineNum = pch1.BaseLine and pch1.BaseType = 20 )
left join opch pch on pch1.DocEntry = pch.DocEntry
left join oitm i on por1.ItemCode = i.ItemCode
where ( por.DocNum = @poNum or @poNum = 0 )
and ( por.CardCode = @VendorCode or @VendorCode = '' )
and ( por.CardName like @VendorName + '%' or @VendorName = '' )
and ( por1.ItemCode like @ItemCode + '%' or @ItemCode = '' )
order by
por.DocNum
, por1.LineNum
, coalesce( pdn.DocDate, 0 )
, coalesce( pdn.DocEntry, 0 )
, coalesce( pch.DocDate, 0 )
, coalesce( pch.DocEntry, 0 )
for browse
Thanks & Regards,
Nagarajan