Solved

Reproducing the functionality of Oracle Sequence object on SQL Server

Posted on 2007-11-21
12
1,214 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
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 142

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

Expert Comment

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

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

 
LVL 142

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 142

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup

930 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now