Solved

Function that finds a value in a table

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Procedure 7 48
SQLCMD question to have fields created as fixed length 2 35
Report 8 24
Restore a log backup compressed 3 14
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

737 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