Ok - so first: We WERE running a vanilla version of SQL 2000 (hey - it's a dev machine, OK?).
But: I tried my UDF on a machine running SP3 and got the same result. It's definitely a bug.
Friday, December 23, 2005 10:08:00 AM
I encountered a strange bug in some sql code I was refactoring this morning, when I replaced a number of IF .. ELSE statements with a simple COALESCE. The essence of the buggy code was as follows:
USE Northwind
GO
CREATE FUNCTION dbo.Foo (@EmployeeID int)
RETURNS nvarchar(20)
AS
BEGIN
--Declare a variable that really shouldn't be used
DECLARE @EmployeeName nvarchar(20)
SET @EmployeeName = 'Not expected'
RETURN COALESCE
(
(SELECT e.LastName FROM dbo.Employees e
WHERE e.EmployeeID = @EmployeeID),
'(Not Found)'
)
--The function SHOULD have exited by now...
RETURN @EmployeeName
END
GO
--Both of these return unexpected results
SELECT dbo.Foo(1) --Returns 'Not expected'
SELECT dbo.Foo(564654) --Returns 'Not expected'
As can be seen from the code - I would have expected the first RETURN to exit the function. Instead what is actually returned is the contents of the @EmployeeName.
This appears to be a bug in how SQL handles the combination UDF, multiple RETURNs and a SELECT statement within a COALESCE.
(For the record, the following version of the function works fine:)
USE Northwind
GO
ALTER FUNCTION dbo.Foo (@EmployeeID int)
RETURNS nvarchar(20)
AS
BEGIN
--Declare a variable that really shouldn't be used
DECLARE @EmployeeName nvarchar(20)
SET @EmployeeName = 'Not expected'
RETURN COALESCE
(
(SELECT e.LastName FROM dbo.Employees e
WHERE e.EmployeeID = @EmployeeID),
'(Not Found)'
)
--The function SHOULD have exited by now...
--RETURN @EmployeeName <--SECOND RETURN COMMENTED OUT
END
GO
--These now return the expected results
SELECT dbo.Foo(1) --Returns 'Davolio'
SELECT dbo.Foo(564654) --Returns '(Not Found)'
posted by Oskar Austegard at 9:24 AM on Dec 14, 2005
"SQL: Bug in UDFs using COALESCE with SELECT and Multiple RETURNs?"
2 Comments -
My friend Yosef pointed me to this MS KnowledgeBase article: FIX: Coalesce with Subquery May Generate an Access Violation. Guess I have to check what version of SQL 2000 I'm running.
Friday, December 23, 2005 9:49:00 AM
Ok - so first: We WERE running a vanilla version of SQL 2000 (hey - it's a dev machine, OK?).
But: I tried my UDF on a machine running SP3 and got the same result. It's definitely a bug.
Friday, December 23, 2005 10:08:00 AM