In order to use newid() in a UDF, I’ve created a view that can help me:
create view RandomUUID as select newid() as UUID
In this way, UDFs can now get access to newid(). Cool.
My question, however, is what is the best way to select this? Does it make sense to add a TOP 1 or (nolock) to the query in my UDF? As in:
select UUID from RandomUUID
vs.
select top 1 UUID from RandomUUID (nolock) -- Or any other combo of query modifiers
UPDATE:
This SqlFiddle demonstrates how this is being used.
There is no reason to add (nolock) because there is no record involved to lock! For the record,
(NOLOCK)should be written asWITH (nolock)from SQL Server 2008 onwards (or was it 2005).TOP (1)will add a SORT operator here that will be extraneous, since there is only ever one row created.You can create it like this:
Note:
(nolock)will be optimized away, butTOP(1)adds a SORT operation as seen here (expand the Execution plans).