如何从SQL Server中的SELECT更新? [英] How do I UPDATE from a SELECT in SQL Server?

查看:109
本文介绍了如何从SQL Server中的SELECT更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server 中,可以使用SELECT语句将INSERT插入表中:

In SQL Server, it is possible to INSERT into a table using a SELECT statement:

INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3 
FROM other_table 
WHERE sql = 'cool'

是否还可以通过SELECT进行更新?我有一个包含这些值的临时表,并想使用这些值更新另一个表.也许是这样的:

Is it also possible to update via a SELECT? I have a temporary table containing the values and would like to update another table using those values. Perhaps something like this:

UPDATE Table SET col1, col2
SELECT col1, col2 
FROM other_table 
WHERE sql = 'cool'
WHERE Table.id = other_table.id

推荐答案

UPDATE
    Table_A
SET
    Table_A.col1 = Table_B.col1,
    Table_A.col2 = Table_B.col2
FROM
    Some_Table AS Table_A
    INNER JOIN Other_Table AS Table_B
        ON Table_A.id = Table_B.id
WHERE
    Table_A.col3 = 'cool'

这篇关于如何从SQL Server中的SELECT更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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