问题 如何从SQL Server中的SELECT更新?
在 SQL Server,有可能 insert
使用a进入表 SELECT
声明:
INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE sql = 'cool'
是否也有可能 更新 通过一个 SELECT
?我有一个包含值的临时表,并希望使用这些值更新另一个表。也许是这样的:
UPDATE Table SET col1, col2
SELECT col1, col2
FROM other_table
WHERE sql = 'cool'
WHERE Table.id = other_table.id
1985
2018-02-25 14:36
起源
答案:
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'
4463
2018-02-25 14:39
在SQL Server 2008(或更好)中,使用 MERGE
MERGE INTO YourTable T
USING other_table S
ON T.id = S.id
AND S.tsql = 'cool'
WHEN MATCHED THEN
UPDATE
SET col1 = S.col1,
col2 = S.col2;
或者:
MERGE INTO YourTable T
USING (
SELECT id, col1, col2
FROM other_table
WHERE tsql = 'cool'
) S
ON T.id = S.id
WHEN MATCHED THEN
UPDATE
SET col1 = S.col1,
col2 = S.col2;
672
2017-09-09 09:40
UPDATE table
SET Col1 = i.Col1,
Col2 = i.Col2
FROM (
SELECT ID, Col1, Col2
FROM other_table) i
WHERE
i.ID = table.ID
524
2018-01-22 17:47
我会修改 罗宾的出色答案 以下内容:
UPDATE Table
SET Table.col1 = other_table.col1,
Table.col2 = other_table.col2
FROM
Table
INNER JOIN other_table ON Table.id = other_table.id
WHERE
Table.col1 != other_table.col1
OR Table.col2 != other_table.col2
OR (
other_table.col1 IS NOT NULL
AND Table.col1 IS NULL
)
OR (
other_table.col2 IS NOT NULL
AND Table.col2 IS NULL
)
如果没有WHERE子句,您甚至会影响不需要受影响的行,这可能(可能)导致索引重新计算或实际上不应该触发的触发器。
253
2017-09-08 21:20
单程
UPDATE t
SET t.col1 = o.col1,
t.col2 = o.col2
FROM
other_table o
JOIN
t ON t.id = o.id
WHERE
o.sql = 'cool'
180
2018-02-25 14:41
还没有提到的另一种可能性就是扔掉它 SELECT
声明自己进入CTE然后更新CTE。
;WITH CTE
AS (SELECT T1.Col1,
T2.Col1 AS _Col1,
T1.Col2,
T2.Col2 AS _Col2
FROM T1
JOIN T2
ON T1.id = T2.id
/*Where clause added to exclude rows that are the same in both tables
Handles NULL values correctly*/
WHERE EXISTS(SELECT T1.Col1,
T1.Col2
EXCEPT
SELECT T2.Col1,
T2.Col2))
UPDATE CTE
SET Col1 = _Col1,
Col2 = _Col2
这样做的好处是很容易运行 SELECT
语句本身首先对sanity检查结果,但是如果在源表和目标表中它们的名称相同,它确实要求您对列进行别名。
这也与专有限制相同 UPDATE ... FROM
语法显示在其他四个答案中。如果源表位于一对多连接的许多一侧,则不确定哪些可能的匹配连接记录将用于 Update
(一个问题 MERGE
如果尝试多次更新同一行,则通过引发错误来避免)。
140
2017-11-06 00:18
对于记录(以及其他像我一样的搜索),你可以在MySQL中这样做:
UPDATE first_table, second_table
SET first_table.color = second_table.color
WHERE first_table.id = second_table.foreign_id
95
2017-10-05 14:20
使用别名:
UPDATE t
SET t.col1 = o.col1
FROM table1 AS t
INNER JOIN
table2 AS o
ON t.id = o.id
78
2018-05-23 13:06
简单的方法是:
UPDATE
table_to_update,
table_info
SET
table_to_update.col1 = table_info.col1,
table_to_update.col2 = table_info.col2
WHERE
table_to_update.ID = table_info.ID
61
2017-11-14 13:17
这可能是执行更新的一个利基理由(例如,主要用于过程),或者对其他人来说可能是显而易见的,但是还应该声明您可以在不使用连接的情况下执行update-select语句(如果是您正在更新的表没有公共字段)。
update
Table
set
Table.example = a.value
from
TableExample a
where
Table.field = *key value* -- finds the row in Table
AND a.field = *key value* -- finds the row in TableExample a
48
2018-06-11 16:58
这是另一个有用的语法:
UPDATE suppliers
SET supplier_name = (SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS (SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id);
它使用“WHERE EXIST”检查它是否为null。
46
2018-05-02 09:48