2013-04-09 80 views
-2

我想用更新任何記錄,所有記錄我的表是column1 <> column2 我的命令是:我可以使用內部連接更新嗎?

update table1 set name = (select name from table2 
          where code1 <> code2 and table1.id=table2.id) 

這可能嗎?

在update命令中可以使用inner join嗎?

+1

[https://www.google.com/search?hl=en&safe=off&site=&source=hp&q=sql+server+update+with+inner+加入&OQ = SQL +服務器+更新+與+內部+加入](谷歌搜索結果的'內部連接SQL Server更新') – LittleBobbyTables 2013-04-09 15:26:42

+0

檢查此問題http://stackoverflow.com/questions/2044467/how-to-update-two- sql-server-2005 sql-server-2005 – 2013-04-09 15:27:05

+0

可能重複[在SQL Server中使用JOIN更新表](http://stackoverflow.com/questions/1604091/update-a-table-using -join-在-SQL服務器) – LittleBobbyTables 2013-04-09 15:27:49

回答

1

你可以簡單地加入這兩個表,

UPDATE a 
SET  a.Name = b.Name 
FROM Table1 a 
     INNER JOIN Table2 b 
      ON a.ID = b.id 
WHERE a.Code1 <> a.Code2