Share via

How to prevent sql update from updating same value twice in same column

Emeka Okoye 126 Reputation points
2024-08-01T23:03:36.43+00:00

Hello

I have an SQL query in stored procedure to update an Amount balance column, my query is working fine, but the issue I am having is, sometimes when user updates I find out that the value doubled.

For example, where a user have 15, and update with another 15, instead of having 30, it will turn out to be 45. This usually happens when user clicked the submit button multiple times.
I really want to prevent that from happening.

Below is my query sample


Update AccTab  	 
set AmtBal += @Amount  	      
where UId = @UId;  	      
if @@rowcount = 0 throw 50000, 'Hey, no rows were updated',1;																										

How do I position my query to make sure it updates with one increment in single update request.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2024-08-02T16:04:41.17+00:00

    This is about the practice of making api calls idempotent. This is a main feature of correctly coded REST apis. This means the api detects the duplicate transaction and ignores it. This allow the client to safely reexecute the api call on network errors. The easiest way to implement is to add request ids for all api calls.

    https://restfulapi.net/idempotent-rest-apis/

    Was this answer helpful?

    0 comments No comments

  2. Lan Huang-MSFT 30,221 Reputation points Microsoft External Staff
    2024-08-02T06:38:40.01+00:00

    Hi @Emeka Okoye,

    You can create a popup message box called "yes/no" to prevent multiple submit buttons.

    Demo

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type = "text/javascript">
            function confirm_closing() {
                var confirm_closingValue = document.createElement("INPUT");
                confirm_closingValue.name = "confirm_closingValue";
                if (confirm("Do you want to accept the closing?")) {
                    confirm_closingValue.value = "CONFIRM";
                } else {
                    confirm_closingValue.value = "CANCEL";
                }
                document.forms[0].appendChild(confirm_closingValue);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
          <asp:Button ID="btnConfirm" runat="server" OnClick="OnConfirm" Text="Confirm" OnClientClick="confirm_closing()"/>
        </form>
    </body>
    </html>
    
    public void OnConfirm(object sender, EventArgs e)
    {
    	string confirmValue = Request.Form["confirm_closingValue"];
    	if (confirmValue == "CONFIRM")
    	{
    		this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked CONFIRM!')", true);
    	}
    	else
    	{
    		this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked CANCEL!')", true);
    	}
    }
    

    Or design an UPDATE with a SELECT (untested):

    Update AccTab  	 
    set AmtBal += @Amount  	      
    where UId = @UId  	      
    and not exists (select * from AccTab where AmtBal = (@Amount * 2));
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    Was this answer helpful?

    0 comments No comments

  3. Olaf Helper 47,621 Reputation points
    2024-08-02T05:31:45.2066667+00:00

    This usually happens when user clicked the submit button multiple times.

    Then it's a bug in your front end/business logic, nothing on SQL Server side.

    Was this answer helpful?

    0 comments No comments

  4. SurferOnWww 6,016 Reputation points
    2024-08-02T00:56:42.7033333+00:00

    Why don't you disable the button right after the user click it. If this does not satisfy your requirement, please let me know why.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.