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

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

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

  1. select Total
  2. into #t1
  3. from
  4. (
  5. select Total = 'The data did not load into customers table'
  6. from
  7. (
  8. select count(*) as total
  9. from customers
  10. having count(*) = 0
  11. ) a
  12. OR
  13. select Total= 'The data did not load into Employees table'
  14. from
  15. (
  16. select count(*) as total
  17. from Employees
  18. having count(*)=0
  19. ) a
  20. ) b
smtd7mpg

smtd7mpg1#

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

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

gpnt7bae2#

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

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

相关问题