7

How does sql server determine if a function is deterministic or not?

Consider the following function, I think it's deterministic (as it's ultimately an integer operation), but sql server returns 0 when I call the objectproperty method on it.

Understanding how it works will help me understand why I'm wrong in my assessment of this function.

CREATE FUNCTION dbo.EndOfPreviousMonth ( @Date date )
RETURNS date
AS BEGIN
 RETURN DATEADD(day, 0 - DAY(@Date), @Date);
END;
SELECT dbo.EndOfPreviousMonth('2010-01-01'); --Usage example
--Returns 0, I expected it to return 1
SELECT OBJECTPROPERTY( OBJECT_ID( 'dbo.EndOfPreviousMonth' ), 'IsDeterministic' );
ErikE
4,3554 gold badges29 silver badges39 bronze badges
asked Feb 9, 2012 at 21:22
2
  • Are you running in compatibility mode for anything? (Ignore previous, DATEADD is deterministic.) Commented Feb 9, 2012 at 22:31
  • No, I don't think so. I guess that's to say I didn't set a compatibility mode. Commented Feb 10, 2012 at 13:33

1 Answer 1

7

First it looks whether used functions are deterministic.
Then it looks whether your function is schema bound.
(Or in reverse order.)

So add with schemabinding to your function.

answered Feb 9, 2012 at 23:28
4
  • That changed the answer to what I was expecting, but why? Commented Feb 10, 2012 at 13:34
  • @BrianDishaw I'm not sure, to be honest. Apparently because any data-accessing function is not deterministic unless schema bound, and SQL Server expands this behaviour to functions that don't in fact access data. Maybe it's not able to see they don't. Commented Feb 10, 2012 at 14:24
  • I don't understand it either. It would make more sense if I had something in the function that I could drop. Oh well, thanks for taking the time to answer my question! Commented Feb 10, 2012 at 14:43
  • 2
    Explained here Commented Dec 15, 2014 at 6:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.