How To: Generate lots of Globally unique identifiers (GUID) using SQL 2008

Syed Aslam Basha here from the Information Security Tools Team.

For one of my projects I had to load the database (DB) with huge number of transactions and check for look and feel of the application. Some of the fields in DB required unique identifier, with the following SQL statement you can generate single GUID

  1: select NEWID()

 

You can use the following code snippet to generate the required number of GUIDs by modifying the RequiredNoOfGuids value

  1: use master
  2: GO 
  3: drop table #GUIDstable
  4: create table #GUIDstable (GUIDsCol uniqueidentifier)
  5: declare @RequiredNoOfGuids int
  6: set @RequiredNoOfGuids = 2
  7: while @RequiredNoOfGuids > (select count (1) from #GUIDstable)
  8: begin
  9: insert into #GUIDstable select NEWID()
  10: end
  11: go
  12: select * from #GUIDstable
  13: go

I am sure there are many ways of doing the same, I have used temp tables for my requirement. Any other ways?

-Syed Aslam Basha ( syedab@microsoft.com )

Microsoft Information Security Tools (IST) Team

Test Lead

---------------------------------------------------------

Please leave a comment if the blog post has helped you.