比较 SQL 中 operation/load 前后的 COUNT 个结果

Compare COUNT results before and after operation/load in SQL

我的一个案例需要帮助。

  1. 假设我有一个 table,只有一个名为 CustomerID 的列,有 1500 条记录。
  2. CustomerID table 每天在数据库中加载 2 次 - 上午 10 点和晚上 10 点
  3. 我想比较早上(上午 10 点)和晚上(晚上 10 点)
  4. 的 CustomerID table

SELECT COUNT(*) from CustomerID -- 10 AM / 1500 records.

SELECT COUNT(*) from CustomerID -- 10 PM / 1510 records.

我想检查这 10 条额外的记录 - 仅检查计数,仅此而已。

主要思想是跟踪 table,如果在晚上 10 点没有新记录 - 告诉负责人 table 已“损坏”,因为 table 应该是随着每次加载而增加的计数。

谢谢!

我最近为多个 DB 和 table 做了这个,但可以告诉你如何只为一个 table。

说明:

  1. 使用下面的查询创建存储过程(更新数据库和 table 姓名)

    *您需要创建 table 才能 运行 这个

  2. 把它放在 10a 和 10p 的工作计划中

  3. 每天检查或创建一个 visualization/dashboard 使用这个新的 table 作为数据 显示所有内容是否按应有的方式加载的来源

查询:

use [YOUR DB NAME]
go

create procedure [YOURSCHEMA.YOUR_NEW_AUDIT_TABLE_NAME] as

insert into [TABLE_NAME_YOU_WANT_TO_CREATE_FOR_TRACKING]
select schema_name(schema_id) as [schemaname],
       [tables].name as [tablename],
       sum([partitions].[rows]) as [totalrowcount],
       getdate() as date_checked
from sys.tables as [tables]
    join sys.partitions as [partitions] on [tables].[object_id] = [partitions].[object_id] and [partitions].index_id in ( 0, 1 )
where [tables].name = '[TABLE_NAME_YOU_WANT_TRACKED]'
group by schema_name(schema_id), [tables].name;


go;