【问题标题】:How to insert, update, delete when import data from table to table?从表到表导入数据时如何插入、更新、删除?
【发布时间】:2012-11-06 10:23:18
【问题描述】:

我有一个查询需要每天运行多次。此查询正在将数据从一个数据库导入另一个。

目标表结构为:

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 的值将不同。在这种情况下,我不会更新此记录。

如果源表和目标表中的状态不同并且目标表中的值 State = StateSource,我将更新。

当目标表中不存在该记录时,我将插入一条记录。当记录已经存在时不要插入(无论是应用程序添加还是我的查询在第一次运行时添加)。

当我的源表和 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?

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

谁能帮助我达到预期的结果?

【问题讨论】:

  • 您正在删除所有不是来自应用程序插入/更新的数据...然后更改应用程序插入/更新的所有状态...然后插入所有不是应用程序插入/更新的数据..您的状态字段就像是更新时的意思?喜欢更新版本吗?

标签: sql sql-server stored-procedures


【解决方案1】:

你可以使用合并语句......像这样......

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);

如果您通过声明表和插入一些数据来设置示例...

我应该提供更多帮助,提供真正有效的代码......而不仅仅是一个示例......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多