Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQl Server Sequence Function Getting To Increment!

Posted on 2004-09-29
4
Medium Priority
?
1,475 Views
Last Modified: 2012-06-27
I was in need of a sequence function for SQL Server and this article does the trick:

http://www.winnetmag.com/Article/ArticleID/22442/22442.html

As I implemented this example figure 4 with success:

http://www.winnetmag.com/Files/23/22442/Listing_04.txt

However I need it to increment by one each time it's called currently if I run this function like this:

SELECT id FROM fn_sequence(1,1)

It gives me a start value and end value of one which is what I want however I'd like to increment by one each time it's called - is this possible!
0
Comment
Question by:dylanone
[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
4 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12184680
The only way it can increment each time is if it stores the value between calls. If you use a global variable, it won't survive a stop/start of SQL Server. You can store the value in a table just for that purpose to overcome that durablity problem. Or, my preference would be to fetch the most recent value from the destination table and increment that for the insert (just to avoid creating a table to hold the variable).
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 500 total points
ID: 12185029
the following procedure will maintain a sequence number for you - each time it is called it returns the next value in the sequence.
If you want to reset it, simply drop the table MySequence


create procedure MySequenceProc
as
begin
      declare @val integer
      if not exists (select * from dbo.sysobjects where id = object_id(N'MySequence') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
      create table MySequence (id integer identity (1,1), dummy bit)
      delete from MySequence
      insert into MySequence values (1)
      select @val = id from MySequence

      return @val
      
end

you call it like this :

declare @value integer
exec @value = MySequenceProc
select @value

0
 
LVL 12

Expert Comment

by:monosodiumg
ID: 12185235
Read the article. I use a table of in  quite regularly. I populate a tabkle of ints with values from 0 up to whatever. Don't understand why they present a  solution hardcoded to be limited to 10K.  There doesn't seem a lot of value to being able to specify the initial value since any user can simply add that value to the sequence of ints starting at 0.

If you need it to persists across session then , as others have said, you need persistent storage so you need a table.
Create a table with and indentity column and insert a record to get a new value from the identity generating mechanism. You can delete any records you create on the same occasion. That will give you a monotonic increasing series. Not guaranteed gap free under operational error though.
0
 

Author Comment

by:dylanone
ID: 12189599
Perfect - exactly what I needed!
0

Featured Post

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!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

609 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