更新交易 SQL 链接 table 时出现太多行受到影响错误

Too Many Rows Were Affected error on updating a Transact SQL linked table

我正在尝试用这个更新链接 table...

update openquery (LINKSERVERID, 'select tng_email from user_list where tng_id = 62873') 
  set tng_email = 'blah@blah.com';

...但出现以下错误...

OLE DB provider "MSDASQL" for linked server "LINKSERVERID" returned message "Key column information is insufficient or incorrect. Too many rows were affected by update.".

仅供参考:tng_id 是主键。

我该如何解决?

我认为您需要在 select 查询中包含密钥,所以试试这个:

update openquery(
  LINKSERVERID, 'select tng_id, tng_email from user_list where tng_id = 62873'
) set tng_email = 'blah@blah.com';

我尝试了上面来自 jpw 的答案,但它对我不起作用。

我开发了一个触发器,但我使用以下代码也收到了同样的错误:

begin

    if TRIGGER_NESTLEVEL() > 1
    return

    declare @JCid       int =   (select top 1   iJCMasterID from inserted)

    begin
        update L set uiJCTxCMLineNumber = NewLineNum
        from (
        select
            rank() over (order by idJCTxLines) NewLineNum
        ,   iJCMasterID
        ,   uiJCTxCMLineNumber
        ,   idJCTxLines
        from    _btblJCTxLines
        where iJCMasterID =  @JCid
        ) L
        where   iJCMasterID =  @JCid
    end
end
go

但是,我通过将其更改为解决了它:

begin

    if TRIGGER_NESTLEVEL() > 1
    return

    declare @JCid       int =   (select top 1   iJCMasterID from inserted)

    begin
        update L set uiJCTxCMLineNumber = NewLineNum
        from (
        select
            rank() over (order by idJCTxLines) NewLineNum
        ,   iJCMasterID
        ,   uiJCTxCMLineNumber
        ,   idJCTxLines
        from    _btblJCTxLines
        where iJCMasterID =  @JCid
        ) L
    join inserted i on L.idJCTxLines = i.idJCTxLines
    end
end
go

希望对您有所帮助。

我通过向基础 table 添加唯一索引解决了这个错误。