Link to home
Start Free TrialLog in
Avatar of vincem1099
vincem1099Flag for United States of America

asked on

Function that finds a value in a table

I need a function that will return true in the passed value is in a table.  For example:
StationCode      Site
100      Site1
200      Site2
300      Site3
400      Site4

Passing  a value of 100 would return 1 or true
Passing a value of 150 would return 0 or false
ASKER CERTIFIED SOLUTION
Avatar of indianguru2
indianguru2
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vincem1099

ASKER

I get the following error message when trying to execute the code:
Msg 455, Level 16, State 2, Procedure udf_LookupSite, Line 20
The last statement included within a function must be a return statement.
This modification works though
CREATE FUNCTION dbo.udf_LookupSite
(
	
	@StationCode int
)
RETURNS bit
AS
BEGIN
	DECLARE @RETURNVALUE BIT
	IF EXISTS ( SELECT  1
                FROM    vSites 
                WHERE   StationCode = @StationCode ) 
        set @RETURNVALUE = CAST(1 AS BIT)
    ELSE 
        set @RETURNVALUE = CAST(0 AS BIT)
        
	
	RETURN @RETURNVALUE

END

Open in new window

Thanks for pointing me in the right direction