Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql update from select

UPDATE YourTable 
SET Col1 = OtherTable.Col1, 
    Col2 = OtherTable.Col2 
FROM (
    SELECT ID, Col1, Col2 
    FROM other_table) AS OtherTable
WHERE 
    OtherTable.ID = YourTable.ID
Comment

How do I UPDATE from a SELECT in SQL Server?


In SQL Server 2008 (or newer), use 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;
Alternatively:

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;
Comment

How do I UPDATE from a SELECT in SQL Server?

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'
Comment

update select sql

--the simplest way of doing this 
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
Comment

PREVIOUS NEXT
Code Example
Sql :: conda install pymysql "windows" 
Sql :: sql last week 
Sql :: alter table change default 
Sql :: start mysql server using docker 
Sql :: postgres add superuser to database 
Sql :: v$session table or view does not exist 
Sql :: sql drop table statement 
Sql :: nvl2 syntax 
Sql :: asp.net core with postgresql deploy on ubuntu 
Sql :: postgres list tables and row counts 
Sql :: view linked servers sql 
Sql :: sql select only time from datetime 
Sql :: difference between where and having clause 
Sql :: strict in postgres SQL 
Sql :: mysql query unique column 
Sql :: select count of distinct values sql 
Sql :: select tables from mysql database 
Sql :: upper and lower in oracle sql 
Sql :: mysql data types 
Sql :: oracle current date minus 1 day 
Sql :: mysql alter decimal precision 
Sql :: oracle exceeded simultaneous sessions_per_user limit 
Sql :: mysql docker 
Sql :: purge undo tablespace usage 
Sql :: oracle search in date columns 
Sql :: linebreak sql 
Sql :: allsource oracle 
Sql :: what is my mysql version 
Sql :: mysql change collation one column 
Sql :: what is integrity constraints 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =