Buen día
Prueba este query como busqueda formateada
if (select $[$38.43.0])='17' begin select top 1 DocNum from ORDR where DocEntry= $[$38.45.0] end if (select $[$38.43.0])='15' begin if (select top 1 BaseType from DLN1 where DocEntry=$[$38.45.0])='17' begin select top 1 DocNum from ORDR where DocEntry=(select top 1 BaseEntry from DLN1 where DocEntry=$[$38.45.0]) end end else select 0
Saludos