sql where exist
1
SELECT * FROM Doc_AllotOrder rt WHERE EXISTS( SELECT * FROM ( SELECT distinct t1.DocEntry ,t1.SONum ,t1.CustPONum, t1.Brand, t1.DueDate, t0.DocDate FROM dbo.Doc_AllotOrder t0 '+@sqlWhere+' ) as res2 WHERE DocEntry in(2,3,6) and DocEntry=rt.DocEntry AND SONum=rt.SONum ....... )