Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to create SQL Server Stored Proc to increment or decrement a column value

Posted on 2008-10-07
6
Medium Priority
?
707 Views
Last Modified: 2013-12-17
hi - I want to call a stored procedure from C# and pass it 2 values. ID and Action.

Action will be decrement or increment.

I want to find row in table and decrement a column value or increment the value depending on the action.

I also need to lock the current value so that other users can't change it in between my read and update.

I'd like as much as possible in the stored procedure.

Many thanks

Chris
0
Comment
Question by:themagicmagician
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 22660768
create procedure myProc (@id int, @action varchar(3))
as
update mytable
set  my_column = case when @action = 'INC' then my_column+1
                                     when @action = 'DEC' then my_column-1
       else my_column end
where my_column = @id
0
 

Author Comment

by:themagicmagician
ID: 22660834
Thanks.

 Will this prevent other users from updating the value between read and write?

Will I get access denied errors if another user calls the stored procedure at the same time?

Regards
Chris
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22661692
just calling the stored procedure will not prevent others from doing a dirty read
to prevent that you need to do a locking mechanism i.e. transactions - when you are updating the value only you are updating and no one else

transactions dont prevent other users from modifying what one user has already modified - so in such a case a modified time stamp comes in picture
when a user fetches data from the database the modified time stamp is also returned and when the user comes in to update the database the time stamp is returned back and compared in the stored procedure so that any other user has not modified the data - if its not modified then you update else you return the error back to the user saying that please update as the data has been modified
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22661736
the code i posted will prevent concurrent updates by users because sql server perform data locking when you update a record
the entire update operation is an atomic process
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question