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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:43:29+00:00 2026-05-31T21:43:29+00:00

I have a table which contains unique indexes;lets say A B 1 1 1

  • 0

I have a table which contains unique indexes;lets say

A B
1 1
1 2
1 3
1 4

And i want to update the B column,since sql updates them one by one if it tries to update the first column to 2 for ex i get::

A B
1 2
1 2
1 3
1 4

And as a result i would get two duplicate values at the 1st and 2nd row and of course an error message.

And i should update the table like that:

A B
1 2
1 1
1 3
1 4

So whats the course of action i should follow in case of this scenario?

Regards.

Maybe i should update the question abit:
What if i wanted to change the b column completely; such as:

A B
1 4
1 2
1 3
1 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-31T21:43:30+00:00Added an answer on May 31, 2026 at 9:43 pm

    The solution is to do the swap in a single statement:

    UPDATE YOUR_TABLE
    SET B = (CASE B WHEN 1 THEN 2 ELSE 1 END)
    WHERE A = 1 AND B IN (1, 2)
    

    — EDIT —

    To update several rows at once, you can do a JOIN-update from the temporary table:

    CREATE TABLE #ROWS_TO_UPDATE (
        A int,
        B int,
        NEW_B int,
        PRIMARY KEY (A, B)
    );
    
    INSERT INTO #ROWS_TO_UPDATE (A, B, NEW_B) VALUES (1, 1, 4);
    INSERT INTO #ROWS_TO_UPDATE (A, B, NEW_B) VALUES (1, 2, 3);
    INSERT INTO #ROWS_TO_UPDATE (A, B, NEW_B) VALUES (1, 3, 2);
    INSERT INTO #ROWS_TO_UPDATE (A, B, NEW_B) VALUES (1, 4, 1);
    
    UPDATE YOUR_TABLE
    SET B = NEW_B
    FROM
        YOUR_TABLE JOIN #ROWS_TO_UPDATE
            ON YOUR_TABLE.A = #ROWS_TO_UPDATE.A AND YOUR_TABLE.B = #ROWS_TO_UPDATE.B;
    
    DROP TABLE #ROWS_TO_UPDATE;
    

    The above code transforms the following data…

    A B
    1 1
    1 2
    1 3
    1 4
    

    …to this:

    A B
    1 4
    1 2
    1 3
    1 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which contains my ads that can be searched in sql-server-2008.
In a SQL server database, I have a table which contains a TEXT field
I use SQL Server 2008, C#, I have a table which contains about 20000
I want to have a unique constraint on a column which I am going
I have a MySQL table with a datetime column named 'created' which contains the
I have a table which contains a unique integer field containing consecutive values. When
I have a table which contains my server status create table ServerStatus ( ServerId
i have a table which contains a bunch of dynamically created radio button lists,
I have a table which contains house details called property. I am creating a
Ok, I have a table which contains multiple rows. Each row contains some data

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.