2012-11-06 106 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); 

如果設置與申報表和插入一些數據的樣本...

我要幫助更多的,與一個真正有效的代碼..不只是一個樣本...

相關問題