Partager via


Please join me and welcome "SEQUENCEs" to the family of T_SQL

yes it’s, it’s the Sequence that you can use as a number generator separated from Columns and tables. it’s a new Object added to SQL 2012 ANSI Standard. to be used in many situations. I can remember now that it would made my life easier if I had it before. let’s have a look on the implementation

1. we’ll create a new sequence with T-SQL

 CREATE SEQUENCE [dbo].[S2] 
 START WITH 200
 INCREMENT BY 20
 GO

Or from the GUI under <DATABASE NAME>programmatically/sequences

image

2. to get the next Value for it simply type (Next Value For S2)

 select (next value for s2)

so a simple table with two columns (ID bigint, TheText varchar(5) )

 insert into test1(thetext) 
values ( next value for s2, 'Text1')

or even put the default binding for the ID Column to be (Next Value For s2 )

image

I can’t even think if the options. one thing I needed before is to update a table with an Auto Number. now you can do it without a loop.

So Welcome Sequence to to T-SQL Family