Oracle在程序内部选择查询错误

时间:2013-12-25 11:19:18

标签: sql oracle stored-procedures plsql syntax-error

我在程序中运行select查询时遇到错误。错误打印如下:

  

[错误] PLS-00428(24:9):PLS-00428:此SELECT语句中需要INTO子句

CREATE OR REPLACE PACKAGE BODY PACK_EMP
IS
    PROCEDURE find_employee(
        P_ID NUMBER,
        P_ERR OUT VARCHAR2
    )
    IS
    BEGIN
        IF P_ID IS NULL THEN
            SELECT * FROM EMPLOYEE WHERE ID = P_ID
        ELSE
            P_ERR := 'An error occured on database!!';
        END IF;
        EXCEPTION
        WHEN OTHERS THEN
            P_ERR := SQLERRM;
    END;
END;

2 个答案:

答案 0 :(得分:0)

CREATE OR REPLACE PACKAGE BODY PACK_EMP
IS
    PROCEDURE find_employee(
        P_ID NUMBER,
        P_ERR OUT VARCHAR2,
        V_VAL OUT VARCHAR
    )
    IS
    BEGIN
        IF P_ID IS NULL THEN
            SELECT FIRTSNAME ||' '||LASTNAME INTO V_VAL FROM EMPLOYEE WHERE ID = P_ID
        ELSE
            P_ERR := 'An error occured on database!!';
        END IF;
        EXCEPTION
        WHEN OTHERS THEN
            P_ERR := SQLERRM;
    END;
END;

答案 1 :(得分:0)

使用rowtype。另外我认为你的意思是说“如果P_ID是 那么就是那么......”因为如果它为null则你的选择永远不会有用。

CREATE OR REPLACE PACKAGE BODY PACK_EMP
IS
    PROCEDURE find_employee(
        P_ID NUMBER,
        P_ERR OUT VARCHAR2
    )
    IS
    empRec employee%rowtype; /* ADD THIS LINE */
    BEGIN
        IF P_ID IS NOT NULL THEN
            SELECT * into empRec FROM EMPLOYEE WHERE ID = P_ID;
            --now you can reference the columns like this: empRec.id
        ELSE
            P_ERR := 'An error occured on database!!';
        END IF;
        EXCEPTION
        WHEN OTHERS THEN
            P_ERR := SQLERRM;
    END;
END;