POST POST

JUN
3
2021

Sequences

ORIGINALLY POSTED TO: https://blog.simontimms.com/2021/06/03/sequence

Sequences are a handy feature in SQL server which provide an increasing, unique number. You wouldn't typically use them directly but might use them under the covers in an identity. However from time to time they are useful when you need numbers but your primary key is a uniqueidentifier or you need two different ways of numbering records. I've been using them to associate records in a table into groups.

1
2
3
create SEQUENCE Seq_PermitNumber 
start with 1
increment by 1

You can then use them like this

1
2
3
update tblManualPayment 
set PermitNumber = next value for Seq_PermitNumber
where PermitNumber is null

This will give each record a unique permit number.


Simon Timms

Email Email
Web Web
Twitter Twitter
GitHub GitHub
RSS

Looking for someone else?

You can find the rest of the Western Devs Crew here.

© 2015 Western Devs. All Rights Reserved. Design by Karen Chudobiak, Graphic Designer