PLS-00405:在这种情况下不允许子查询

时间:2019-06-10 14:43:05

标签: oracle select plsql

我正在开发一个启动和停止服务的触发器。关键是我的触发器如果首先发现以下错误,则不会停止服务:

ORA-00604: error occurred at recursive SQL level 3
ORA-44303: service name exists
ORA-06512: at "SYS.DBMS_SERVICE_ERR", line 21
ORA-06512: at "SYS.DBMS_SERVICE", line 316

因此,为了避免收到错误,我需要首先检查该服务是否在线,并且只有在停止时才停止它,但是PL / SQL不接受select into if语句。我该怎么办?

CREATE OR REPLACE TRIGGER managed_service_start
    AFTER STARTUP ON DATABASE
DECLARE
    pdb_role VARCHAR2(64);
    host VARCHAR2(64);
    pdb_name VARCHAR2(64);
    rw_service VARCHAR2(64);
    ro_service VARCHAR2(64);
BEGIN
    SELECT database_role INTO pdb_role FROM v$database;
    SELECT host_name INTO host FROM v$instance;
    SELECT Upper(sys_context ('userenv', 'con_name')) INTO pdb_name FROM dual;
    SELECT rw_service into rw_service FROM dual;
    SELECT ro_service into ro_service FROM dual;
    IF pdb_role = 'PRIMARY' THEN
        DBMS_SERVICE.START_SERVICE(rw_service);
        IF (ro_service) IN (SELECT name FROM v$services) THEN
            DBMS_SERVICE.STOP_SERVICE(ro_service);
        END IF;
    ELSE
        -- IF host IN ('host_name1','host_name2') THEN
        IF host LIKE '%de%' THEN
            DBMS_SERVICE.START_SERVICE(ro_service);
            IF (rw_service) IN (SELECT name FROM v$services) THEN
                DBMS_SERVICE.STOP_SERVICE(rw_service);
            END IF;
        ELSE
            IF (ro_service) IN (SELECT name FROM v$services) THEN
                DBMS_SERVICE.STOP_SERVICE(ro_service);
            END IF;
        END IF;
    END IF;
END;
/


    LINE/COL ERROR
-------- -----------------------------------------------------------------
15/3     PL/SQL: Statement ignored
15/22    PLS-00405: subquery not allowed in this context
22/4     PL/SQL: Statement ignored
22/23    PLS-00405: subquery not allowed in this context
26/4     PL/SQL: Statement ignored
26/23    PLS-00405: subquery not allowed in this context

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您从何处获取rw_servicero_service的值,因此这里仅以示例进行硬编码。

CREATE OR REPLACE TRIGGER managed_service_start
    AFTER STARTUP ON DATABASE
DECLARE
    pdb_role VARCHAR2(64);
    host VARCHAR2(64);
    pdb_name VARCHAR2(64);
    rw_service VARCHAR2(64) := 'rw_service_name';
    ro_service VARCHAR2(64) := 'ro_service_name';
    rw_up varchar2(1);
    ro_up varchar2(1);
BEGIN
    SELECT database_role INTO pdb_role FROM v$database;
    SELECT host_name INTO host FROM v$instance;
    SELECT Upper(sys_context ('userenv', 'con_name')) INTO pdb_name FROM dual;
    -- validate that services are up. if they are down the variable will be null.
    select max('Y') into rw_up
            from v$services
            where name = rw_service;
    select max('Y') into ro_up
            from v$services
            where name = ro_service;
    IF pdb_role = 'PRIMARY' THEN
        DBMS_SERVICE.START_SERVICE(rw_service);
        IF (ro_up is not null) THEN
            DBMS_SERVICE.STOP_SERVICE(ro_service);
        END IF;
    ELSE
        -- IF host IN ('host_name1','host_name2') THEN
        IF host LIKE '%de%' THEN
            DBMS_SERVICE.START_SERVICE(ro_service);
            IF (rw_up is not null) THEN
                DBMS_SERVICE.STOP_SERVICE(rw_service);
            END IF;
        ELSE
            IF (ro_up is not null) THEN
                DBMS_SERVICE.STOP_SERVICE(ro_service);
            END IF;
        END IF;
    END IF;
END;
/

不是将查询放入IF语句中,而是将IF逻辑放入查询中,并在IF语句中使用结果。