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 9150921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:40:37+00:00 2026-06-17T11:40:37+00:00

I have two tables, Users and #TempTable (which is a subset of Users). I

  • 0

I have two tables, Users and #TempTable (which is a subset of Users). I would like to update a column, IsActive, in the Users table. I would like to set IsActive = 1 if a user that is in #TempTable is also in the Users table, and set IsActive = 0 otherwise.

Getting the users from Users that are NOT in #TempTable (IsActive should be set to 0 for these users):

-- (Users \ #TempTable) U (#TempTable \ Users)
SELECT  u.UserName
FROM    Users u 
WHERE   (u.UserName) NOT IN 
    (SELECT t.[User Name] FROM #TempTable t) 
UNION ALL 
SELECT  t.[User Name] 
FROM    #TempTable t
WHERE   (t.[User Name]) NOT IN 
    (SELECT u.UserName FROM Users u)

Let’s call this the ResultSet. I would appreciate some help with my UPDATE statement. What I’d like to be able to do is:

UPDATE Users
SET IsActive = (CASE WHEN User.UserName IN ResultSet THEN 0 ELSE 1 END) 

without having to write out the CASE WHEN for each User.UserName. Thanks in advance!

  • 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-17T11:40:38+00:00Added an answer on June 17, 2026 at 11:40 am

    You can use a join in the UPDATE statement.

    UPDATE Users
    SET Users.Active = CASE WHEN T.UserName is null THEN 0 ELSE 1 END 
    FROM Users AS U
    LEFT JOIN #TempTable AS T ON U.UserName = T.UserName
    

    Notes :

    • You could also use a sub-query but that would be much slower (order of n squared not order of n). For a small number of users this would not matter.

    • I did not test so I could have a typo / bug in the code above.


    Based on crazy comments about how this would not work I implemented a fiddle.

    Enjoy it working:

    http://sqlfiddle.com/#!6/25235/3

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

Sidebar

Related Questions

I have two tables users registered_members I want to confirm values from user table
I have two tables users(id, username, password) and user_badges(user_id, badge_id, badge_slot) I would like
I have two tables named users and contacts . I would like to delete
I have two tables USERS and CALENDER, in calender table user place his availability
suppose we have two tables users and products table users has an accountBalance column
I have two tables: Users and Roles. A user may have more roles, so
I'm using SQL-Server 2005. I have two tables Users and Payments which has a
I have two tables users and lead_contacts which have similar data. When somebody buys
I have two tables users table: id|name user_relationships id | user_id | friend_id and
I have two tables: Users and Files . Users table has 10 rows, Files

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.