【问题标题】:If record doesn't exist,generate AnnouncementID,if record exists,don't generate AnnouncementID,how to do it? [duplicate]如果记录不存在,生成AnnouncementID,如果记录存在,不生成AnnouncementID,怎么办? [复制]
【发布时间】:2018-11-19 15:11:57
【问题描述】:

我有 2 个这样的表:

CREATE TABLE t1
(
    [SupplyTitle] [NVARCHAR](50) NOT NULL,
    [EmployeeCode] INT NOT NULL,
    [registered] DATETIME,
    [modified]  DATETIME
) 

CREATE TABLE t2
(
    [SupplyTitle] [NVARCHAR](50) NOT NULL,
    [EmployeeCode] INT NOT NULL,
    [AnnouncementID] INT NOT NULL
    [registered] DATETIME,
    [modified]  DATETIME
) 

t1 中的记录是这样的:

SupplyTitle  EmployeeCode  registered                 modified
-----------------------------------------------------------------------------
aaa          9001         2018-11-15 15:41:36.613     2018-11-15 15:41:36.613
bbb          9002         2018-11-15 15:42:36.613     2018-11-15 15:42:36.613
ccc          9003         2018-11-15 15:43:36.613     2018-11-15 15:43:36.613

t2中的AnnouncementID是由这个过程R_GetManageID生成的:

DECLARE @ManageID TABLE(ManageID INT)   
DECLARE @AnnouncementID int         

INSERT INTO @ManageID           
EXEC  R_GetManageID         

SET @AnnouncementID = (SELECT TOP 1 * FROM @ManageID)       
SELECT @AnnouncementID

我想将数据从 t1 插入到 t2。

插入规则为:

  • 如果t2中没有t1的记录,则插入t2,关联条件为t1.SupplyTitle = t2.SupplyTitle and t1.EmployeeCode = t2.EmployeeCode

  • 如果t2中存在t1'记录,则更新t2.modified = getdate()

也就是说t2的AnnouncementID只有在没有达到关联条件的时候才会生成@AnnouncementID

比如一开始T2是空的。

t1 中的记录是这样的:

SupplyTitle  EmployeeCode  registered                 modified
    aaa          9001         2018-11-15 15:41:36.613     2018-11-15 15:41:36.613
    bbb          9002         2018-11-15 15:42:36.613     2018-11-15 15:42:36.613
    ccc          9003         2018-11-15 15:43:36.613     2018-11-15 15:43:36.613

如果@AnnouncementID的初始值为1001,则该值每次加一。

那么t2中生成的记录是这样的:

SupplyTitle  EmployeeCode AnnouncementID     registered                 modified
aaa          9001         1001       2018-11-19 12:00:00.000     2018-11-19 12:00:00.000
bbb          9002         1002       2018-11-19 12:00:00.000     2018-11-19 12:00:00.000
ccc          9003         1003       2018-11-19 12:00:00.000     2018-11-19 12:00:00.000

那么t1中的记录就变成了这样:

SupplyTitle  EmployeeCode  registered                 modified
aaa          9001         2018-11-15 15:41:36.613     2018-11-15 15:41:36.613
bbb          9002         2018-11-15 15:42:36.613     2018-11-15 15:42:36.613
www          9008         2018-11-15 15:43:36.613     2018-11-15 15:43:36.613

所以 t2 中的记录应该像这样添加一行:

SupplyTitle  EmployeeCode AnnouncementID     registered                 modified
aaa          9001         1001       2018-11-19 12:00:00.000     2018-11-19 12:00:00.000
bbb          9002         1002       2018-11-19 12:00:00.000     2018-11-19 12:30:00.000
ccc          9003         1003       2018-11-19 12:00:00.000     2018-11-19 12:30:00.000
www          9008         1004       2018-11-19 12:30:00.000     2018-11-19 12:30:00.000

前两条记录的modified 发生了变化。

那么我怎样才能插入(更新)t2 喜欢这个?

【问题讨论】:

  • 您是否尝试过任何代码来插入或更新表格?
  • 您的解决方案是MERGE。看看下面的链接:Using MERGE in SQL Server to insert, update and delete at the same time 使用MERGE 可以帮助你同步两个表。
  • @Mohammad Mohabbati 我不知道怎么处理,所以我还没有。
  • 您需要unique key 来比较两个表中的数据。您的表中是否有任何唯一键?
  • 或在您的表中构成唯一键的某些字段的组合?

标签: sql sql-server sql-server-2008


【解决方案1】:

使用MERGE 可以帮助您同步两个表,如下所示:

merge t2 as t
using t1 as s
on
(
    t.SupplyTitle = s.SupplyTitle
    and
        t.EmployeeCode = s.EmployeeCode
)
when matched then
    update
    set
        t.registered = s.registered, --any other data
        t.modified = s.modified,
        t.AnnouncementID = 0--'your_data'
when not matched by target then
    insert
        (
            SupplyTitle,
            EmployeeCode,
            AnnouncementID,
            registered,
            modified
        )
    values
        (
            s.SupplyTitle,
            s.EmployeeCode,
            0,--'your_data',
            s.registered,
            s.modified
        )

output $action, 
       deleted.SupplyTitle      as target_SupplyTitle, 
       deleted.EmployeeCode     as target_EmployeeCode, 
       deleted.registered       as target_registered,
       deleted.modified         as target_modified,
       inserted.SupplyTitle     as source_SupplyTitle, 
       inserted.EmployeeCode    as source_EmployeeCode, 
       inserted.registered      as source_registered,
       inserted.modified        as source_modified;

查看Reference Link 以了解其工作原理。

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2014-04-04
    • 1970-01-01
    相关资源
    最近更新 更多