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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:48:24+00:00 2026-05-17T21:48:24+00:00

I use a cursor to iterate through quite a big table. For each row

  • 0

I use a cursor to iterate through quite a big table. For each row I check if value from one column exists in other.

If the value exists, I would like to increase value column in that other table.
If not, I would like to insert there new row with value set to 1.

I check “if exists” by:

IF (SELECT COUNT(*) FROM otherTabe WHERE... > 1)
   BEGIN
      ...
   END
ELSE
   BEGIN
      ...
   END

I don’t know how to get that row which was found and update value. I don’t want to make another select.

How can I do this efficiently?

I assume that the method of checking described above isn’t good for this case.

  • 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-17T21:48:25+00:00Added an answer on May 17, 2026 at 9:48 pm

    Depending on the size of your data and the actual condition, you have two basic approaches:

    1) use MERGE

    MERGE TOP (...) INTO table1
    USING table2 ON table1.column = table2.column
    WHEN MATCHED
     THEN UPDATE SET table1.counter += 1
    WHEN NOT MATCHED SOURCE
     THEN INSERT (...) VALUES (...);
    

    the TOP is needed because when you’re doing a huge update like this (you mention the table is ‘big’, big is relative, but lets assume truly big, +100MM rows) you have to batch the updates, otherwise you’ll overwhelm the transaction log with one single gigantic transaction.

    2) use a cursor, as you are trying. Your original question can be easily solved, simply always update and then check the count of rows updated:

    UPDATE table 
      SET column += 1
    WHERE ...;
    IF @@ROW_COUNT = 0
    BEGIN
       -- no match, insert new value 
       INSERT INTO (...) VALUES (...);
    END
    

    Note that this approach is dangerous though because of race conditions: there is nothing to prevent another thread from inserting the value concurrently, so you may end up with either duplicates or a constraint violation error (preferably the latter…).

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

Sidebar

Related Questions

No related questions found

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.