PLS-00103:遇到符号" IF"

时间:2018-02-19 01:48:38

标签: oracle

我这样做

DECLARE v_Valid_Cnt NUMBER;

BEGIN
SELECT COUNT(*) cnt INTO v_Valid_Cnt
FROM
(    
SELECT * FROM TABLE1    
MINUS 
SELECT * FROM TABLE2
);
dbms_output.put_line('CNT'||v_Valid_Cnt);
IF (v_Valid_Cnt = 0) 
THEN    
SELECT 'IFSTMT' FROM DUAL;
ELSE
SELECT 'ELSESTMT' FROM DUAL;       
END IF;
END;

我可以正确地将dbms输出视为0,在检查时if else条件我收到错误 PLS-00428:此SELECT语句中需要INTO子句

1 个答案:

答案 0 :(得分:1)

当你做BEGIN时 - >你已经进入了PL / SQL世界。

在IN PL / SQL中,你可以做一个SELECT,但它是一个SELECT INTO

Docs

这有效:

set serveroutput on
DECLARE
    v_valid_cnt   NUMBER;
    message       varchar2(25);
BEGIN
    SELECT
        COUNT(*) cnt
    INTO v_valid_cnt
    FROM
        (
            SELECT
                *
            FROM
                employees
            MINUS
            SELECT
                *
            FROM
                emps_excel_copy
        );

    dbms_output.put_line('CNT: ' || v_valid_cnt);
    IF
        ( v_valid_cnt = 0 )
    THEN
        SELECT
            'IFSTMT' into message
        FROM
            dual;

    ELSE
        SELECT
            'ELSESTMT' into message
        FROM
            dual;

    END IF;

-- and don't forget to print your message!
 dbms_output.put_line(message);

END;