Select
   MIV_VALVEID as VALVEID,
   MIV_GISOBJID as GISOBJID,
   MIV_BLOCKID1 as BLOCKID1,
   MIV_BLOCKID2 as BLOCKID2
from
   R5MAINISOLATIONVALVES
where
   MIV_BLOCKID1 = <param.blockid>
   OR MIV_BLOCKID2 =<param.blockid>
intersect
Select
   MIV_VALVEID as VALVEID,
   MIV_GISOBJID as GISOBJID,
   MIV_BLOCKID1 as BLOCKID1,
   MIV_BLOCKID2 as BLOCKID2
from
   R5MAINISOLATIONVALVES
where
   MIV_VALVEID NOT IN
   (
      Select
         MIV_VALVEID
      from
         R5MAINISOLATIONVALVES
      where
         MIV_BLOCKID1 IN
         (
            Select
               MID_DEPBLOCKID
            from
               R5MAINISOLATIONDEPBLOCKS
            where
               MID_BLOCKID = <param.blockid>
         )
         OR MIV_BLOCKID2 IN
         (
            Select
               MID_DEPBLOCKID
            from
               R5MAINISOLATIONDEPBLOCKS
            where
               MID_BLOCKID = <param.blockid>
         )
   )
