Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Default values
How to auto-generate values in a database
17. INSERT using SEQUENCE
Summary

Instruction

Okay. Now let's practice using sequence. Let's say we've created a database sequence called IdSeq. We can use it to insert data into the Post table, like this:

INSERT INTO Post (Id, AuthorId, Title, Text, ModifiedDateTime) 
VALUES (NEXT VALUE FOR IdSeq, 4, N'A celebrity''s holiday', 
N'Lucy, a celebrity, is going to spend lots of time on holiday in Japan.', DEFAULT);

In this query, we use NEXT VALUE FOR to get the next value generated by our sequence. We put that value in the Id column. The start value has been set to 16, and the increment value has been set to 1.

It's your decision when and where to use sequences. Just remember that it's an available option when you need to have unique identifiers for columns.

Exercise

The Author table uses the same sequence (IdSeq) to insert new values in the Id column. Insert information for a new author, Lisa Thomas. She has an active account but no photo. Use IdSeq to generate an ID number for her.

Stuck? Here's a hint!

Use:

NEXT VALUE FOR IdSeq