Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Function that finds a value in a table

Posted on 2011-09-16
4
Medium Priority
?
281 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:vincem1099
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
indianguru2 earned 2000 total points
ID: 36552176
Try this:

CREATE FUNCTION Function_CheckIfStationCodeExists ( @StationCode INT )
RETURNS BIT
AS
    BEGIN
        IF EXISTS ( SELECT  1
                    FROM    tableName --- Enter you table Name here
                    WHERE   StationCode = @StationCode )
            RETURN CAST(1 AS BIT)
        ELSE
            RETURN CAST(0 AS BIT)
           
    END

GO
0
 
LVL 7

Author Comment

by:vincem1099
ID: 36552227
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.
0
 
LVL 7

Author Comment

by:vincem1099
ID: 36552254
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

0
 
LVL 7

Author Closing Comment

by:vincem1099
ID: 36552315
Thanks for pointing me in the right direction
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question