SQL - 检查另一个表上的数据是否也存在

时间:2012-11-25 19:11:31

标签: sql oracle

我正在尝试检查表trkdriver和trkmechanic中是否存在L#;

我创建了一个程序,它将接受一个L#,但问题是如何检查L#存在于trkdriver和trkmechanic中。

execute v_emp(10001);

PLSQL代码:

CREATE OR REPLACE procedure v_emp
(emp_license IN trkdriver.L#%TYPE) 
IS
result VARCHAR2(10);

BEGIN
select e# INTO result
from trkdriver where l#=emp_license;

DBMS_OUTPUT.PUT_LINE('RESULT' || result);
END;
/

SQL>从trkdriver中选择E#;

        E#
----------
         1
         3
         5
         7
         9
        11
        13

选择了7行。

我在下面尝试检查它是否存在于两个表中:

SQL> select E# from trkdriver where EXISTS (select * from trkmechanic where trkd
river.L# = trkmechanic.L#);

它给我0行返回..

SQL> desc trkdriver;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 E#                                        NOT NULL NUMBER(12)
 L#                                        NOT NULL NUMBER(8)
 STATUS                                    NOT NULL VARCHAR2(10)

SQL> desc trkmechanic;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 E#                                        NOT NULL NUMBER(12)
 L#                                        NOT NULL NUMBER(8)
 STATUS                                    NOT NULL VARCHAR2(10)
 EXPERIENCE                                NOT NULL VARCHAR2(10)

2 个答案:

答案 0 :(得分:0)

尝试此查询:

select td.e# INTO result
from trkdriver td
join trkmechanic tm on tm.L#= td.L#
where rd.l#=emp_license;

答案 1 :(得分:0)

BEGIN
  EXISTS 
    (SELECT A.L# 
       from trkdriver AS A 
          INNER JOIN
          trkmechanic AS B
          ON A.L#=B.L#
          WHERE A.L# = emp_license
    )
END