Solved

Reproducing the functionality of Oracle Sequence object on SQL Server

Posted on 2007-11-21
12
1,222 Views
Last Modified: 2012-08-13
Hello Experts!
Is there any object in the SQL Server that is similar to the Oracle Sequence object?
Identity columns are not what I'm looking for.

Thanks!
0
Comment
Question by:andrepires
[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
12 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 20330109
unsure found this in my notes

SQL Server in itself offers numerous equivalents of Oracle Packages, Sequences and Exception Handling methods in terms of T-SQL system functions, identities, and system and user defined error messages and functions. However, SSMA offers a choice to users whether to keep Oracle methodologies or employ much simpler and easy-to-use SQL Server methodologies. For example, in order to read LOB columns in an Oracle database table, DBMS_LOB package may have to be used. Such a situation is handled internally by SQL Server without explicit usage of any package by the user. Another example would be the use of the Oracle exception TOO_MANY_ROWS which can be easily handled by capturing the SQL Server system error variable @@error or the function ERROR_MESSAGE().

In code converted by SSMA from PL/SQL to T-SQL, references to Oracle packages and exceptions are made through calls to their simulated counterparts in the SQL Server database, SYSDB where such simulations are stored.

    While specifying conversion options, a user can choose to have SSMA simulate any or all of the Oracle Packages, do sequence-to-identity conversion or do conversion of exception handling.

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20330160
well, you will have to use an identity column anyhow, and use it like this:
create table tbl_sequence ( id int identity ) 
create function dbo.get_sequence_value(@x uniqueidentifier ) return int
as
begin
   declare @res int
   insert into tbl_sequence default values
   set @res = scope_identity()
   delete tbl_sequence where id < @res
   return @res
end 
 
and you can use it like this:
select dbo.get_sequence_value(newid()) sequence_value

Open in new window

0
 
LVL 22

Accepted Solution

by:
dportas earned 500 total points
ID: 20330260
There is no Sequence object but you can reproduce the behaviour of an independent sequence using a dedicated table and a proc to generate the values. The following technique originates with Itzik Ben Gan as far as I know. He discusses this and other alternatives in his book "Inside SQL Server 2005 - T-SQL Querying".

CREATE TABLE Sequence (id INT NOT NULL IDENTITY);
GO

CREATE PROCEDURE dbo.usp_GetNextSequenceValue
(
  @pSequenceValue INT OUTPUT
)
AS
BEGIN;
  BEGIN TRAN;
    INSERT INTO Sequence DEFAULT VALUES;
  ROLLBACK TRAN;
  SET @pSequenceValue = SCOPE_IDENTITY();
END;

GO
DECLARE @i INT;

EXEC dbo.usp_GetNextSequenceValue
  @pSequenceValue = @i OUTPUT;

SELECT @i;
0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 22

Expert Comment

by:dportas
ID: 20330277
angelIII: Somehow I don't think you tested the function you posted ;-)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20330306
there is indeed a typo
create function dbo.get_sequence_value(@x uniqueidentifier ) 
returns int
as
begin
   declare @res int
   insert into tbl_sequence default values
   set @res = scope_identity()
   delete tbl_sequence where id < @res
   return (@res)
end 

Open in new window

0
 
LVL 22

Expert Comment

by:dportas
ID: 20330351
angelIII: So you STILL didn't get around to testing it then...? :0)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20330380
well, it's difficult to test without an sql server instance around...
I guess you would not mind telling us what I am missing?
0
 
LVL 2

Author Comment

by:andrepires
ID: 20330406
Thanks guys.
AngellII, I don't know if I fully understood the code you showed here.
It seams to me like it is "similar" to what the Sequence object does, but sounds very unflexible.
What if in a certain moment and for some reason I need to set my increment value to something different? Ie. My column has an increment seed of 1 and suddenly I need the next value to jump by 100.
Can I easily do this in SQL Server?
0
 
LVL 22

Expert Comment

by:dportas
ID: 20330428
angelIII:

Msg 443, Level 16, State 15, Procedure get_sequence_value, Line 6
Invalid use of side-effecting or time-dependent operator in 'INSERT' within a function.
Msg 443, Level 16, State 15, Procedure get_sequence_value, Line 8
Invalid use of side-effecting or time-dependent operator in 'DELETE' within a function.

You can't do it in a function.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20330461
to change the increment, you could alter the table:
alter table tbl_sequence alter column id int identity(1, 100)

and then issue a reseed to ensure the identity seed is set to the value that is currently stored in the existing record, if any, in that table.
dbcc checkident (tbl_sequence, reseed )
0
 
LVL 22

Assisted Solution

by:dportas
dportas earned 500 total points
ID: 20330463
andrepires:

Yes. You can use DBCC CHECKIDENT to change the seed. Or use SET IDENTITY_INSERT ON to insert some value from which the next value will continue.

The big advantage of using IDENTITY in this way is that you get all the concurrency of IDENTITY (no blocking) but without the added complexity and irritations of an IDENTITY column in the table that matters. Note that you may still get gaps, which are inevitable unless you serialize all inserts (same applies to Oracle).

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20330473
>You can't do it in a function.
gotcha !!!!
I forgot that bit :(
thanks for the correction
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

749 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