Solved

select count plus 1 and update it

Posted on 2007-12-05
8
371 Views
Last Modified: 2012-06-27
I have a record in a table and I want to create a stored procedure that when invoked it will do a select on the record and get the value of counter, increment it by one update the record and return the new value.

is this possible with a stored proc?
0
Comment
Question by:lobos
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 20412687
yes, that is possible:


CREATE PROCEDURE dbo.GetSequence
AS
 SET NOCOUNT ON
 DECLARE @value INT
 BEGIN TRANSACTION
 SELECT @Value = yourfield + 1 FROM yourtable WHERE ...
 UPDATE yourtable SET yourfield = @value WHERE ...
 COMMIT
 SELECT @value as NewValue

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20412693
variant with output parameter:
CREATE PROCEDURE dbo.GetSequence
( @value int OUTPUT ) 
AS
 SET NOCOUNT ON
 BEGIN TRANSACTION
 SELECT @Value = yourfield + 1 FROM yourtable WHERE ...
 UPDATE yourtable SET yourfield = @value WHERE ...
 COMMIT

Open in new window

0
 
LVL 10

Expert Comment

by:lahousden
ID: 20412722
Create PROC advance_counter
@pk_col int
@new_value int = null output
AS
begin transaction
update your_table
set counter = counter + 1
where pk_col = @pk_col
select @new_value = counter
from your_table
where pk_col = @pk_col
commit transaction
0
 

Author Comment

by:lobos
ID: 20412866
how do I test this with query anylyser?
exec  GetSequence
Procedure 'GetSequence' expects parameter '@value', which was not supplied.

also why do I have to use an output parameter...can't I just use a select statement and then with that record I can get the value of the counter?
ie.
CREATE procedure GetSequence
as
Begin
select * from your_table      
end
GO

whats the difference, and how would I get that value if I was to use an output parameter...when using the select statement like above...I could just call and refence the column name....but not sure why and how it would be with the output parameter.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Expert Comment

by:lahousden
ID: 20412931
Use Angel's first response to get the value in the result set instead of an output parameter.  You haven't indicated how you are going to use this SP, so that's why a couple of different ways of doing it have been suggested.  For instance, if you are going to call this SP from another T-SQL batch or Stored Procedure then you will not be able to access the value if you just return it in a result set, but you would be able to get it from an output parameter.  If you are just going to call the SP from a Client-Side programming technology then using the Result Set version should be fine.
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 20413673
If this is SQL 2005 you could also do:

create proc usp_IncrementAndReturn @id int as
UPDATE mycol SET mycol=mycol+1 OUTPUT inserted.mycol WHERE id=@id
GO
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20413879
the second example would be used like this in query analyser:

declare @value int
exec  GetSequence @value output
select @value result
0
 

Author Closing Comment

by:lobos
ID: 31412883
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 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

13 Experts available now in Live!

Get 1:1 Help Now