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

  • Home
  • SEARCH
  • 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 6703389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:09:55+00:00 2026-05-26T07:09:55+00:00

I have the following query: UPDATE #Temp_SessionItem SET [Status] = CASE WHEN ([AddressFK] IS

  • 0

I have the following query:

UPDATE #Temp_SessionItem SET [Status] = 
    CASE
        WHEN ([AddressFK] IS NULL OR [StatusFK] IS NULL) AND [Status] = 1
            THEN 3
        WHEN [AddressFK] IS NOT NULL AND [StatusFK] IS NOT NULL AND [Status] = 1
            THEN 2
    END

The problem is that when it’s executed it doesn’t actually give me the result I am expecting. i.e. it doesn’t set the value of [Status] field to 3 in case [AddressFK] or [StatusFK] is NULL. Instead, it just tries to add NULL to Status when it should add 3.

If I remove the AND [Status] = 1 part of the expression it does return 3 but then it updates also the values that are not [Status] = 1.

Is there anything obvious that I’m missing here?

I’m using SQL Server 2008.

EDIT:

After Royi Namir pointed out to me I realized that the problem must be further down the stored procedure that this query is being executed in. Specifically, #Temp_SessionItem is just a temporary table. After the upper mentioned query I’m trying to update the original table with the following query:

--  update the status of the original session item table
    UPDATE UploadSessionItem SET [Status] = T.[Status]
    FROM #Temp_SessionItem AS T
    WHERE UploadSessionItem.UploadSessionItemId  = T.ID

..which is obviously not working since all the [Status] items there are still 1

  • 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-05-26T07:09:56+00:00Added an answer on May 26, 2026 at 7:09 am

    If you only want to affect rows with a current STATUS of 1, you need a WHERE clause:

    UPDATE #Temp_SessionItem SET [Status] = 
        CASE
            WHEN ([AddressFK] IS NULL OR [StatusFK] IS NULL)
                THEN 3
            WHEN [AddressFK] IS NOT NULL AND [StatusFK] IS NOT NULL
                THEN 2
        END
    WHERE [Status] = 1
    

    The update you’ve shown works fine:

    create table UploadSessionItem(UploadSessionItemID int,Status int)
    insert into UploadSessionItem
    select 1,1 union all select 2,2
    create table #Temp_SessionItem (ID int,Status int)
    insert into #Temp_SessionItem select 1,4
    --Your UPDATE:
    UPDATE UploadSessionItem SET [Status] = T.[Status]
        FROM #Temp_SessionItem AS T
        WHERE UploadSessionItem.UploadSessionItemId  = T.ID
    --
    select * from UploadSessionItem
    

    Result:

    UploadSessionItemID Status
    1                   4
    2                   2
    

    So if the problem is elsewhere, it’s not in a code sample you’ve shown us yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL query that takes the following form: UPDATE foo SET flag=true
I've run the following query: UPDATE main_table, reference_table SET main_table.calc_column = (CASE WHEN main_table.incr
I have the following query.... UPDATE vehicle_catalog SET parent_id = 0 WHERE parent_id =
I have the following query: UPDATE PRODUCT SET FIXEDCOST = (Select PRICE from PRODUCTPROGRAM
I have the following query: $sql = UPDATE db.users SET $str WHERE users.{$this->row} =
I have following SQL UPDATE query: UPDATE Invoices SET Price = (SELECT SUM(Price*ProductsAndServices.Amount) FROM
Assume I have the following query: Update LPMBonusReport Set BoxID = (Select ContainerSerialNumber From
i have the following code: $update = mysql_query(UPDATE member_regok SET newmemberid = '.$last_id.', status
I have the following query: UPDATE lessonstatus INNER JOIN user ON lessonstatus.user_id = user.user_id
I have the following query of linq to entities. The problem is that it

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.