Solved

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

Posted on 2008-10-07
6
628 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
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 125 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now