Solved

select count plus 1 and update it

Posted on 2007-12-05
8
392 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

785 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