检查两个选择语句之间的条件

问题描述 投票:0回答:2

如果一个表或两个表中的一个表中的记录为零,则需要将它们放入临时表中。

我们如何在SQL Server中编写以下语句。

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
sql sql-server tsql group-by sql-insert
2个回答
1
投票

简单的not existsunion all结合可以达到目的:

select 'The data did not load into Customers table' Error
into #temp
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);

0
投票

如果我正确地跟随了您,则可以如下使用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
© www.soinside.com 2019 - 2024. All rights reserved.