【问题标题】:Updating a table in SQL Server with information from another table使用来自另一个表的信息更新 SQL Server 中的表
【发布时间】:2021-02-19 15:04:01
【问题描述】:

我有一个“索赔”表。每个索赔都有一个附加的客户,由几个字母“ABED”表示。出于某种原因,在声明表中找不到实际的 clientID,所以我想解决这个问题。 “客户”表确实包含代码和客户 ID。如何运行查询,以便根据代码使用 clientID 更新现有声明?

UPDATE claims 
SET clientID = (SELECT clientID FROM clients WHERE claimID = code) 

在声明表中,客户端的代码称为“claimID”。在客户表中,它被称为代码。

为了清楚起见,我确实在 claim 表中添加了 clientID 列,它当前为 NULL,我希望用 clients 表中的 clientID 填充它。

【问题讨论】:

  • 运行此查询时会发生什么?
  • 样本数据和期望的结果可以做到这一点。更清晰。

标签: sql sql-server sql-server-2008 sql-update subquery


【解决方案1】:

这是你想要的吗?

update cla
set clientid = (select t.clientid from clients cli where cli.code = cla.claimid)
from claims cla

这会搜索其code 与claims 的claimid 匹配的clients,并更新对应的clientid。

【讨论】:

    【解决方案2】:
    UPDATE Clm
    SET Clientid = Cnt.Clientid
    FROM Claims Clm
    JOIN Clients Cnt
    ON Clm.Claimid = Cnt.Code    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多