【问题标题】:Too Many Rows Were Affected error on updating a Transact SQL linked table更新 Transact SQL 链接表时影响了太多行错误
【发布时间】:2015-06-26 14:38:17
【问题描述】:

我正在尝试使用此更新链接表...

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

...但我收到以下错误...

链接服务器“LINKSERVERID”的 OLE DB 提供程序“MSDASQL”返回消息“键列信息不足或不正确。 太多行受更新影响。”。

仅供参考:tng_id 是主键。

我该如何解决?

【问题讨论】:

    标签: sql-server tsql sql-update linked-server


    【解决方案1】:

    我认为您需要在选择查询中包含键,所以试试这个:

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

    【讨论】:

      【解决方案2】:

      我从 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
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-07
          • 2011-07-16
          • 2014-05-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多