?
Solved

select count plus 1 and update it

Posted on 2007-12-05
8
Medium Priority
?
410 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
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 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 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

741 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