将PL / SQL中的数据与我想要比较的输入进行比较

时间:2012-11-26 19:12:15

标签: oracle plsql

我想创建一个PLSQL PROCEDURE来验证DRIVER和MECHANIC中不存在EMPLOYEE的数据。

我创建了1条记录进行测试。但是,当我运行EXEC Verify(2)时,我不会创建用于测试的记录,它仍然会显示我不想验证的测试记录。

我喜欢用一套EMPLOYEE.E#执行PROCEDURE,我选择运行EMPLOYEE的比较在DRIVER和MECHANIC中不存在。

这是我创建的PLSQL。

CREATE OR REPLACE PROCEDURE Verify(enum IN EMPLOYEE.E#%TYPE) IS
eNo DRIVER.E#%TYPE;
eName EMPLOYEE.NAME%TYPE;

CURSOR c1 IS
    SELECT DRIVER.E#, EMPLOYEE.NAME
    FROM DRIVER
    INNER JOIN EMPLOYEE
    ON EMPLOYEE.E# = DRIVER.E#
    WHERE
    EXISTS (SELECT * FROM MECHANIC WHERE DRIVER.E# = MECHANIC.E#);

BEGIN   
    OPEN c1;
    FETCH c1 into eNo, eName;
    IF c1%NOTFOUND THEN
        CLOSE c1;               
        DBMS_OUTPUT.PUT_LINE('No Records Found');

    ELSE
        CLOSE c1;           
        DBMS_OUTPUT.PUT_LINE('E#| NAME');
        FOR EmpRecord IN c1
        LOOP
          DBMS_OUTPUT.PUT_LINE(EmpRecord.E# || ' | ' || EmpRecord.NAME || ' is in                   
              driver and mechanic.');
        END LOOP;
    END IF;

END;
/

1 个答案:

答案 0 :(得分:0)

试试此代码
CURSOR c1 IS
SELECT EMPLOYEE.E# , EMPLOYEE.NAME FROM EMPLOYEE WHERE EMPLOYEE.E#=enum AND NOT EXISTS (SELECT 'x' FROM MECHANIC a,DRIVER b WHERE a.E# = b.E# and a.E#=EMPLOYEE.#E);
我还没有测试过这个,但我希望它有所帮助。