使用引用表更新表中的多条记录 SQL Server [英] Update multiple records in table by using a reference table SQL Server
本文介绍了使用引用表更新表中的多条记录 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 | 参考编号 |
---|---|
1 | 123 |
2 | 321 |
3 | 213 |
4 | 413 |
临时表 B
ID | ExtractedNum |
---|---|
1 | 213 |
2 | 413 |
3 | 321 |
4 | 123 |
预期结果
表 C
表A_ID | TableB_ID |
---|---|
3 | 1 |
4 | 2 |
2 | 3 |
1 | 4 |
我尝试了一些不同的查询,但没有一个能像我需要的那样工作:
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屋!
查看全文