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

Total Post:101

Points:709
 1285  View(s)
Ratings:
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

    Points:70
    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
    SET
        AnswerContent = CASE
            WHEN AnswerContent='Tiger' AND IsTrue='False` THEN `Elephant`
            ELSE AnswerContent ---- Keep the value unchanged
        END
    ,   IsTrue = ... -- Do other columns using the same expression structure

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

Answer

NEWSLETTER

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