Solved

Reproducing the functionality of Oracle Sequence object on SQL Server

Posted on 2007-11-21
12
1,211 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
Comment Utility
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]
Comment Utility
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
Comment Utility
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
Comment Utility
angelIII: Somehow I don't think you tested the function you posted ;-)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
angelIII: So you STILL didn't get around to testing it then...? :0)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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]
Comment Utility
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
Comment Utility
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]
Comment Utility
>You can't do it in a function.
gotcha !!!!
I forgot that bit :(
thanks for the correction
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

6 Experts available now in Live!

Get 1:1 Help Now