Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently found out that SQL Server 2005 doesn't allow the use of newid() within user-defined functions. Why is this?

I have an alternate solution that suits my needs, so I'm not looking for ways to get around this. I'm curious why the designers would make this decision.

share|improve this question
up vote 3 down vote accepted

SQL Server only allows deterministic functions to be used within user-defined functions. Since the value of NEWID() is not deterministic, it cannot be used. You will find the same thing is true with GETDATE() and any other non-deterministic function.

I'm not qualified to answer why they would make that decision. It annoys me as well but I'm sure the SQL team at MS are not slouches.

EDIT: It turns out that my knowledge about GETDATE() is out-of-date. As the commenter says, you can use GETDATE() within functions from SQL Server 2005 onward. However, you still cannot use NEWID(), which I believe has to do with the same non-determinism constraint.

share|improve this answer
GETDATE is allowed from SQL Server 2005 onwards – gbn Jan 28 '11 at 13:41
As @gbn says, I'm able to use getdate() - create function returns table as return select getdate() 'dt' works just fine. – goric Jan 28 '11 at 13:44
I believe it is down to the query planner needing some things to be reliably repeatable. Some optimisations that the QP uses are not "safe" (i.e. may result in inconsistent or just plain incorrect output) in some cases in the presence of non-deterministic functions. Presumably it is seen that denying the QP this optimisation options (or having it check each function before use) would be more of a problem (in terms of efficiency hits) than the determinism limitation. GETDATE is allowed now, but is only run once per query (so in the context of that one query, it returns a fixed value). – David Spillett Jan 28 '11 at 13:45
NEWID() is a non Deterministic function, which after 2005 is allowed in UDF but, as NEWID changes some of the internal counters and values inside the database structure it is considered to have a 'side effect' and as UDF's are not allowed to have side effects it is not allowed in the UDF, the same goes for RAND() as each call to RAND changes the seed value in the inner workings of MSSQL and this again is considered a side effect. GETDATE()/GETUTCDATE() and the allowed lists of functions have no side effects, so are allowed. HTH – Payload Aug 16 '12 at 8:11
@DavidSpillett: FYI you can get around the ban of newid() by reading from a view : CREATE VIEW vw_NewGuid AS SELECT NEWID() AS new_guid . Then in your UDF, you can do : SELECT new_guid FROM vw_NewGuid. – Dio Phung Jun 15 at 18:09

I believe the answer is that NEWID() is a non-deterministic function - i.e. it has side effects. User defined functions are not allowed to have side effects.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.