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 :: sql select contem uma palavra 
Sql :: mysql order by desc limit 
Sql :: how to count null values in mysql 
Sql :: postgresql create table with boolean column 
Sql :: mysql best varchar length size 
Sql :: oracle cannot access v$session 
Sql :: drop table 
Sql :: woocommerce mysql price table 
Sql :: What is localhost IP and MySql default port no. 
Sql :: sql insert query 
Sql :: view linked servers 
Sql :: get first n letter of department name in sql 
Sql :: having vs where sql 
Sql :: how to fetch alternate records from two tables 
Sql :: return insert results in POSTGRESQL 
Sql :: show tables postgresql 
Sql :: update row in mysql 
Sql :: reset identity column values in sql server 
Sql :: insert multiple rows in sql workbench 
Sql :: org.h2.jdbc.JdbcSQLSyntaxErrorException 
Sql :: sql select all from table 
Sql :: ora-02391 
Sql :: docker mysql random root password 
Sql :: mysql does sentence contain word 
Sql :: How to convert Varchar to Double in sql? 
Sql :: sql query to search for a string in all columns 
Sql :: oracle pl sql source 
Sql :: postgres change column type to uuid 
Sql :: get date ISO in psql 
Sql :: To count number of rows in SQL table 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =