Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to Generate GUID/UUID?

Author  Topic 

mattboy_slim
Yak Posting Veteran

72 Posts

Posted - 2009-03-03 : 14:07:25
Is it possible to generate a GUID/UUID with T-SQL? I would like to insert a GUID with each INSERT done as a reference.

Here is the current code:

INSERT tb_alerts
(
f_alertname,
f_alertgenerated,
f_machineaffected
)
SELECT 'Machine Not Reporting',
GETDATE(),
x.f_itemGUID
FROM tb_locationsmachines AS x
WHERE x.f_machinelastreported < DATEADD(MINUTE, -15, GETDATE())
AND NOT EXISTS (SELECT * FROM tb_alerts AS y WHERE y.f_machineaffected = x.f_itemGUID)

And I would like something like the following:

INSERT tb_alerts
(
f_alertname,
f_alertGUID
f_alertgenerated,
f_machineaffected
)
SELECT 'Machine Not Reporting',
GUID()
GETDATE(),
x.f_itemGUID
FROM tb_locationsmachines AS x
WHERE x.f_machinelastreported < DATEADD(MINUTE, -15, GETDATE())
AND NOT EXISTS (SELECT * FROM tb_alerts AS y WHERE y.f_machineaffected = x.f_itemGUID)


I've searched the forum and Google with no luck, so I'm passing this off to the experts.
Thanks in advance,
Matt

mattboy_slim
Yak Posting Veteran

72 Posts

Posted - 2009-03-03 : 14:09:58
Nevermind, I found it. Apparently I suck at Google :)

newid()
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-03 : 14:12:19
random or what?
Have you looked into NEWSEQUENTIALID ( ), NEWID ( ). search for GUID in BOL

Go to Top of Page

mattboy_slim
Yak Posting Veteran

72 Posts

Posted - 2009-03-03 : 14:15:48
Random is what I'm looking for. Since there will be, at most, 100 records in the table at any given time, I'm not worried about the chance of collisions.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-03 : 14:50:57
there are dozens of randomizers posted in this forum just search for th ekety word "random" or "randomizer". it depends how fancy you want to get.
Go to Top of Page
   

- Advertisement -