Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Screenshots of the new look and feel of BIDS in DenaliFirst look at SQL Server Management Studio Denali »
    comments

    People have been asking for sequences for a very long time and now finally it is included with SQL Server Denali CTP 1. You don't have to muck around with an identity table that you share across many tables anymore.

    A simple sequence will look like this

    1. CREATE SEQUENCE GlobalCounter
    2.     AS INT
    3.     MINVALUE 1
    4.     NO MAXVALUE
    5.     START WITH 1;
    6. GO

    To get the next value from the sequence you would do something like this

    1. SELECT NEXT VALUE FOR GlobalCounter -- 1
    2.  
    3. SELECT NEXT VALUE FOR GlobalCounter -- 2

    As you can see, the value gets incremented by one.

    Now we will create a table and instead of using an identity, we will use the sequence as a default

    1. CREATE TABLE bla (id INT DEFAULT NEXT VALUE FOR GlobalCounter)

    Insert some data into the table, one row will use the default, the other row will call the sequence

    1. INSERT bla DEFAULT VALUES  --3
    2.  
    3. INSERT bla VALUES(NEXT VALUE FOR GlobalCounter)  --4

    Now let's see what is in the table, should be 3 and 4

    1. SELECT * FROM bla  --3,4

    Here is one more example, this one will show you what happens when you reach the maximum value and how to reset it

    1. CREATE SEQUENCE GlobalCounterTest
    2.     AS BIGINT
    3.     MINVALUE 1
    4.     MAXVALUE 2
    5.     START WITH 1;
    6. GO

    These two statements will succeed

    1. SELECT NEXT VALUE FOR GlobalCounterTest --1
    2.     SELECT NEXT VALUE FOR GlobalCounterTest --2

    this one will fail

    1. SELECT NEXT VALUE FOR GlobalCounterTest  --error

    Here is the error message
    Msg 11728, Level 16, State 1, Line 1
    The sequence object 'GlobalCounterTest' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.

    To reset a sequence, you need to use restart

    1. ALTER SEQUENCE GlobalCounterTest RESTART
    2. GO

    Let's take a look at another example, this sequence will generate values 1 and 2 and will restart once you reach the max, this is accomplished by using cycle

    1. CREATE SEQUENCE MySequence
    2.  MINVALUE 1
    3.  MAXVALUE 2
    4.  CYCLE

    Now run these 4 statements and you will see 2 rows with the vale 1 and two rows with the value 2

    1. select NEXT VALUE FOR MySequence  --1
    2.  select NEXT VALUE FOR MySequence  --2
    3.  select NEXT VALUE FOR MySequence  --1
    4.  select NEXT VALUE FOR MySequence  --2

    You can also use the tinyint data type

    1. CREATE SEQUENCE TinySequence
    2.     AS TINYINT
    3.     MINVALUE 1
    4.     NO MAXVALUE
    5.     START WITH 1;

    Of course tinyint can only hold values up to 255, so if you run this in SSMS

    1. select NEXT VALUE FOR TinySequence
    2. GO 256

    Msg 11728, Level 16, State 1, Line 1
    The sequence object 'TinySequence' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.

    One more example, if you create a sequence like this without specifying a start value, it will start at -2147483648 for an integer

    1. CREATE SEQUENCE TestSequence
    2. NO MAXVALUE;
    3.  
    4. select NEXT VALUE FOR TestSequence

    output
    ------------
    -2147483648



    Sequence Folder

    If you navigate to the sequence folder and right click on a sequence and then select properties you will see the following image
    Sequence Properties
    From here you can change the sequence and you can also create scripts by clicking on the script icon

    Click on the SQL Server Denali tag to see all our SQL Server Denali related posts

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    4316 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)