Solved

Function that finds a value in a table

Posted on 2011-09-16
4
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
indianguru2 earned 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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