SQL - Update multiple cells in one columns with the same ID

Total Post:101

 1285  View(s)
Rate this:

I have a table Answer in SQL Server like this:

QuestionID         AnswerID            AnswerContent                                IsTrue

1                              1                              1990                                       True

1                              2                              300$                                       False

1                              3                              1992                                       True

2                              4                              1993                                       False

2                              5                              400$                                       True

2                              6                              600$                                       False

5                              7                              Lion                                        False

5                              8                              Tiger                                      True

5                              9                              Elephant                              False


Because the AnswerID is an auto-increment and I don't know exactly its value when running the query.

So how can I update the AnswerContent column?

Example: I need to update

"Tiger - False" to "Elephant - True"

"Lion - True" to "1992 - False"


--- Update ---


I cannot change the table structure because it's my teacher's order.

  1. Post:10

    Re: SQL - Update multiple cells in one columns with the same ID

    hi pravesh,

    you need to try this:

    You can do it in one shot using the CASE expression. Here is an example:

    UPDATE MyTable
        AnswerContent = CASE
            WHEN AnswerContent='Tiger' AND IsTrue='False` THEN `Elephant`
            ELSE AnswerContent ---- Keep the value unchanged
    ,   IsTrue = ... -- Do other columns using the same expression structure

      Modified On Apr-05-2018 05:59:07 AM



Enter your email address here always to be updated. We promise not to spam!