select
  a.cucd, a.cuscd, a.spnm,
  a.cuedi
from afm03 d, afm05 c, afm036 b, afm032 a
where a.cucd = b.cucd(+) and a.cshcd = b.cshcd(+)
  and a.spktcd = c.ktktcd(+)
  and a.cucd = d.cucd(+) 
  and (:ccd is null or a.cucd=:ccd)
  and (:cd is null or a.cuscd like :cd||'%')
  and ((NVL(:stpflg, '0') = '0' ) or (NVL(:stpflg, '0') = '1' and a.spstp is null)) /*stpflg->0:廃止可,1:廃止不可(,2:マスタ無可)*/
order by a.cucd, a.cuscd
