简体   繁体   English

如何在plsql条件中解决子查询?

[英]How can I fix a subquery in a plsql conditional?

Currently, inside a loop, I have: 目前,在循环中,我有:

    IF(R1.CURRENT_STATE_CODE not in (select stvstat_code from stvstat))
        THEN
        v_cur_state := 'FR';
        ELSE
        v_cur_state := R1.CURRENT_STATE_CODE;
        END IF;

This fails because you cannot perform a subquery in a plsql conditional. 之所以失败,是因为您无法在plsql条件中执行子查询。 "subquery not allowed in this context" “在这种情况下不允许子查询”

How could I accomplish this instead 我该怎么做呢

Why not just set the value in a select ? 为什么不只是在select设置值?

select coalesce(max(s.stvstat_code, 'FR'))
into v_cur_state
from stvstat s
where R1.CURRENT_STATE_CODE = s.stvstat_code;

This is an aggregation query with no group by , so it always returns exactly one row. 这是一个聚合查询,没有group by ,因此它始终只返回一行。 If the where filters out all rows, then the MAX() returns NULL . 如果where过滤掉所有行,则MAX()返回NULL The COALESCE() then fills in a value in that case. 然后,在这种情况下, COALESCE()会填充一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM