如果您正在使用PL/SQL,那么如果返回了多行,则选择使用列select-into将抛出too_many_rows异常:
declare
var table.column%type;
begin
select column
into var
from table
where ...;
end;
Run Code Online (Sandbox Code Playgroud)
如果您只想使用SQL来执行此操作,那么您可以执行以下操作:
select *
from
(select s.*, count(*) over () c
from
(select *
from table
where ...
and rownum <= 2
) s
)
where c = 1
Run Code Online (Sandbox Code Playgroud)
UPDATE
正如DazzaL在评论中所说,rownum <= 2限制的原因是如果结果集中有超过2行,则会使查询短路.如果数据集很大,这可以带来显着的性能优势.
| 归档时间: |
|
| 查看次数: |
6583 次 |
| 最近记录: |