Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234»»

TSQL Expand / Collapse
Author
Message
Posted Monday, April 13, 2009 8:27 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 23,083, Visits: 31,624
Well, as of now (8:26 MDT) the answer hasn't been corrected.
Still says Yes is the correct answer.



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)
Post #695832
Posted Monday, April 13, 2009 8:31 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 23,083, Visits: 31,624
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.



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)
Post #695840
Posted Monday, April 13, 2009 8:36 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 8:44 PM
Points: 1,520, Visits: 739
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
Post #695847
Posted Monday, April 13, 2009 9:20 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 5:13 PM
Points: 33,100, Visits: 15,210
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
Post #695885
Posted Tuesday, April 14, 2009 6:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 19, 2011 7:31 AM
Points: 2,832, Visits: 5,316
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.
Post #696462
Posted Wednesday, April 15, 2009 5:06 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 2,441, Visits: 6,433
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)
Post #697383
Posted Wednesday, April 15, 2009 6:07 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, December 14, 2012 6:45 AM
Points: 881, Visits: 324
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.
Post #698066
Posted Monday, April 20, 2009 7:03 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, December 17, 2012 7:13 AM
Points: 235, Visits: 36
Answer is NO , explanation is you can not use NON Deterministic functions inside a UDF
Post #700570
Posted Tuesday, April 21, 2009 1:10 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 2,441, Visits: 6,433
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)
Post #701199
Posted Tuesday, April 21, 2009 1:19 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, December 17, 2012 7:13 AM
Points: 235, 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.
Post #701202
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse