使用 select 语句更新多行 [英] Update multiple rows using select statement

查看:27
本文介绍了使用 select 语句更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些表格和值:

Table1
------------------------
ID | Value
------------------------
2 | asdf
4 | fdsa
5 | aaaa


Table2
------------------------
ID | Value
------------------------
2 | bbbb
4 | bbbb
5 | bbbb

我想使用 Table1 中的值及其各自的 ID 来更新 Table2 中的所有值.

I want to update all the values in Table2 using the values in Table1 with their respective ID's.

有没有办法用一个简单的 SQL 查询来做到这一点?

Is there a way to do that with a simple SQL query?

推荐答案

运行选择以确保它是您想要的

Run a select to make sure it is what you want

SELECT t1.value AS NEWVALUEFROMTABLE1,t2.value AS OLDVALUETABLE2,*
FROM Table2 t2
INNER JOIN Table1 t1 on t1.ID = t2.ID

更新

UPDATE Table2
SET Value = t1.Value
FROM Table2 t2
INNER JOIN Table1 t1 on t1.ID = t2.ID

另外,考虑使用 BEGIN TRAN 以便您可以在需要时回滚它,但请确保在您满意时COMMIT.

Also, consider using BEGIN TRAN so you can roll it back if needed, but make sure you COMMIT it when you are satisfied.

这篇关于使用 select 语句更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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