My query uses 'exists' function as filter and it has scalar valued function within it. The scalar valued funciton contains cte and "(select top 1 1)". When I use exists it does not filter at all. Rather when I use "where 1=(svf)" it seems work.Did I miss anything or anything wrong in exists query?
SELECT * FROM TBL1
WHERE EXISTS (SELECT SVF(1,2))
--where SVF is my scalar valued from which returns bit and looks like as shown below.
CREATE FUNCTION SVF
(
@x int,
@y int
)
RETURNS bit
AS
BEGIN
declare @result bit
;WITH T1
AS (
SELECT * from tbl2
)
SELECT @result= (select top 1 1
FROM t1
)
return isnull(@result,0)
END
GO
--the following query works
SELECT * FROM TBL1
WHERE 1=(SELECT SVF(1,2))
The EXISTS() operator
(emphasis added)
That's important because your Scalar function is going to return a value, either 1 or 0 (as Damien pointed out even NULL will satisfy) but IT IS going to return a value.
And because it returns a value, the
EXISTSis always going to return TRUE.You are basically asking SQL Server:
Try to re-write your query to not use the function as a Scalar function is not normally Set Based and so is normally a performance killer.