Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL parameters in scalar UDFs on MSSQL

The option "RETURNS NULL ON NULL INPUT" for a scalar UDF (see CREATE FUNCTION) stops the function body executing if the parameter is null and simply returns NULL.

That is, it short circuits.

Does anyone know how it handles multiple parameters?

It would be useful to short circuit a function call with multiple parameters, say if the first one is NULL at least.

When I have time, I'll use profiler to try and trace the udf calls. I've searched but can't find anything.. more likely I may not have used the correct search terms.

In the meantime, does anyone have any ideas or experience?

Answers from the other RDBMS worlds are welcome too.. this is an ANSI setting and I saw results for DB2 and MySQL in my searches

Edit, based on comment: For non-CLR functions only

Cheers S

Edit: I don't need to run profiler. Doh! This demonstrates the behaviour:

CREATE FUNCTION dbo.ufnTest (
    @dummy tinyint
)
RETURNS tinyint
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
    RETURN 1
END
GO
SELECT dbo.ufnTest(0), dbo.ufnTest(NULL)
GO


ALTER FUNCTION dbo.ufnTest (
    @dummy tinyint
)
RETURNS tinyint
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
    RETURN 1
END
GO
SELECT dbo.ufnTest(0), dbo.ufnTest(NULL)
GO


ALTER FUNCTION dbo.ufnTest (
    @dummy tinyint,
    @dummy2 tinyint
)
RETURNS tinyint
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
    RETURN 1
END
GO
SELECT dbo.ufnTest(0, 2), dbo.ufnTest(NULL, 2), dbo.ufnTest(0, NULL)
GO


ALTER FUNCTION dbo.ufnTest (
    @dummy tinyint,
    @dummy2 tinyint
)
RETURNS tinyint
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
    RETURN 1
END
GO
SELECT dbo.ufnTest(0, 2), dbo.ufnTest(NULL, 2), dbo.ufnTest(0, NULL)
GO
like image 756
gbn Avatar asked Feb 16 '09 14:02

gbn


People also ask

Which values can not be returned by a scalar UDF?

For example, a UDF cannot return a value of any of these datatypes: text, ntext, image, cursor, or timestamp.

How do you execute a scalar valued function in SQL Server with parameters?

Scalar-valued functions can be executed by using the EXECUTE statement. If you EXECUTE a function rather than use it in a SELECT statement or constraint, you can leave out the schema name in the function name, and it will look in the dbo schema followed by the users default schema.

Can function return NULL value in SQL Server?

The function always returns NULL when executed, though data is present.

What is a scalar UDF in SQL?

In SQL Server, a scalar UDF, or scalar user-defined function, is a user-defined function that returns a single value. This is in contrast to a table-valued function, which returns a result set in the form of a table. User-defined functions can be written as either T-SQL UDFs or CLR (Common Language Runtime) UDFs.


1 Answers

Yes, a function that specifies RETURNS NULL ON NULL INPUT will short-circuit if any of its parameters are NULL.

The documentation does suggest this, although it's unclear and appears to be referring only to CLR functions. (I think this is Microsoft's attempt to clarify that the NULL behaviour specified in CREATE FUNCTION will override the OnNullCall attribute on the CLR method.)

I've tested this with non-CLR functions in both SQL2005 and SQL2008 and it short-circuits exactly as expected.

like image 165
LukeH Avatar answered Oct 27 '22 10:10

LukeH