[quoted text, click to view] On Sun, 3 Oct 2004 11:12:17 +0200, Agoston Bejo wrote:
>Hi!
>What you told solved the problem completely. I have some minor questions
>left, however:
Hi Agoston,
I hope you also read the article posted by Aaron (in reply to my message)
which shows why the workaround with a view has some problems as well.
Please follow his advice instead of my original advice.
I'll type the answers to your minor questions inline.
[quoted text, click to view] >"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> az alábbiakat írta a következo
>hírüzenetben: dc2ul09hsjru7d0der6jpqoe2cbics6e2m@4ax.com...
>> >1. Why can't non-deterministic functions be used?
>>
>> Because (unlike views) a user-defined function gets called multiple times
>> during the execution of one query; if the results change from call to
>> call, all kinds of nasty side-effects will kick in,
>
>I see, only to be clear on the case: does 'non-deterministic' in this
>terminology also mean that the function has side effects? It is possible
>that a 'non-deterministic' function (in the classical sense) may not change
>anything in the database.
No, 'non-deterministic' in this terminology means *only* whether the
function is deterministic or not - in other words: if the result of the
function (with the same arguments) will always be the same or not.
Not having side effects is *another* requirement for user-defined
functions (UDFs). I've seen some workarounds to this requirement posted in
these newsgroups as well, but I'd recommend against using them - there is
some solid reasoning behind the requirement of not having side effects.
The main reason is the fact that the optimizer is free to choose different
query plans, that might involve more or less calls to the UDFs used in the
query, and (if columns are used as parameters) different sequences. The
result of the query becomes inpredictable.
[quoted text, click to view] >> >4. How on Earth can such a simple function be non-deterministic?
>>
>> It isn't. :-)
>
>From the MSDN:
>
>"All built-in string functions, except for CHARINDEX and PATINDEX, are
>deterministic."
>
>(URL:
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_
>fa-fz_7oqb.asp - Transact-SQL Reference / Built-in functions)
>
>But I didn't get any error message, and CHARINDEX worked indeed in my
>function.
>Is the MSDN lying? :)
It isn't lying. Unclear, yes. But not lying.
First, let me give you a quote that is more relevant for you. The URL is
http://msdn.microsoft.com/library/en-us/createdb/cm_8_des_08_460j.asp. (start quote)
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 @PACK_SENT GETDATE
@@CPU_BUSY @PACKET_ERRORS GetUTCDate
@@IDLE @TIMETICKS NEWID
@@IO_BUSY @TOTAL_ERRORS RAND
@@MAX_CONNECTIONS @@TOTAL_READ TEXTPTR
@@PACK_RECEIVED @@TOTAL_WRITE
(end quote)
The quote you gave relates to determining whether your UDF will be
considered deterministic or non-deterministic. Since it contains a call to
CHARINDEX (which is considerde non-determinstic), your UDS will be
considered non-deterministic as well. This means that you can't create an
index on a view or computed column that is creating using this UDF. As
long as that is not your intention, it should not bother you.
(By the way - I do admit I often wondered WHY SQL Server regards CHARINDEX
and PATINDEX to be nondeterministic).
Best, Hugo
--