oracle 如果一个表没有返回任何值,如何从另一个表中获取值- plsql

rlcwz9us  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(145)

我有两个视图(A和B),这两个视图都有Inquirey_no,Revision_no,Total作为列。我总是先查询视图A,如果视图A没有返回任何行,我想查询第二个视图(B)并获得结果。
我试着跟踪,但它给了我错误。

SELECT 
   CASE 
     WHEN a.inquiry_no is NULL THEN b.inquiry_no
     ELSE a.inquiry_no
   END inquiry_no,
   CASE 
     WHEN a.revision_no is NULL THEN b.revision_no
     ELSE a.revision_no
   END revision_no,
   CASE 
     WHEN a.total is NULL THEN b.total
     ELSE a.total
   END total
FROM A a, B b

如果视图A有记录,它应该从查询中获取,如果只有视图A没有记录,那么我想从视图B中获取记录。
有人能帮我一下吗?
使用结果集编辑:
假设testa有以下内容。

查询_无修订版_无总计

1 2 3
testb有以下内容

查询_无修订版_无总计

100 200 300
如果我加上如下的where条件,预期的结果如下。
1.其中inquiry_no = 1 -> 1 2 3
1.其中inquiry_ no = 100 -> 100 200 300
1.其中inquiry_no = 11 ->未找到记录
能帮我拿一下吗?

7uzetpgm

7uzetpgm1#

一个选项是UNION从两个表中选择,使用exists(从testa中选择时)和not exists(从testb中选择时)。
最初,两个表都有数据,这意味着查询应该只从testa中选择:

SQL> insert into testa values (1, 2, 3);

1 row created.

SQL> insert into testb values (100, 200, 300);

1 row created.

查询方式:

SQL> select *
  2  from testa
  3  where exists (select null
  4                from testa
  5               )
  6  union
  7  select *
  8  from testb
  9  where not exists (select null
 10                    from testa
 11                   );

INQUIRY_NO REVISION_NO      TOTAL
---------- ----------- ----------
         1           2          3       --> this belongs to TESTA table

让我们从testa中删除行:

SQL> delete from testa;

1 row deleted.

再试一次;结果现在应该来自testb

SQL> select *
  2  from testa
  3  where exists (select null
  4                from testa
  5               )
  6  union
  7  select *
  8  from testb
  9  where not exists (select null
 10                    from testa
 11                   );

INQUIRY_NO REVISION_NO      TOTAL
---------- ----------- ----------
       100         200        300    --> really, this belongs to TESTB

SQL>

当你用plsql标记这个问题时,让我们切换到它:

SQL> create or replace procedure p_test is
  2  begin
  3    for cur_r in (
  4            select *
  5          from testa
  6          where exists (select null
  7                        from testa
  8                       )
  9          union
 10          select *
 11          from testb
 12          where not exists (select null
 13                            from testa
 14                           )
 15    ) loop
 16      dbms_output.put_line(cur_r.inquiry_no);
 17    end loop;
 18  end;
 19  /

Procedure created.

初始样本数据:

SQL> set serveroutput on
SQL> truncate table testa;

Table truncated.

SQL> truncate table testb;

Table truncated.

SQL> insert into testa values (1, 2, 3);

1 row created.

SQL> insert into testb values (100, 200, 300);

1 row created.

测试:

SQL> exec p_test;
1                             --> this is from TESTA

PL/SQL procedure successfully completed.

SQL> delete from testa;

1 row deleted.

SQL> exec p_test;
100                          --> this is from TESTB

PL/SQL procedure successfully completed.

SQL>

[编辑],基于附加说明和样本数据以及预期结果。

SQL> select * from testa;

INQUIRY_NO REVISION_NO      TOTAL
---------- ----------- ----------
         1           2          3

SQL> select * From testb;

INQUIRY_NO REVISION_NO      TOTAL
---------- ----------- ----------
       100         200        300

稍微修改的查询,在两个表中搜索inquiry_no。如果testa中存在testb中的行,则not exists将删除这些行。

SQL> with temp as
  2    (
  3      select *
  4      from testa
  5      where inquiry_no = &&par_inquiry_no
  6      union
  7      select *
  8      from testb
  9      where not exists (select null
 10                        from testa
 11                        where inquiry_no = &&par_inquiry_no
 12                       )
 13        and inquiry_no = &&par_inquiry_no
 14    )
 15  select *
 16  from temp;

测试:

Enter value for par_inquiry_no: 1

INQUIRY_NO REVISION_NO      TOTAL
---------- ----------- ----------
         1           2          3

SQL> undefine par_inquiry_no
SQL> /
Enter value for par_inquiry_no: 100

INQUIRY_NO REVISION_NO      TOTAL
---------- ----------- ----------
       100         200        300

SQL> undefine par_inquiry_no
SQL> /
Enter value for par_inquiry_no: 11

no rows selected

SQL>

相关问题