all groups > sql server programming > december 2006 >
You're in the

sql server programming

group:

UDF Question


UDF Question Steve Harp
12/28/2006 8:30:48 PM
sql server programming:
Hi Guys,

When I try to create the following UDF I get an error "The last
statement included within a function must be a return statement.".

create function dbo.udfItExists(@that int)
RETURNS bit
AS
Begin
if (Exists(select ID
from my_table
where this_col = @that))
return 1;
else
return 0;
End

Is there a way to make this work?

Thanks,
Re: UDF Question Tom Cooper
12/28/2006 9:18:34 PM
create function dbo.udfItExists(@that int)
RETURNS bit
AS
Begin
declare @result bit;
if (Exists(select ID
from my_table
where this_col = @that))
set @result = 1;
else
set @result = 0;
return @result;
End

Tom

[quoted text, click to view]

Re: UDF Question Gert-Jan Strik
12/29/2006 2:51:17 PM
Yet another solution:

create function dbo.udfItExists(@that int)
RETURNS bit
AS
Begin
if (Exists(select ID
from my_table
where this_col = @that))
return 1;

return 0;
End

Gert-Jan


[quoted text, click to view]
Re: UDF Question Steve Harp
12/29/2006 3:42:28 PM
Thanks to everyone. Looks like there are lots of ways to make it
work (all of which should have been obvious to me).

On Thu, 28 Dec 2006 20:30:48 -0500, Steve Harp <NoSpam@NoPlace.com>
[quoted text, click to view]
Re: UDF Question Hugo Kornelis
12/29/2006 9:12:10 PM
[quoted text, click to view]

Hi Steve,

And a third way:

CREATE FUNCTION dbo.udfItExists(@that int)
RETURNS bit
AS
BEGIN
RETURN CASE WHEN (EXISTS(SELECT ID
FROM my_table
WHERE this_col = @that))
THEN 1
ELSE 0
END;
END;

--
AddThis Social Bookmark Button