在DB2中运行2个select语句,具体取决于其中一个语句的结果

ggazkfy8  于 2022-11-07  发布在  DB2
关注(0)|答案(2)|浏览(313)

我尝试在DB2中使用2个select语句执行SP。如果第一个select返回null,则执行第二个select。
比如说

Select a, b, c from table A where...

--如果第一次选择返回null

Select a, from table B where...

我试了很多办法,但没有一个奏效。
谢谢

e37o9pze

e37o9pze1#

您可以使用此通用模式,当然您必须调整两个结果集以使其匹配

WITH first AS 
(
SELECT ..result1.. FROM table1
WHERE ..clause1.. 
)
SELECT ..result1.. FROM first
UNION
SELECT ..result2.. FROM table2
WHERE 0=(SELECT COUNT(1) FROM first)
AND
..clause2..
7vhp5slm

7vhp5slm2#

下面是一个简单的写法

Select a, from table B where...
and not exists (select * from table a where...)
union 
select a,.. from table A)

相关问题