在oracle中设置多行的Nvl函数
Nvl function with multiple row set in oracle
我对 oracle 中的 NVL 函数有疑问。
如果我将 periods 变量发送为 null 它是 working.There query.I 中没有数据意味着 NVL 函数正常工作。
如果我发送 periods 变量,例如 10 ,它会给出这样的错误
single-row-subquery-returns-more-than-one
此代码块不起作用,因为 select 查询 returns 多于一行
Select ....... FROM students st, orders od
WHERE st.id IN NVL((select id from students
where student_id = 321
ORDER BY id desc
FETCH FIRST periods ROWS ONLY),(od.countNo))
我尝试在 where 块中使用 case when 但我无法使用 it.Do 你知道吗?
如果将 IN
更改为 =
,您会得到同样的错误。 IN
可以处理来自子查询的多行集合,但 NVL()
和 =
不能。
如果您坚持使用 NVL,请将您的子查询更改为 return 最多 1 行:
-- using periods=1
Select ....... FROM students st, orders od
WHERE st.id = NVL((select id from students
where student_id = 321
ORDER BY id desc
FETCH FIRST 1 ROWS ONLY),(od.countNo));
-- using an aggregate function
Select ....... FROM students st, orders od
WHERE st.id = NVL((select max(id) from students
where student_id = 321),(od.countNo));
或者如果您需要多行,请重写您的查询以不使用 NVL:
Select ....... FROM students st, orders od
WHERE EXISTS (select 1 from students
where student_id = 321
and st.id = students.id
ORDER BY students.id desc
FETCH FIRST periods ROWS ONLY)
OR st.id = od.countNo;
戈登昨天的回答的修改版本是另一个例子:
with s3 as (
select id
from students
where student_id = 321
ORDER BY id desc
FETCH FIRST periods ROWS ONLY
)
Select ....... FROM students st, orders od
where st.id in (select id from s3) or
(not exists (select 1 from s3) and st.id = od.countNo);
我对 oracle 中的 NVL 函数有疑问。 如果我将 periods 变量发送为 null 它是 working.There query.I 中没有数据意味着 NVL 函数正常工作。 如果我发送 periods 变量,例如 10 ,它会给出这样的错误
single-row-subquery-returns-more-than-one
此代码块不起作用,因为 select 查询 returns 多于一行
Select ....... FROM students st, orders od
WHERE st.id IN NVL((select id from students
where student_id = 321
ORDER BY id desc
FETCH FIRST periods ROWS ONLY),(od.countNo))
我尝试在 where 块中使用 case when 但我无法使用 it.Do 你知道吗?
如果将 IN
更改为 =
,您会得到同样的错误。 IN
可以处理来自子查询的多行集合,但 NVL()
和 =
不能。
如果您坚持使用 NVL,请将您的子查询更改为 return 最多 1 行:
-- using periods=1
Select ....... FROM students st, orders od
WHERE st.id = NVL((select id from students
where student_id = 321
ORDER BY id desc
FETCH FIRST 1 ROWS ONLY),(od.countNo));
-- using an aggregate function
Select ....... FROM students st, orders od
WHERE st.id = NVL((select max(id) from students
where student_id = 321),(od.countNo));
或者如果您需要多行,请重写您的查询以不使用 NVL:
Select ....... FROM students st, orders od
WHERE EXISTS (select 1 from students
where student_id = 321
and st.id = students.id
ORDER BY students.id desc
FETCH FIRST periods ROWS ONLY)
OR st.id = od.countNo;
戈登昨天的回答的修改版本是另一个例子:
with s3 as (
select id
from students
where student_id = 321
ORDER BY id desc
FETCH FIRST periods ROWS ONLY
)
Select ....... FROM students st, orders od
where st.id in (select id from s3) or
(not exists (select 1 from s3) and st.id = od.countNo);