Friday, 20 March 2026

Can We Share same sequence between multiple tables

Yes, SQL Server SEQUENCE can be shared across multiple tables. It is a separate database object and not table dependent like IDENTITY. Multiple tables can generate values using NEXT VALUE FOR. It ensures globally unique numbering, supports caching, works in distributed systems, and is ideal for enterprise microservice architecture. However, sequence is not transactional and gaps may occur, which is acceptable in real enterprise systems.

See the demo

Creating a sequence

CREATE SEQUENCE Globle_Seq_Demo

    AS BIGINT

    START WITH 500000

    INCREMENT BY 1;

Now creating tables.

create table Globle_Seq_tbl1

(

id bigint ,

Nm varchar(50)

create table Globle_Seq_tbl2

(

id bigint ,

Nm varchar(50)

)

 Now inserting records in both tables using the same sequence object.

See the records in both tables.

So, we can use sequence in the multiple tables.

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts