How to update multiple columns with a single statement with multiple conditions

前端 未结 2 970
野性不改
野性不改 2021-01-25 20:25

I want to update a series of columns Country1, Country2... Country 9 based on a comma delimited string of country names in column Country.

2条回答
  •  抹茶落季
    2021-01-25 21:30

    You should run 2 separate update queries. The problem is that Country can be null independently of Genre. So however you combine the conditions Country IS NOT NULL and Genre IS NOT NULL in the where clause of the query, you will miss some columns that could have been updated, or you will update some columns with null values.

    Now, I haven't seen the implementation of returnCommaDelimitedValue. It is possible that it returns null when the string argument is null. In that case, you might consider remove the where clause completely and update the countryN and genreN columns in the same query. In that case, if Country is null, this will also make Country1 null, so this might be something that you want. If almost all rows have a non-null Country and Genre, this approach might be faster.

提交回复
热议问题