2014-01-23 3 views
1

Я пытаюсь запустить этот SQL для обновления таблицы. Проблема в том, что мне нужно присоединиться, чтобы обновить правильную запись. Это то, что я придумал до сих пор:Как написать Update with join в where where

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 - даже ИОФ этот синтаксис выглядит немного странно.

1

Вы пропускаете ЕК:

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';