Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1239
  • Last Modified:

Reproducing the functionality of Oracle Sequence object on SQL Server

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
andrepires
Asked:
andrepires
2 Solutions
 
James MurrellProduct SpecialistCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
dportasCommented:
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
dportasCommented:
angelIII: Somehow I don't think you tested the function you posted ;-)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
dportasCommented:
angelIII: So you STILL didn't get around to testing it then...? :0)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
andrepiresAuthor Commented:
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
 
dportasCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
dportasCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>You can't do it in a function.
gotcha !!!!
I forgot that bit :(
thanks for the correction
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now