在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,可以实现更灵活的查询和操作结果集的功能。