簡體   English   中英

如何確保將所有行都插入到數據模型中,然后截斷臨時表

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

在我的ETL作業中,我想截斷stg表,但是在截斷stging表之前,我想確保所有記錄都已插入到數據模型中。 示例如下

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`

如何檢查這些表,並確保所有值都已加載到數據模型中,並且可以將登台表截斷。

謝謝

您可以檢查至少兩個表中是否存在這些值。 這是一種使用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);

如果返回的值大於0 ,則不會插入所有內容。 (通過使用select *您可以查看缺少的內容。)

當然,這不能保證您想要的,因為這些值可能早先存在於表中。 我假設這是一個簡化的表結構,並且您具有一些用於標識最近插入的記錄的機制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM