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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:38:44+00:00 2026-06-15T09:38:44+00:00

I have a table that keeps a count of user actions. Each time an

  • 0

I have a table that keeps a count of user actions. Each time an action is done, the value needs to increase. Since the user can have multiple sessions at the same time, the process needs to be atomic to avoid multi-user issues.

The table has 3 columns:

  • ActionCode as varchar
  • UserID as int
  • Count as int

I want to pass ActionCode and UserID to a function that will add a new row if one doesn’t already exist, and set count to 1. If the row does exist, it will just increase the count by one. ActionCode and UserID make up the primary unique index for this table.

If all I needed to do was update, I could do something simple like this (because an UPDATE query is atomic already):

UPDATE (Table)
SET Count = Count + 1 
WHERE ActionCode = @ActionCode AND UserID = @UserID

I’m new to atomic transactions in SQL. This question has probably been answered in multiple parts here, but I’m having trouble finding those and also placing those parts in one solution. This needs to be pretty fast as well, without getting to complex, because these actions may occur frequently.

Edit: Sorry, this might be a dupe of MySQL how to do an if exist increment in a single query. I searched a lot but had tsql in my search, once I changed to sql instead, that was the top result. It isn’t obvious if that is atomic, but pretty sure it would be. I’ll probably vote to delete this as dupe, unless someone thinks there can be some new value added by this question and answer.

  • 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-15T09:38:46+00:00Added an answer on June 15, 2026 at 9:38 am

    Assuming you are on SQL Server, to make a single atomic statement you could use MERGE

    MERGE YourTable AS target
    USING (SELECT @ActionCode, @UserID) AS source (ActionCode, UserID)
    ON (target.ActionCode = source.ActionCode AND target.UserID = source.UserID)
    WHEN MATCHED THEN 
        UPDATE SET [Count] = target.[Count] + 1
    WHEN NOT MATCHED THEN   
        INSERT (ActionCode, UserID, [Count])
        VALUES (source.ActionCode, source.UserID, 1)
    OUTPUT INSERTED.* INTO #MyTempTable;
    

    UPDATE Use output to select the values if necessary. The code updated.

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

Sidebar

Related Questions

Suppose I have a MySQL table that keeps the page visit count and I
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that has account numbers in (account_num) and user profiles (profile_id).
I have a table of user-generated content that includes an ENUM format column for
I have a table that keeps track of the letters we send to our
I have a PriceComparison table with (StoreNumber, ItemNumber, Price) that keeps pricing data for
I have a views table that keeps track of daily views. We use this
I have a table that I keep track of fees for a specific item.
I have table that I insert data with following query (from c# code): INSERT

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.