检查两个select语句之间的条件

quhf5bfb  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(436)

如果一个表或两个表的表中都没有记录,我们需要将它们放入一个临时表中。
如何在SQLServer中编写下面的语句。

select Total
into #t1 
from
(    
    select Total = 'The data did not load into customers table'
    from 
    (
        select count(*) as total
        from customers  
        having count(*) = 0
    ) a

    OR

    select Total= 'The data did not load into Employees table'
    from 
    (
        select count(*) as total
        from Employees  
        having count(*)=0
    ) a
) b
smtd7mpg

smtd7mpg1#

一个简单的 not exists 结合了 union all 应该做到:

select 'The data did not load into Customers table' Error
into #t1
where not exists (select 1 from Customers)
union all
select 'The data did not load into Employees table' Error
where not exists (select 1 from Employees);
gpnt7bae

gpnt7bae2#

如果我没弄错的话,你可以 union all 具体如下:

insert into #t1(total)
select 'The data did not load into customers table' from customers having count(*) = 0
union all
select 'The data did not load into employees table' from employees having count(*) = 0

相关问题