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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:16:55+00:00 2026-05-23T16:16:55+00:00

I need to update a table and set a column (enabled) to 1 with

  • 0

I need to update a table and set a column (enabled) to 1 with the following rules

  1. There must be 1 (and never 0 or > 1) record with enabled set to 1 per Employee_Guid.
  2. It should choose the record that has deleted set to 0 (there should only be one or no records with deleted set to 0 per Employee_Guid) to set enabled to 1.
  3. If all records have deleted set to 1 it should use the record with the most recent Create_Date_Time
  4. If there is a tie with the create date time any record of the tieing group can be set to 1 but only one of them can be used.

If there are more than 1 record with deleted set to 0 I would like a error thrown as that is a illegal state.

Here is what I have so far

if(exists(select count(employee_guid) from Employees_M where deleted = 0 group by employee_guid having count(employee_guid) > 1))
    RAISERROR('More than one record has deleted set to 0')
Update Employees_M set [ENABLE] = 0

select * into #t from employees_m where deleted = 0
insert into #t select * from employees_m where employee_guid not in (select employee_guid from #t) 
                                         and --fulfill rules 3 and 4

Update Employees_M set [ENABLE] = 1
where pk_guid in (select pk_guid from #t)

Here is the table structure

PK_Guid (primary key, uniuque, uniuqueidenitfier, not null)
Employee_Guid (uniuqueidenitfier, not null)    
Deleted (bit, not null)
Enable (bit, not null)
Create_Date_Time (datetime defaults to getdate(), not null)

This is not just a “Show me teh codez” question. I want to learn how to do it correctly, so links or examples that are similar but not solving the problem would be appreciated too.

  • 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-23T16:16:56+00:00Added an answer on May 23, 2026 at 4:16 pm

    It’s often possible to use row_number in this scenario. Its order by clause allows you to assign priorities. In your case, deleted asc and Create_Date desc would seem to capture the requirements.

    Here’s an example of how to use row_number in an update query:

    update  emp
    set     enabled = case when rn = 1 then 1 else 0 end
    from    (
            select  row_number() over (partition by employee_guid 
                                       order by deleted, Create_Date desc) as rn
            ,       *
            from    @employees
            ) emp
    

    Full example at SE Data.

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

Sidebar

Related Questions

I need to update table and set table . related_id (column that binds the
I need to achieve this update [table] set [column c] = ( select [column
I need to update a row in a table, and get a column value
I need to update/replace the data in datatable.column. The table has a field named
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I need to update the comments field in a table for a large list
I have table 'products' and I need to update 'price' field by 20% if
I have a table of about a million rows and I need to update
table 1(ob): name,address table 2(address): dname,addr I need to update ob.address with address.addr when
[Previous essay-title for question] Oracle SQL: update parent table column if all child table

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.