Solved

Function that finds a value in a table

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now