Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:31:02+00:00 2026-06-12T09:31:02+00:00

I have the following UPDATE statement: update MESSAGES set status= (select (CASE WHEN from_id=’111111111′

  • 0

I have the following UPDATE statement:

update MESSAGES set status=
    (select (CASE 
               WHEN from_id='111111111' 
               THEN (CASE 
                      WHEN status='A' 
                      THEN 'S' 
                  WHEN status='R' 
                      THEN 'D' 
                     END)
               WHEN to_id='111111111' 
               THEN (CASE 
                       WHEN status='A' 
                       THEN 'R' 
                   WHEN status='S' 
                       THEN 'D' 
                     END) 
             END)
    as status_value)
where primary_key='236499681204' 
and status_value not null ;

The problem with this query is, at the last line, status_value is not recognized. If I remove the last comparison, and status_value not null, it works. I dint expect the above version to work, but could someone tell me an alternative that would achieve the same?

Is there a way I can use the AS keyword in the UPDATE query to define the variable status_value?

Edit

The actual requirement is not just that i check if status_value is null, but also that I dont perform any update if it is null

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:31:03+00:00Added an answer on June 12, 2026 at 9:31 am

    Try to add ELSE STATUS and remove and status_value not null:

      update MESSAGES set status=
        (select (CASE 
                   WHEN from_id='111111111' 
                   THEN (CASE 
                          WHEN status='A' 
                          THEN 'S' 
                      WHEN status='R' 
                          THEN 'D' 
                         END)
                   WHEN to_id='111111111' 
                   THEN (CASE 
                           WHEN status='A' 
                           THEN 'R' 
                       WHEN status='S' 
                           THEN 'D' 
                         END) 
                 ELSE STATUS 
                 END)
        as status_value)
    where primary_key='236499681204'; 
    

    You can also try to use view

    create view MyView as
    select (CASE 
                   WHEN from_id='111111111' 
                   THEN (CASE 
                          WHEN status='A' 
                          THEN 'S' 
                      WHEN status='R' 
                          THEN 'D' 
                         END)
                   WHEN to_id='111111111' 
                   THEN (CASE 
                           WHEN status='A' 
                           THEN 'R' 
                       WHEN status='S' 
                           THEN 'D' 
                         END) 
                 END)
        as status_value;
    
    update MESSAGES set status= mv.status_value
    from MyView mv
    where primary_key='236499681204'
    and mv.status_value is null
    

    or you can use case in condition

     update MESSAGES set status=
        (select (CASE 
                   WHEN from_id='111111111' 
                   THEN (CASE 
                          WHEN status='A' 
                          THEN 'S' 
                      WHEN status='R' 
                          THEN 'D' 
                         END)
                   WHEN to_id='111111111' 
                   THEN (CASE 
                           WHEN status='A' 
                           THEN 'R' 
                       WHEN status='S' 
                           THEN 'D' 
                         END) 
                 END)
        as status_value)
    where primary_key='236499681204'
    and CASE
         WHEN from_id='111111111' 
          THEN (CASE 
                 WHEN status='A' 
                 THEN 'S' 
                 WHEN status='R' 
                 THEN 'D' 
                END)
         WHEN to_id='111111111' 
          THEN (CASE 
                 WHEN status='A' 
                 THEN 'R' 
                 WHEN status='S' 
                 THEN 'D' 
                END) 
        END is not null; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have the following update statement in SQL Server: UPDATE MY_TABLE SET COLUMN1 = FN_GETFIRSTVALUE(T.VALUE1),
I have the following SQL statement where i'm trying to update multiple rows matching
I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I quite often have rows of code like the following: UPDATE my_table SET name
I have the following code: $SQL = UPDATE jobs SET read = '1' WHERE
I have the following query: SET @q = 12; UPDATE `table` SET qty =
I currently have the following update statement but is there anyway that I can
I'm creating a trigger for a table, and I have the following statement: UPDATE
I am updating my table with the following statement: UPDATE table SET percentage =
I have the following statement that is executed at the end of a really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.