SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL


TSQL

Author
Message
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39039 Visits: 38514
Well, as of now (8:26 MDT) the answer hasn't been corrected.
Still says Yes is the correct answer.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39039 Visits: 38514
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Vinay Thakur-585143
Vinay Thakur-585143
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1755 Visits: 778
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
Steve Jones
Steve Jones
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: Administrators
Points: 61911 Visits: 19099
The wrong answer was marked correct. This has been fixed and points awarded back.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
tosscrosby
  tosscrosby
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3276 Visits: 5316
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.
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2430 Visits: 6494
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)
Douglas Duncan
Douglas Duncan
SSC Eights!
SSC Eights! (881 reputation)SSC Eights! (881 reputation)SSC Eights! (881 reputation)SSC Eights! (881 reputation)SSC Eights! (881 reputation)SSC Eights! (881 reputation)SSC Eights! (881 reputation)SSC Eights! (881 reputation)

Group: General Forum Members
Points: 881 Visits: 325
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.
piyush.dhangar
piyush.dhangar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 36
Answer is NO , explanation is you can not use NON Deterministic functions inside a UDF
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2430 Visits: 6494
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)
piyush.dhangar
piyush.dhangar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 36
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search