select x.*,
	ceil(rnum / :lcnt) as pgno,
	0 chkval,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY a.cucd,a.cuscd,a.adat,a.vseq) as rnum,
		a.viseq,
		a.cucd,
		a.cunm,
		a.cuscd,
		a.spnm,
		a.spktcd,
		a.tcls,
		a.tclsnm,
		to_char(a.dat2, 'yyyy/mm/dd') as dat2,
		to_char(a.adat, 'yyyy/mm/dd') as adat,
		a.vseq,
		a.amt,
		a.amt2
	from af226v01 a
	where a.ktktcd=:ktktcd
		and (:adats is null or a.adat>=to_date(:adats, 'yyyy/mm/dd'))
		and (:adate is null or a.adat<=to_date(:adate, 'yyyy/mm/dd'))
		and (:cucd is null or a.cucd = :cucd)
		and (:cuscd is null or a.cuscd = :cuscd)
		and (:vseq is null or a.vseq = :vseq)
		and (:tcls is null or a.tcls = :tcls)
		and (:spktcd is null or a.spktcd = :spktcd)
		and (nvl(:pmode, '0') = '0'
			or (nvl(:pmode, '0') = '1' and Chk_Sbat(1,ktktcd,cucd,cuscd,sbat,sbat2,cuclcls)=0)	/*伝票検索*/
		)
		and (nvl(:pmode2, '0')= '0'
			or (nvl(:pmode2, '0') = '1' and hvseq is not null)	/*直送のみ*/
		)
		and (:cls = 'ALL'
			or (:cls not in ('ALL', '04') and tcls = :cls)
			or (:cls = '04' and tcls ='01' and hvseq is not null)
		)
	) x
	order by x.rnum
