What is NEWSEQUENTIALID()

Posted on : 30-06-2011 | By : Devi Prasad | In : SQL Server Basics

Share:

0


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.

 SELECT NEWSEQUENTIALID() 

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:

CREATE TABLE Table1 (Col1 uniqueidentifier DEFAULT NEWSEQUENTIALID()) 

How are values inserted into columns with NEWSEQUENTIALID?

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

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.

(Visited 30 times, 1 visits today)



References : Devi Prasad (sqlserverlearner.com)

Need Help On SQL Server?

Cannot Find Solution to your problem (or) If you are looking for some help on SQL Server. Dont worry Click Here to Post your question and solve your issue.


Do you like my blog?

If you liked reading this blog, please help spread the word by sharing this blog with your friends.




Write a comment



Tags: , , ,