Create a sequence named my_seq
create sequence my_seq
as int
start with 1
increment by 1
GO
Create a table in which one of the columns has a default value of my_seq
create table testing (col1 int, col2 int default next value for my_seq)
Now add some data to the table
insert into testing (col1)
select 34 union all
select 6
Select data from the table and see what col2 returns
select * from testing
Col2 returns unique numbers. It should be noted that if the sequence is used by many objects, the value may not be sequential i.e. some values may be used somewhere else.
This way we can use sequence object to generate unique numbers like an identity column
No comments:
Post a Comment