Skip navigation
Using the newsequentialid() GUID Function

Using the newsequentialid() GUID Function

In SQL Server 2000, the only SQL Server function for GUIDs is newid, but this function doesn't create an ever-increasing pattern. In SQL Server 2005, you can use a new GUID function called newsequentialid() to populate your uniqueidentifier column. Here's an example of how you can use it:

CREATE TABLE Test 
( 
TestID uniqueidentifier
  CONSTRAINT Test_TestID_Default
       DEFAULT newsequentialid(),
Inserted datetime
  CONSTRAINT Test_Inserted_Default
       DEFAULT getdate()
) 
go 

INSERT Test DEFAULT VALUES
go
 
SELECT * FROM Test
go

Is there a way to create a sequential GUID in SQL Server 2000? Yes—you can use an extended procedure to generate sequential GUIDs or you can leverage someone else's code. Gert Drapers wrote an extended stored procedure, which he has published on his Web site, SQLDev.NET, at http://sqldev.net/xp/xpguid.htm.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish