Using Distinct in SQL Update

后端 未结 4 1264
难免孤独
难免孤独 2021-01-13 04:07

Here is the Select I need to convert to an Update:

SELECT DISTINCT f.SectionID, f.Name, v.Enabled
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID =          


        
相关标签:
4条回答
  • 2021-01-13 04:44

    Based on your comment, all of the matching rows should be updated you don't need distinct. Just execute this:

    UPDATE SETTING_VALUE
    SET Enabled = 0
    FROM SETTING_VALUE v
    INNER JOIN SETTING s ON v.SettingID = s.SettingID
    INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
    WHERE v.DisplayValue LIKE '%Miami%'
    AND f.ControlName LIKE '%City%'
    

    Everything that you saw in your select will be updated.

    0 讨论(0)
  • 2021-01-13 04:45

    Option 1: Use a temporary table to hold your initial query results, then update based on the query results.

    Option 2:

    Insert into SETTING_VALUE
    (SectionID, Name, Enabled)
    SELECT DISTINCT f.SectionID, f.Name, v.Enabled
    FROM SETTING_VALUE v
    INNER JOIN SETTING s ON v.SettingID = s.SettingID
    INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
    WHERE v.DisplayValue LIKE '%Miami%'
    AND f.ControlName LIKE '%City%'
    
    0 讨论(0)
  • 2021-01-13 04:47
    UPDATE SETTING_VALUE
       SET Enabled = 0
     WHERE DisplayValue LIKE '%Miami%'
           AND EXISTS (
                       SELECT *
                         FROM SETTING s 
                        WHERE SETTING_VALUE.SettingID = s.SettingID
                              AND EXISTS (
                                          SELECT *
                                            FROM LU_FIELD f 
                                           WHERE f.FieldID = s.FieldID
                                                 AND f.ControlName LIKE '%City%'
                                         )
                      );
    
    0 讨论(0)
  • 2021-01-13 04:50

    You can try the following

    UPDATE v
    SET Enabled = 0 
    FROM SETTING_VALUE v 
    WHERE EXISTS(
       select * from SETTING s 
       INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID 
       WHERE v.DisplayValue LIKE '%Miami%' 
       AND f.ControlName LIKE '%City%' 
       AND v.SettingID = s.SettingID
    ) 
    
    0 讨论(0)
提交回复
热议问题