简体   繁体   English

如何确保将所有行都插入到数据模型中,然后截断临时表

[英]How to make sure that all the rows are inserted into datamodel and then truncate staging table

In my ETL job I would like to truncate stg table but before truncating stging table, I want to make sure that all the records are inserted in the data model. 在我的ETL作业中,我想截断stg表,但是在截断stging表之前,我想确保所有记录都已插入到数据模型中。 The sample is as below 示例如下

create table #stg (
    CreateID int,
    Name nvarchar(10)
)
insert into #stg
select 1, 'a' union all
select 2, 'b' union all
select 3, 'c' union all
select 4, 'd' 

go

create table #Approve1 (
    CreateID int,
    Name nvarchar(10)
)
insert into #Approve1 
select 1, 'a' union all
select 4, 'd' 

create table #Approve2 (
    CreateID int,
    Name nvarchar(10)
)
insert into #Approve2
select 3, 'c'

create table #Reject (
    CreateID int,
    Name nvarchar(10)
)
insert into #Reject
select 2, 'b'



select * from #stg
select * from #Approve1
select * from #Approve2
select * from #Reject`

How can I check among these tables and make sure that all values are loaded into the data model and the staging table can be truncated. 如何检查这些表,并确保所有值都已加载到数据模型中,并且可以将登台表截断。

Thanks 谢谢

You can check that the values exist in at least one of the tables. 您可以检查至少两个表中是否存在这些值。 Here is a method that counts the number of records remaining in the staging table using not exists : 这是一种使用not exists计数登台表中剩余记录数的方法:

select count(*)
from #stg s
where not exists (select 1 from #Approve1 a where a.CreateId = s.CreateId and a.name = s.name) and
      not exists (select 1 from #Approve2 a where a.CreateId = s.CreateId and a.name = s.name) and
      not exists (select 1 from #Reject r where r.CreateId = s.CreateId and r.name = s.name);

If this returns a value greater than 0 , then not everything was inserted. 如果返回的值大于0 ,则不会插入所有内容。 (And by using select * you can see what is missing.) (通过使用select *您可以查看缺少的内容。)

Of course, this doesn't guarantee what you want, because the values could have existed in the table earlier. 当然,这不能保证您想要的,因为这些值可能早先存在于表中。 I assume this is a simplified table structure and you have some mechanism for identifying the most recently inserted records. 我假设这是一个简化的表结构,并且您具有一些用于标识最近插入的记录的机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM