简体   繁体   English

用于更新和插入的合并语句

[英]merge statement for update and insert

I am trying to use merge to combine an update and insert statement, although i am getting some error and im not quite sure its right.我正在尝试使用合并来组合更新和插入语句,尽管我遇到了一些错误并且我不太确定它是正确的。 The merge statement is within a stored proc.合并语句位于存储过程中。 Following is the merge statement.以下是合并语句。

MERGE dbo.seg AS TARGET
USING (SELECT segCreateDate
        , segDesc
        , modifiedDate
        , modifiedBy 
    FROM [update].[dbo].[seg] s
    ) AS source (segCreateDate
                , segDesc
                , modifiedDate
                , modifiedBy)
        ON  (dbo.[seg].segID = s.segID
            AND (
                 dbo.[seg].segCreateDate > s.segCreateDate
                 OR dbo.[seg].segDesc <> s.segDesc
                )
    )

WHEN MATCHED THEN
UPDATE dbo.seg SET
  target.segCreateDate = source.segCreateDate
  AND target.segDesc = source.segDesc
  AND target.modifiedDate = source.modifiedDate
  AND target.modifiedBy = source.modifiedBy

WHEN NOT MATCHED THEN
INSERT (segID
        , segCode
        , segDesc
        , segCreateDate
        , createdDate
        , createdBy
        , modifiedDate
        , modifiedBy
        )
VALUES (SELECT  segID
                ,segCode
                ,segDesc
                ,segCreateDate
                ,createdDate
                ,createdBy
                ,modifiedDate
                ,modifiedBy
         FROM [update].[dbo].[seg]);

Its the first time i am using merge so hope can get some help.这是我第一次使用合并,所以希望能得到一些帮助。

Thanks all谢谢大家

There were several problems with your merge statement.您的合并语句有几个问题。 This is what you probably wanted.这可能是你想要的。

merge dbo.seg as TARGET
using
    (
     select segCreateDate,
            segDesc,
            modifiedDate,
            modifiedBy
     from   [update].dbo.seg s
    ) as source (segCreateDate, segDesc, modifiedDate, modifiedBy)
on (
    TARGET.segID = source.segID
    and (
         TARGET.segCreateDate > source.segCreateDate
         or TARGET.segDesc <> source.segDesc
        )
   )
when matched then
    update set TARGET.segCreateDate = source.segCreateDate,
               TARGET.segDesc = source.segDesc,
               TARGET.modifiedDate = source.modifiedDate,
               TARGET.modifiedBy = source.modifiedBy
when not matched then
    insert (
            segID,
            segCode,
            segDesc,
            segCreateDate,
            createdDate,
            createdBy,
            modifiedDate,
            modifiedBy
           )
    values (
            source.segID,
            source.segCode,
            source.segDesc,
            source.segCreateDate,
            source.createdDate,
            source.createdBy,
            source.modifiedDate,
            source.modifiedBy
           );

Note that (1) when updating a table you don't write and after each column but merely add a comma.请注意 (1) 更新表时,您不会在每列之后写入and ,而只是添加一个逗号。 (2) If you are using Source and Target as the table alias then you should stick to them and don't change in the middle to s instead of source . (2) 如果您使用SourceTarget作为表别名,那么您应该坚持使用它们,不要在中间更改为s而不是source

Still the above code will probably not work because (at the end of the code) it is trying to insert into the target table more columns than there are in the source table.仍然上面的代码可能不会工作,因为(在代码的末尾)它试图在target表中插入比source表中更多的列。 At first you are saying that the source table consists of segCreateDate, segDesc, modifiedDate, and modifiedBy.首先,您是说源表由 segCreateDate、segDesc、 modifiedDate 和 modifiedBy 组成。 But at the end you are also trying to insert into target the columns segID, segCode, createdDate, and createdBy.但最后,您还尝试将 segID、segCode、createdDate 和 createdBy 列插入target

This leads me to the second problem why the above code cannot work: you are trying to join source and target on segID.这使我想到了为什么上面的代码无法工作的第二个问题:您正在尝试在 segID 上加入源和目标。 I cannot tell if this column exists in the target table dbo.seg.我无法判断目标表 dbo.seg 中是否存在此列。 But this column is certainly not contained in the source table as defined above.但是这个列肯定不包含在上面定义的源表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM