在PL/SQL中,可以巧妙利用rownum來實現對結果集的篩選和排序。rownum是一個偽列,它表示查詢結果集中的行號,從1開始遞增。下面是一些利用rownum的常見用法:
select *
from table_name
where rownum <= N;
select *
from (
select t.*, rownum rnum
from table_name t
where rownum <= end_row
)
where rnum >= start_row;
select *
from (
select *
from table_name
order by column_name
)
where rownum <= N;
delete from table_name
where rowid not in (
select min(rowid)
from table_name
group by column_name
);
通過巧妙地利用rownum,可以實現更靈活的查詢和操作結果集的功能。