sql-serverfunctionnewid

newid() inside sql server function


I have to insert a fake column at the result of a query, which is the return value of a table-value function. This column data type must be unique-identifier. The best way (I think...) is to use newid() function. The problem is, I can't use newid() inside this type of function:

Invalid use of side-effecting or time-dependent operator in 'newid()' within a function.


Solution

  • Here's a clever solution:

        create view getNewID as select newid() as new_id
        
        create function myfunction ()
        returns uniqueidentifier
        as begin
           return (select new_id from getNewID)
        end
    

    That I can't take credit for. I found it here