如果一个表或两个表中的一个表中的记录为零,则需要将它们放入临时表中。
我们如何在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
简单的not exists
与union 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);
如果我正确地跟随了您,则可以如下使用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