April 13, 2009 at 6:34 am
April 13, 2009 at 6:36 am
I agree. I think the correct answer is No...
April 13, 2009 at 6:44 am
It appears the correct answer is determined using the RAND() function.
April 13, 2009 at 7:27 am
Noel McKinney (4/13/2009)
It appears the correct answer is determined using the RAND() function.
Best explanation yet for the correct, er, incorrect answer!:-D
April 13, 2009 at 7:34 am
I am assuming that this will be corrected in a while, with No being set to the correct answer?
April 13, 2009 at 8:27 am
Well, as of now (8:26 MDT) the answer hasn't been corrected.
Still says Yes is the correct answer.
April 13, 2009 at 8:31 am
There is, of course, a way around this problem. Simply build a view that returns the value of the function RAND() and use the view in your UDF.
You can find an example using NEWID() in my blog A Variable Length Random String.
April 13, 2009 at 8:36 am
When we study on the question and give answer and found that the answer is wrong we feel bad... so if we answer correct we should get the responce otherwise there will be confusion in our mind abt the concept for future....thats my thining.....
Thanx.
Vinay
Thanx.
Vinay
http://rdbmsexperts.com/Blogs/
http://vinay-thakur.spaces.live.com/
http://twitter.com/ThakurVinay
April 13, 2009 at 9:20 am
The wrong answer was marked correct. This has been fixed and points awarded back.
April 14, 2009 at 6:17 am
Vinay (4/13/2009)
When we study on the question and give answer and found that the answer is wrong we feel bad... so if we answer correct we should get the responce otherwise there will be confusion in our mind abt the concept for future....thats my thining.....Thanx.
Vinay
My thinking is, if I know my answer is correct, there is no confusion about the concept. I treat it as a trick question designed to get me to think about it a little more in-depth and polish my skills a bit. Microsoft, and forums like these, have made it so easy to find resolutions without a lot of effort. QOTD forces you to think a bit and apply what you've learned to come up with the correct answer. Right or wrong, the old brain starts grinding and you have to apply yourself as opposed to simply waiting for a reply to a post (I know I've done that many times). Just my 2 cents so early in the morning...
-- You can't be late until you show up.
April 15, 2009 at 5:06 am
Douglas Duncan (4/11/2009)
The correct answer is no, and I would imagine that the people in power will correct the issue soon. In addition to the explanation given in the answer, the following which is taken directly from the MSDN page used as a reference states you cannot use RAND (in addition to several other built-in functions) in UDFs:Built-in functions that can return different data on each call are not allowed in user-defined functions. The built-in functions not allowed in user-defined functions are:
@@CONNECTIONS
@@CPU_BUSY
@@IDLE
@@IO_BUSY
@@MAX_CONNECTIONS
@@PACK_RECEIVED
@@PACK_SENT
@@PACKET_ERRORS
@@TIMETICKS
@@TOTAL_ERRORS
@@TOTAL_READ
@@TOTAL_WRITE
GETDATE
GetUTCDate
NEWID
RAND
TEXTPTR
Douglas, you are quite correct when you say that MSDN lists all of the above as built-in functions that cannot be used in a UDF because they are non-deterministic. However, you should note that in the heading of the article it also says Creating and Maintaining Databases (SQL Server 2000). Under SQL 2K8 (and presumably SQL2K5 as well, didn't try that) only two of those can not be used, namely RAND() and NEWID(). So, the limitation these days is really that one cannot use built-in functions that potentially may have side-effects.
April 15, 2009 at 6:07 pm
Jan Van der Eecken (4/15/2009)
Douglas, you are quite correct when you say that MSDN lists all of the above as built-in functions that cannot be used in a UDF because they are non-deterministic. However, you should note that in the heading of the article it also says Creating and Maintaining Databases (SQL Server 2000). Under SQL 2K8 (and presumably SQL2K5 as well, didn't try that) only two of those can not be used, namely RAND() and NEWID(). So, the limitation these days is really that one cannot use built-in functions that potentially may have side-effects.
Thanks for the correction Jan. I didn't even pay attention to the version that the text was for when posting my note. I just blindly used the link provided as the reference. It would be nice if the questions listed which version of SQL they pertained to since there are situations where an answer could be different based off the version.
Thanks to these questions and people like yourself posting on these boards I have learned more about SQL than I would have done on my own.
April 20, 2009 at 7:03 am
Answer is NO , explanation is you can not use NON Deterministic functions inside a UDF
April 21, 2009 at 1:10 am
Piyush,
I beg to differ. Try this:
CREATE FUNCTION [dbo].[usfn_GetDateTime] ( )
RETURNS datetime
AS
BEGIN
RETURN GETDATE ( )
END
GO
SELECT [dbo].[usfn_GetDateTime]()
Although GETDATE() is clearly non-deterministic this compiles properly and the SELECT statement returns the expected result. All that really happens is that your own UDF will be flagged as non-deterministic if it contains calls to other non-deterministic functions (be they built-in or another UDF) or extended stored procedures. This in turn puts some restrictions on where such a UDF can be used.
Regards,
Jan
April 21, 2009 at 1:19 am
Jan,
I am using SQL Server 2000 and it does not allow me to use getDate() inside a function, when I compile your function it gives me error
Msg 443, Level 16, State 1, Procedure usfn_GetDateTime, Line 7
Invalid use of 'getdate' within a function.
Viewing 15 posts - 16 through 30 (of 31 total)
You must be logged in to reply to this topic. Login to reply