我的表tbl的值如下:
+-------+
|name |
+-------+
|n1 |
|n2 |
+-------+
我想要的是一个查询,可同时将值n1和n2存储到两个不同的变量中。
declare
val1 varchar2(2);
val2 varchar2(2);
begin
select name
into --val1,val2
from tbl
where ...
end;
val1的值必须为n1,而val2的值必须为n2
答案 0 :(得分:2)
使用简单的聚合方式:
select max(name), min(name)
into val1, val2
from tbl;
还可用于非数字变量。
或者在单个查询中使用相关子查询:
select ( select name from tbl where name = 'n1' ),
( select name from tbl where name = 'n2' )
into val1, val2
from dual;
答案 1 :(得分:0)
要使其可读,可以在语句中添加2个select:
select name
into val1
from tbl
where name = 'n1';
select name
into val2
from tbl
where name = 'n2';