2

I've got a SQL Server table in which I have a column where I would like to select the current value and increment by one, is there a way to do this in a single query? This in order to mitigate the chance, however small it might be, that someone else gets the same number.

Something along the lines of this pseudo code:

SELECT NumSeriesCurrent 
FROM NumSeries 
(UPDATE NumSeries SET NumSeriesCurrent = NumSeriesCurrent+1) 
WHERE NumSeriesKey='X'
JaggenSWE
  • 1,950
  • 2
  • 24
  • 41

1 Answers1

5

To update the value and get the value in NumSeriesCurrent previous to the update you can use

UPDATE NumSeries 
SET NumSeriesCurrent += 1
OUTPUT DELETED.NumSeriesCurrent
WHERE NumSeriesKey='X'
Martin Smith
  • 438,706
  • 87
  • 741
  • 845