Solved

Function that finds a value in a table

Posted on 2011-09-16
4
267 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

786 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