In SQL Server, it is possible to insert rows into a table with an
INSERT.. SELECT statement:
INSERT INTO Table (col1, col2, col3) SELECT col1, col2, col3 FROM other_table WHERE sql = 'cool'
Is it also possible to update a table with
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
How to UPDATE from a SELECT in SQL Server? Answer #1:
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'
Update from a Select in SQL server- Answer #2:
In SQL Server 2008 (or newer), use
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;
Query to Update from a Select in SQL server- Answer #3:
UPDATE YourTable SET Col1 = OtherTable.Col1, Col2 = OtherTable.Col2 FROM ( SELECT ID, Col1, Col2 FROM other_table) AS OtherTable WHERE OtherTable.ID = YourTable.ID
Update from Select in SQL- Answer #4:
I’d modify the first answer to the following:
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 )
Without a WHERE clause, you’ll affect even rows that don’t need to be affected, which could (possibly) cause index recalculation or fire triggers that really shouldn’t have been fired.
Examples to update from select in SQL server- Answer #5:
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'
Another possibility not mentioned yet is to just chuck the
SELECT statement itself into a CTE and then update the 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;
This has the benefit that it is easy to run the
SELECT statement on its own first to sanity check the results, but it does requires you to alias the columns as above if they are named the same in source and target tables.
This also has the same limitation as the proprietary
UPDATE ... FROM syntax shown in four of the other answers. If the source table is on the many side of a one-to-many join then it is undeterministic which of the possible matching joined records will be used in the
Update (an issue that
MERGE avoids by raising an error if there is an attempt to update the same row more than once).
For the record (and others searching like I was), you can do it in MySQL like this:
UPDATE first_table, second_table SET first_table.color = second_table.color WHERE first_table.id = second_table.foreign_id
UPDATE t SET t.col1 = o.col1 FROM table1 AS t INNER JOIN table2 AS o ON t.id = o.id
The simple way to do it is:
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
This may be a nice reason to perform an update (for example, mainly used in a procedure), or may be obvious to others, but it should also be stated that you can perform an update-select statement without using join (in case the tables you’re updating between have no common field).
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
Here is another useful syntax:
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);
It checks if it is null or not by using “WHERE EXIST”.
UPDATE from SELECT with INNER JOIN in SQL Database
Since there are too many replies of this post, which are most heavily up-voted, I thought I would provide my suggestion here too. Although the question is very interesting, I have seen in many forum sites and made a solution using INNER JOIN with screenshots.
At first, I have created a table named with schoolold and inserted few records with respect to their column names and execute it.
Then I executed SELECT command to view inserted records.
Then I created a new table named with schoolnew and similarly executed above actions on it.
Then, to view inserted records in it, I execute SELECT command.
Now, Here I want to make some changes in third and fourth row, to complete this action, I execute UPDATE command with INNER JOIN.
To view the changes I execute the SELECT command.
You can see how Third and Fourth records of table schoolold easily replaced with table schoolnew by using INNER JOIN with UPDATE statement.
Hope you learned something from this post. The primary source of this article is StackOverflow.
Follow Programming Articles for more!