I just saw this function definition:
create function dbo.f (@a int, @b int)
returns integer
as
begin
return case when
not exists (Select * from t1 where t1.col1 = @a)
AND @b > 0
then 1 else 0 end
end
GO
Seeing a not exists I thought attention full table scan and tried to improve it
create function dbo.f (@a int, @b int)
returns integer
as
begin
return case when
exists (Select * from t1 where t1.col1 = @a)
OR @b > 0
then 0 else 1 end
end
GO
My feeling is, that this transformation could have been done by an optimizer. It seems to be straight forward, but how can I be sure if he does?
Comment on Igor's answer: (comparison fixed thanks to Matts comment)
This inspires me to the following:
create function dbo.f (@a int, @b int)
returns integer
as
begin
IF @b <= 0
RETURN 0
IF exists (Select * from t1 where t1.col1 = @a)
RETURN 0
RETURN 1
end
GO