2012-11-06 109 views
0

我有一个查询,我需要每天运行多次。该查询将数据从数据库导入到另一个数据库。如何插入,更新,删除从表格中导入数据?

目标表的结构是:

Id Date  Department Location PersonId Starttime  EndTime  State 
1 2012-01-01  2   5  200  12:00:00.000 15:00:00.000 2 

应用程序也可以插入数据到目标表。当该记录存在于具有另一个状态的源(临时表)表中时,应用程序插入的记录也可能不会更新。

为了使这成为可能我有一个解决方案创建。我将在第二个状态下在目标表中创建一个新列,以便我可以检查。

Id Date  Department Location PersonId Starttime  EndTime  State StateSource 
1 2012-01-01  2   5  200  12:00:00.000 15:00:00.000 2  2 

一些要求:

如果一个记录是由比StateSource的应用程序增加将是NULL。意味着该记录不能从源表中删除,更新或再次插入。

如果应用程序更新了记录,则State和StateSource的值将不同。在这种情况下,我不更新此记录。

如果来自sourcetable和targettable的状态不相同并且来自目标表State = StateSource的值,我将更新。

我会在目标表中不存在的情况下插入一条记录。当记录已经存在时,不要插入(不管这是由应用程序添加还是在第一次运行时添加我的查询)。

当我的sourcetable和State = StateSource中不存在记录时,我将从目标中删除这些记录。

我已经有以下查询。我决定做3个陈述。

--Delete Statement first 
Delete from t 
from TargetTable t LEFT JOIN SourceTable s ON t.Id=s.Id 
and t.Date=s.Date 
and t.departments=s.Department 
and t.PersonId=s.PersonId 
and t.State=t.StateSource 

--Just delete if a date is no more exists from the source table and this records is NOT 
--changed by the application (t.State=t.StateSource) 


--Update statement second 
Update t 
set t.State = s.State 
From Targettable t INNER JOIN SourceTable s ON t.Id=s.Id 
and t.Date=s.Date 
and t.departments=s.Department 
and t.PersonId=s.PersonId 

The problem here is: 
--when I have State 2 already in the targettable and in my sourcetable i have 
--another state then the state in the targettable changes. This would not be the case. 


--Insert Statement thirth 

insert into TargetTable (Id, Date, Department, Location, PersonId, Starttime, EndTime,State, StateSource) 
select Id, Date, Department, Location, PersonId, Starttime, EndTime,State, StateSource 
from SourceTable s 
WHERE Date not in (select Date 
        from TargetTable t 
        where t.id=s.id 
        and t.PersonId=s.PersonId 
        and t.date=s.date 
        and t.department=s.department)  

--I have no idea about how the insert should be because the application also can 
--insert records. When a record exists then no insert. What to do with the State? 

请记住,由应用程序更改的状态是领先的。

任何人都可以帮助我达到预期的效果吗?

+0

你正在删除所有不是从应用程序插入/更新的数据...然后改变应用程序插入/更新的所有状态...然后插入所有不是应用程序插入/更新..你的状态字段就像是什么时候的意思更新?像更新版本? – Frederic

回答

0

您可以使用MERGE语句..这样的事情...

with target_T as (select * from UR_TARGET_TABLE 
        where statesource is not null) -- to dont change the data inserted from application... 
merge target_T as TARGET 
using UR_SOURCE_TABLE as SOURCE 
on SOURCE.id = TARGET.id -- id is unique? anyway, put your primary key here... 

when matched and TARGET.state = TARGET.statesource then --if inserted/updated from application, will not change data 
update set TARGET.state = SOURCE.state 
      ,TARGET.statesource = SOURCE.state --important update it together to be different from an application update 
     --, other collumns that you have to set... 

--should use another when matched then update if need to change something on inserted/updated from application data 


when not matched by TARGET then 
     insert (Id, Date, Department, Location, PersonId, Starttime, EndTime,State, StateSource) 
     values(SOURCE.Id, SOURCE.Date, SOURCE.Department, SOURCE.Location, SOURCE.PersonId, SOURCE.Starttime, SOURCE.EndTime,SOURCE.State, SOURCE.StateSource); 

如果设置与申报表和插入一些数据的样本...

我要帮助更多的,与一个真正有效的代码..不只是一个样本...

相关问题