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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:01:43+00:00 2026-05-25T12:01:43+00:00

The problem is whenever I execute the stored procedured usp_Execute100K in the Query Windows

  • 0

The problem is whenever I execute the stored procedured “usp_Execute100K” in the Query Windows simultaneously the server could not locate the record and it creating a new record even though the record is already there. You have to run the “usp_Execute100K” multiple windows simultaneously to see this problem.

What I want is to make sure the server only create 1 record per day, if the record does exist then only update the “StatisticTotal” column to the next number.

This problem is taught.

The below are 1 table and two stored procedures

Statistic /Table name/

usp_Statistic_InsertOrUpdate /this will update the “StatisticTotal” column or insert if the record does not exist/

usp_Execute100K /* this will execute the usp_Statistic_InsertOrUpdate 100 thousand times*/

PLEASE COPY THE SCRIPT BELOW TO RECREATE THE TABLE AND STORED PROCEDURES.

GO
/****** Object:  StoredProcedure [dbo].[usp_Execute100K]    Script Date: 09/07/2011 15:37:29 ******/
DROP PROCEDURE [dbo].[usp_Execute100K]
GO
/****** Object:  StoredProcedure [dbo].[usp_Statistic_InsertOrUpdate]    Script Date: 09/07/2011 15:37:29 ******/

DROP PROCEDURE [dbo].[usp_Statistic_InsertOrUpdate]
GO
/****** Object:  Table [dbo].[Statistic]    Script Date: 09/07/2011 15:37:28 ******/
DROP TABLE [dbo].[Statistic]
GO
/****** Object:  Table [dbo].[Statistic]    Script Date: 09/07/2011 15:37:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Statistic](
    [StatisticID] [uniqueidentifier] NOT NULL,
    [StatisticAccount] [uniqueidentifier] NOT NULL,
    [StatisticTotal] [float] NOT NULL,
    [StatisticCreatedDate] [datetime] NOT NULL,
    [DebugDate] [datetime] NULL,
 CONSTRAINT [PK_Statistics] PRIMARY KEY CLUSTERED 
(
    [StatisticID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  StoredProcedure [dbo].[usp_Statistic_InsertOrUpdate]    Script Date: 09/07/2011 15:37:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Statistic_InsertOrUpdate](
@StatisticAccount  uniqueidentifier
)
AS
SET NOCOUNT OFF; --this can be turn on or off, it has no effects
DECLARE @NowDate date
SET     @NowDate=CONVERT(datetime, CONVERT(char, GETDATE(), 106))--remove all time.
--UPDATE ONLY IF IT HAS THE SAME DATE
UPDATE TOP (1) Statistic SET   StatisticTotal =StatisticTotal+1 WHERE (StatisticAccount=@StatisticAccount AND StatisticCreatedDate = @NowDate)
if @@ROWCOUNT=0--If the above statement return no effects then create a new record
BEGIN
    INSERT TOP (1)INTO Statistic(StatisticID, StatisticAccount, StatisticTotal, StatisticCreatedDate,DebugDate)     VALUES (NEWID(),@StatisticAccount,1,@NowDate,@NowDate)
END
GO
/****** Object:  StoredProcedure [dbo].[usp_Execute100K]    Script Date: 09/07/2011 15:37:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Execute100K]
AS
SET NOCOUNT OFF; --this can be turn on or off, it has no effects
declare @current float
set @current=0
while (@current <100000)    
begin
--INSERT THIS STATEMENT for 100 thousand times
    exec usp_Statistic_InsertOrUpdate'4c34eea5-fe17-4b11-8e06-0039577e7421'
    set @current = @current + 1
 end
GO
  • 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-25T12:01:44+00:00Added an answer on May 25, 2026 at 12:01 pm

    Have you considered wrapping your UPDATE/INSERT combo in a transaction? While the constraint and TRY/CATCH will certainly prevent a second row, it will discard the attempt as well. If you wrap it in a transaction, and use TABLOCK (I assume you are potentially doing a lot of writes to this table, but not a ton of reads), you hamper concurrency slightly but you can better ensure that one user will only see the state of the table after the other user is done with it. I could get two rows reliably as is, but I couldn’t get two rows when I used this:

    ALTER PROCEDURE [dbo].[usp_Statistic_InsertOrUpdate]
        @StatisticAccount UNIQUEIDENTIFIER
    AS
    BEGIN
        SET NOCOUNT ON; -- this should always be on
    
        DECLARE @NowDate DATE = CURRENT_TIMESTAMP; 
        -- by definition, you don't need to convert to remove time from date
    
        BEGIN TRANSACTION;
    
        --UPDATE ONLY IF IT HAS THE SAME DATE
        UPDATE dbo.Statistic WITH (TABLOCK)
          SET StatisticTotal += 1 
          WHERE StatisticAccount = @StatisticAccount 
          AND StatisticCreatedDate = @NowDate;
    
        IF @@ROWCOUNT = 0 
        -- If the above statement return no effects then create a new record
        BEGIN
            INSERT dbo.Statistic
            (
              StatisticID,
              StatisticAccount,
              StatisticTotal,
              StatisticCreatedDate,
              DebugDate
            )
            SELECT
              NEWID(),
              @StatisticAccount,
              1,
              @NowDate,
              @NowDate;
        END
    
        COMMIT TRANSACTION;
    END
    GO
    

    Error trapping, rollback, constraints etc. I’ll leave as an exercise.

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

Sidebar

Related Questions

Whenever I execute the gmaven plugin, I get the following problem org.apache.maven.lifecycle.LifecycleExecutionException: Internal error
I keep getting the error Stream was not writable whenever I try to execute
Whenever I see a problem that would be shared by others, with a solution
problem is , that whenever the grid's row is right clicked the selected item
I encountered a strange problem today. Whenever i put a breakpoint in one of
I have a problem with using the prefix, seems like whenever I use the
I am getting this annoying problem, whenever i do some changes to any file
I'm having an annoying problem with my iPhone app. Whenever I set the optimization
The problem I am facing with below code is that whenever I try to
Whenever a record is deleted or updated on a form, I want to save

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.