SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to Generate GUID/UUID?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mattboy_slim
Yak Posting Veteran

70 Posts

Posted - 03/03/2009 :  14:07:25  Show Profile  Reply with Quote
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

70 Posts

Posted - 03/03/2009 :  14:09:58  Show Profile  Reply with Quote
Nevermind, I found it. Apparently I suck at Google :)

newid()
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1635 Posts

Posted - 03/03/2009 :  14:12:19  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
random or what?
Have you looked into NEWSEQUENTIALID ( ), NEWID ( ). search for GUID in BOL

Go to Top of Page

mattboy_slim
Yak Posting Veteran

70 Posts

Posted - 03/03/2009 :  14:15:48  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1635 Posts

Posted - 03/03/2009 :  14:50:57  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000