2013-04-04 88 views
0

我有一個產品表。我有一些空行。我想用現有的行填充這些行。例如,如果我有如何使用現有行更新行?

Product 
----------------------------------- 
ID Name Desc --------------- 
1  A  A1 --------------- 
2  B  B1 --------------- 
3  C  C1 --------------- 

我想將ID = 1的所有項複製到ID = 3行。

試過,但收到錯誤,Ambigious

UPDATE [Products] 
SET DP.[Name] = SP.Name 
    ,DP.[Desc] = SP.DESC       
FROM [Products] DP 
     INNER JOIN [Products] SP ON SP.ID = 3 
WHERE DP.ID = 1 
+1

[你有什麼** **你試過??](http://www.whathaveyoutried.com) – 2013-04-04 07:57:53

+1

複製 - http://stackoverflow.com/questions/2334712/update-from-select- using-sql-server – 2013-04-04 07:58:41

+0

@感謝Deepak。我已seearched,但沒有發現任何 – user960567 2013-04-04 08:03:19

回答

1

您需要使用別名 - 無論是DP或SP在UPDATE,而不是產品本身,因爲它是兩者之間的曖昧。

UPDATE DP 
SET DP.[Name] = SP.Name 
    ,DP.[Desc] = SP.[DESC]       
FROM [Products] DP 
INNER JOIN [Products] SP ON SP.ID = 3 
WHERE DP.ID = 1 
+0

非常感謝。 – user960567 2013-04-04 08:17:47