2014-01-23 72 views
1

我試圖運行此SQL來更新表。問題是我需要一個連接才能更新正確的記錄。這是我想出迄今:如何使用加入寫入更新在哪裏條款

Update UserProfile 
    set UserProfile.PropertyValue = 'Test' 
    join ProfilePropertyDefinition 
     on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId 
    where UserProfile.UserId = 11 
    and ProfilePropertyDefinition.PortalID = 0 
    and ProfilePropertyDefinition.PropertyName = 'Address1' 
    and ProfilePropertyDefinition.PropertyCategory = 'Address' 

這是我得到的消息:關鍵字「加入」附近

不正確的語法。

回答

2

你就要成功了,你忘了from條款:

Update UserProfile 
    set UserProfile.PropertyValue = 'Test' 
    from UserProfile 
    join ProfilePropertyDefinition 
     on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId 
    where UserProfile.UserId = 11 
    and ProfilePropertyDefinition.PortalID = 0 
    and ProfilePropertyDefinition.PropertyName = 'Address1' 
    and ProfilePropertyDefinition.PropertyCategory = 'Address' 
+0

謝謝,僅此而已。 –

1
Update UserProfile 
    set UserProfile.PropertyValue = 'Test' 
from UserProfile 
    join ProfilePropertyDefinition 
     on ProfilePropertyDefinition.Id = UserProfile.PropertyDefinitionId 
    where UserProfile.UserId = 11 
    and ProfilePropertyDefinition.PortalID = 0 
    and ProfilePropertyDefinition.PropertyName = 'Address1' 
    and ProfilePropertyDefinition.PropertyCategory = 'Address' 

你不得不重複表from子句中更新 - 即使這IOF語法看起來有點怪。

1

你缺少FROM子句:

Update a 
set PropertyValue = 'Test' 
FROM UserProfile as a 
     inner join ProfilePropertyDefinition as b 
     on b.Id = a.PropertyDefinitionId 
where a.UserId = 11 
     and b.PortalID = 0 
     and b.PropertyName = 'Address1' 
     and b.PropertyCategory = 'Address';