What is NEWSEQUENTIALID()

NEWSEQUENTIALID()

New NEWSEQUENTIALID generates new GUID.

The GUID generated is always greater than GUID that was previously generated.

Thus the new values inserted into tables using NEWSEQUENTIALID are in assending order and helps to fill index pages easyly.

NEWSEQUENTIALID is faster in operation than NEWID.

If a GUID Column in a table has to be bound with a unique Identifier then using NEWSEQUENTIALID is preferred.But the values can be predicted

NEWSEQUENTIALID cannot be used in queries like NEWID.

[sql] SELECT NEWSEQUENTIALID() [/sql]

The above query would raise an error:

The newsequentialid() built-in function can only be used in a DEFAULT expression for a column of type ‘uniqueidentifier’ in a CREATE TABLE or ALTER TABLE statement. It cannot be combined with other operators to form a complex scalar expression.

Syntax for using NEWSEQUENTIALID with tables:
[sql]CREATE TABLE Table1 (Col1 uniqueidentifier DEFAULT NEWSEQUENTIALID()) [/sql]

How are values inserted into columns with NEWSEQUENTIALID?

[sql]CREATE TABLE Table1 (Col1 uniqueidentifier DEFAULT NEWSEQUENTIALID())
GO

INSERT INTO Table1(Col1)
VALUES (DEFAULT)

INSERT INTO Table1(Col1)
VALUES (DEFAULT)

INSERT INTO Table1(Col1)
VALUES (DEFAULT)

INSERT INTO Table1(Col1)
VALUES (DEFAULT)
GO

SELECT * FROM TABLE1
GO[/sql]

The output of the above query will be something like this:
07D7AE4D-97A3-E011-B424-0021971FCF00
08D7AE4D-97A3-E011-B424-0021971FCF00
09D7AE4D-97A3-E011-B424-0021971FCF00
0AD7AE4D-97A3-E011-B424-0021971FCF00

It can be seen that the values here are in sequence.

Leave a Reply

Your email address will not be published. Required fields are marked *