giammin

SQL Server UPDATE from SELECT

Nov 20th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.55 KB | None | 0 0
  1. --Sql server 2008 and following
  2. MERGE INTO Table
  3.    USING (
  4.           SELECT id, col1, col2
  5.             FROM other_table
  6.            WHERE sql = 'cool'
  7.          ) AS source
  8.       ON Table.id = source.id
  9. WHEN MATCHED THEN
  10.    UPDATE
  11.       SET col1 = source.col1,
  12.           col2 = source.col2;
  13.  
  14. --general
  15.  
  16. UPDATE
  17.     Table
  18. SET
  19.     Table.col1 = other_table.col1,
  20.     Table.col2 = other_table.col2
  21. FROM
  22.     Table
  23. INNER JOIN
  24.     other_table
  25. ON
  26.     Table.id = other_table.id
  27.  
  28.  
  29. INSERT INTO table1 ( column1 )
  30. SELECT  col1
  31. FROM    table2
Advertisement
Add Comment
Please, Sign In to add comment