TSQL

  • I love it when the explanation contradicts the shown correct answer.......

    _______________________________________________________________________
    For better assistance in answering your questions, click here[/url]

  • I agree. I think the correct answer is No...

  • It appears the correct answer is determined using the RAND() function.

  • 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


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

  • I am assuming that this will be corrected in a while, with No being set to the correct answer?

  • Well, as of now (8:26 MDT) the answer hasn't been corrected.

    Still says Yes is the correct answer.

  • 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.

  • 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

  • The wrong answer was marked correct. This has been fixed and points awarded back.

  • 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.

  • 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.

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • 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.

  • Answer is NO , explanation is you can not use NON Deterministic functions inside a UDF

  • 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

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • 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