Notes: -
1. The table should have ‘ROW LEVEL LOCKING’
2. The sequence generator process is stateless (See more details in Object Oriented Programming)
3. Create one row for each target table in the sequence master table. Do not try to use one sequence for multiple tables. It will work but probably is not a good idea.
Step 1: -Create a table with following structure.
CREATE TABLE sequence_master (
sequence_nm varchar (55) NOT NULL ,
sequence_num integer NOT NULL
)
GO
Step 2: -Create a stored procedure that will return the next sequence.
CREATE PROCEDURE p_get_next_sequence
@sequence_name varchar(100)
AS
BEGIN
DECLARE @sequence_num INTEGER
— Returns an error if sequence row is entered into the table.
SET @sequence_num = -1
UPDATE sequence_master
SET @sequence_num = sequence_num = sequence_num + 1
WHERE Sequence_name = @sequence_name
RETURN @sequence_num
END
GO