使用引用表更新表中的多条记录 SQL Server [英] Update multiple records in table by using a reference table SQL Server

查看:25
本文介绍了使用引用表更新表中的多条记录 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用来自其他两个表的引用来更新 SQL 中的表.我需要用 Table A 中记录的 ID 更新 Table C,但只需要 Temp Table B 中的列code>存在于表A

I am trying to update a table in SQL by using references from two other tables. I need Table C to be updated with the ID of records from Table A but only where a column from Temp Table B exists in Table A

表 A

<头>
ID参考编号
1123
2321
3213
4413

临时表 B

<头>
IDExtractedNum
1213
2413
3321
4123

预期结果

表 C

<头>
表A_IDTableB_ID
31
42
23
14

我尝试了一些不同的查询,但没有一个能像我需要的那样工作:

I've tried a few different queries but none of them work the way I need it to:

UPDATE table_c 
  SET
      table_c.tablea_id = a.id -- int    
FROM table_a a
WHERE table_c.tableb_id =
(
    SELECT t.Id, 
           t.ExtractedNum
    FROM #tempTableB t, 
         table_a a2  
    WHERE t.ExtractedNum = a2.ReferenceNumber
);

推荐答案

UPDATE c
SET c.tablea_id=a.id
FROM tableC c INNER JOIN
tableA a ON a.id=c.tablea_id INNER JOIN
#tempTableB b ON b.ExtractedNum=a.ReferenceNumber

这篇关于使用引用表更新表中的多条记录 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆