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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:02:43+00:00 2026-06-15T04:02:43+00:00

I want to update many rows in a single command with different IDs. These

  • 0

I want to update many rows in a single command with different IDs. These IDs should be passed in the query like

Update table
Set Updated = 1
where ID is in 
(
     1
     2
     3
)

The further difficulty is that there are two primary key columns to check for. I don’t want to update each row in its own command because its very slow because of the server roundtrips.

What is the best way to update a table like this?

  • 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-15T04:02:44+00:00Added an answer on June 15, 2026 at 4:02 am

    Join the table to a derived table that contains a VALUES clause (SQL 2008) or a series of UNION ALL SELECTs.

    UPDATE T
    SET Col = 1
    FROM dbo.Table T
    INNER JOIN (
       VALUES
          (1, 3),
          (3, 5),
          (5, 7)
    ) X (ID1, ID2)
       ON T.ID1 = X.ID1
       AND T.ID2 = X.ID2
    

    For SQL 2005 and earlier:

    UPDATE T
    SET Col = 1
    FROM dbo.Table T
    INNER JOIN (
       SELECT 1, 3
       UNION ALL SELECT 3, 5
       UNION ALL SELECT 5, 7
    ) X (ID1, ID2)
       ON T.ID1 = X.ID1
       AND T.ID2 = X.ID2
    

    Alternately, insert to a temp table and join to that. You could also create a stored procedure that accepts a parameter containing all the keys you need to join with. The parameter could be text (which you would split), xml, or a table-valued parameter.

    If the amount of data is simply enormous then you might consider doing a bulk load of the keys from a text file into a table.

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

Sidebar

Related Questions

I want to update 50% of the rows in a table, randomly selected. Is
How do I know how many rows were affected by an update query? mysql>
I have something like 40K rows and i must run update query like this:
I want to update the lastlogin column value .i wrote the code like this
I have a mysql MyISAM table with many rows that contain information about places,
I want to update a table with PHP and mySQL. I need to add
I have a table with around 6 million rows and has many columns with
I want to count many strings (>3G), so I choose SQLite with a table
I would like to combine insert/update with a case statement, meaning that I want
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true

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.