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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:48:27+00:00 2026-05-20T07:48:27+00:00

I have a table that I use to keep track of customer usage. I

  • 0

I have a table that I use to keep track of customer usage. I track the number of hits the user does per day, so I have a table that looks like this:

CustID (uniqueidentifier, not null)

UseDate (smalldatetime, not null)

NumHits (smallint, not null)

I use this SQL in a stored proc to insert a row for today (if needed), or to increment the counter for today:

declare @today datetime
set @today = getdate()

/* Try to bump it by one if an entry for today exists */
if (
    select count(*) from CustomerUsage
    where CustID = @cust_guid and year(UseDate) = year(@today) and month(UseDate) = month(@today) and day(UseDate) = day(@today)
    ) = 0

    insert into CustomerUsage (CustID, UseDate, NumHits) values (@cust_guid, getdate(), 1)

else
    update CustomerUsage set NumHits = NumHits + 1 
    where CustID = @cust_guid and year(UseDate) = year(@today) and month(UseDate) = month(@today) and day(UseDate) = day(@today)

Is there a better way to do this?

Also, I want to move to an environment where multiple web servers can call the stored proc for the same customer. I think this code might be vulnerable to multithreading issues.

Thanks!

  • 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-20T07:48:28+00:00Added an answer on May 20, 2026 at 7:48 am

    You can do like this.

    declare @Today smalldatetime = dateadd(d, datediff(d, 0, getdate()), 0)
    declare @Tomorrow smalldatetime = dateadd(d, 1, @Today)
    
    insert into CustomerUsage(CustId, UseDate, NumHits )
    select Data.CustID, Data.UseDate, Data.NumHits
    from (select
            @cust_guid as CustID,
            getdate() as UseDate,
            0 as NumHits) as Data
    where not exists (select *
                      from CustomerUsage with (updlock, serializable)
                      where
                        CustID = @cust_guid and
                        UseDate >= @Today and
                        UseDate < @Tomorrow)        
    
    
    update CustomerUsage 
    set NumHits = NumHits + 1 
    where
      CustID = @cust_guid and
      UseDate >= @Today and
      UseDate < @Tomorrow
    

    First insert a row with NumHits = 0. The insert checks if there already exists a row for that CustID on that UseDate and only inserts if necessary.

    After the insert always increment NumHits by 1.

    This is basically the same idea as Thomas non merge answer but I do the insert before the update.

    Edit 1 Added Table Hints to the insert statements where not exists part. serializable is needed to prevent primary key constraint violation and updlock is needed to avoid deadlocks.

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

Sidebar

Related Questions

I have a table that I use to keep track of some associations between
I have a remote mySQL table that I use in a few tools, occasionally
I have a groupby and sum table that I use to display current stock
I use SQL Server 2008 R2 and have a table that I want no
I use SQL Server 2005. I have a simple logging table that my web
I have a custom class that I use to build table strings. I would
I have many tables that use Lookup/Enum references for most of their column values.
I have 2 tables that I am working with that use the same column;
Anyone have a PL-SQL statement that i can use to generate database & tables
I use oracle 10. I have update statement like that : update table1 t1

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.