0

3つ以上のSQLテーブルがあります。

今、私はselect count(*)すべてのテーブルからしようとしていますが、どうすればこれを行うことができますか?

すべてのテーブルにデータが存在するかどうかを知りたい

I need to check the row count from previous business day ~15% for any table and it sends an email alert

私は次のように試しました私が完了するのを手伝ってください

PROCEDURE [dbo].[SendEmail_WSOTableDataAlert]
AS
BEGIN

declare @err int
IF NOT EXISTS (select 1 from T1) OR
NOT EXISTS (select 1 from T2)
BEGIN
set @error=1
END

//here i need to show which table is having empty data how can i do this please help

SET @tableHTML = @tableHTML +  +
            '</TABLE>' + @EmailFooter;



@error =1

then

メールを送る

END
4

5 に答える 5

2

ゼロカウントを示すフラグを一緒に乗算してみることができます。それらのいずれかがゼロの場合、結果はゼロになります。

select (case when (select count(*) from table1)=0 then 0 else 1 end
      *case when (select count(*) from table2)=0 then 0 else 1 end
      *case when (select count(*) from table3)=0 then 0 else 1 end) as no_zeros

どのテーブルにすべてゼロがあるかを知りたい場合は、クエリを次のように変換できます。

select (case when (select count(*) from table1)=0 then 1 else 0 end
      +case when (select count(*) from table2)=0 then 2 else 0 end
      +case when (select count(*) from table3)=0 then 4 else 0 end
      +case when (select count(*) from table4)=0 then 8 else 0 end) as no_zeros

フラグとして2の累乗(1、2、4、8、16、32など)を使用します。結果1のバイナリ表現にあるものは、どのテーブルにレコードがないかを示します。

于 2012-07-10T14:21:48.897 に答える
2
Select 
    case when count(*) = 0 then 
        'No rows' 
    else 
        'Has rows'
    end 
FROM
(
   Select * from @table1
   UNION ALL
   Select * from @table2
   UNION ALL
   Select * from @table3
) t

アップデート

これにより、すべてに少なくとも1つの行があり、いずれかの行にレコードがない場合は失敗します。

Select 
    case when count(*) = 0 then 
        'No rows' 
    else 
        'Has rows'
    end 
FROM
(
   Select top 1 1 found from @table1
   intersect
   Select top 1 1 found from @table2
   intersect
   Select top 1 1 found from @table3
) t
于 2012-07-10T14:22:25.950 に答える
1

このEXISTSキーワードを使用すると、テーブルにデータがあるかどうかを効率的に確認できます。

IF NOT EXISTS (SELECT 1 FROM Table1) OR NOT EXISTS (SELECT 1 FROM Table2) OR NOT EXISTS (SELECT 1 FROM Table3) 
BEGIN
   /* do something */
END
于 2012-07-10T14:25:37.893 に答える
1

table1からcount()を選択)union all(table2からcount()を選択)union all(table3からcount(*)を選択)

そして、結果の行をループします

于 2012-07-10T14:23:09.643 に答える
1
declare @count1 int 
select @count1 = count(*)
from table1

declare @count2 int 
select @count2 = count(*)
from table2

declare @count3 int 
select @count3 = count(*)
from table3

if (@count1 + @count2 + @count3 = 0)
    --do something
else 
    --do something else
于 2012-07-10T14:23:16.133 に答える