0

I would like to recreate the way ms sql server creates a GUID via the newid() function.

The GUID generated through that function is of the format: 7792AF32-2AB9-4681-98FF-698316ED168C for example.

I am looking to reproduce the exact logic of construction of the newid() function of MS-SQL-SERVER (I am not using Native SQL and do not want to)

EDIT: My need is to use it within doctrine to populate a mssqlserver database.

Marc
  • 9,217
  • 21
  • 67
  • 90
  • `NEWID()` already does what you need...what's the purpose of this? This question stands dangerously close to being down voted or closed as "not useful" unless you can provide some substantive, meaningful details. – rory.ap Apr 26 '17 at 11:11
  • i would start by knowing how they work,below is a series from Eric Lippert,i found usefull...https://ericlippert.com/2012/04/30/guid-guide-part-two/ – TheGameiswar Apr 26 '17 at 11:11
  • 2
    A GUID is basically just a 128bit number displayed in a different way. But why would you want to do what is already there and is battle hardened and tested by millions of people? – DavidG Apr 26 '17 at 11:14
  • 1
    @DavidG -- it's like bringing a GUID to a gun fight (reference to "battle hardened") :) – rory.ap Apr 26 '17 at 11:18
  • 2
    ["`NEWID()` is compliant with RFC 4122"](https://learn.microsoft.com/en-us/sql/t-sql/functions/newid-transact-sql). If you [read that](https://tools.ietf.org/html/rfc4122), it should give you a better idea of why "recreating the way" is pretty much no effective solution to any problem you could have, but those are the details. Most languages/frameworks will have built-in ways of generating GUIDs by this exact algorithm that won't clash with what SQL Server generates, and that's really all you should need. – Jeroen Mostert Apr 26 '17 at 11:18
  • Just for extra clarification: *do use* existing features for generating GUIDs, do not reimplement your own, as this has a high chance of getting it wrong. And whatever you do, do not simply take a string of hexadecimal digits you've produced somehow, insert dashes, and call it a day; this happens more often than it should, but the resulting "fake GUIDs" have no uniqueness guarantees whatsoever. – Jeroen Mostert Apr 26 '17 at 11:27
  • Hello all. Thks for the help. My goal is to recreate it within doctrine to populate a mssqlserver database (I am not using Native SQL and cannot switch to it) – Marc Apr 26 '17 at 11:27
  • Doctrine is PHP, no? [Then you're really asking this](http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid). (Be *sure* to read the answers and comments carefully, there's plenty wrong with some -- including the accepted answer!) – Jeroen Mostert Apr 26 '17 at 11:30
  • @JeroenMostert Yes indeed – Marc Apr 26 '17 at 11:33
  • 1
    I don't know how Doctrine handles columns with defaults, but assuming it supports those, given the sorry state of GUID generation for PHP, you're better off having a column with `DEFAULT NEWID()` and leave it to SQL Server. You don't need to be bothered about using SQL if the GUID is neatly generated in the background. – Jeroen Mostert Apr 26 '17 at 11:36

0 Answers0