Solved

SQL Server scalar function

Posted on 2014-04-28
4
457 Views
Last Modified: 2014-04-29
Hi,

I want a scalar function in SQL Server that checks if ID exists in the table, if it does then find if userid for that ID exists. If both are true then return 1 else return 0.

Here's my code:
CREATE FUNCTION dbo.ufnCheckIDAndUSERID(@ID varchar)
RETURNS bit
AS
BEGIN
--first check if the ID is there in SQL table:
IF ((SELECT count(ID) from [mydb].[dbo].[t] where ID = @ID) = 1)
AND
((SELECT count(UserID) from [mydb].[dbo].[t] where ID = @ID)=1)
RETURN 1
ELSE
RETURN 0
END;

Open in new window


I m getting
Msg 156, Level 15, State 1, Procedure ufnCheckDistrictAndntid, Line 7
Incorrect syntax near the keyword 'IF'.

I m a newbie using SQL function. ANy clue to resolve it? Thanks.
0
Comment
Question by:dearnemo
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
Comment Utility
You can only have one return statement in a function and it must be last.

create function dbo.ufnCheckIDAndUSERID
(
  @Id varchar(38)
)
returns bit
as
begin
  declare @Result bit

  --first check if the ID is there in SQL table:
  if ((select count(ID) from mydb.dbo.t where ID = @Id) = 1) AND ((select count(UserID) from mydb.dbo.t where ID = @Id) = 1) set @Result = 1

  return @Result
end

Open in new window

0
 

Author Closing Comment

by:dearnemo
Comment Utility
Ty very much for your prompt response. :)
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
No worries, actually just realised if you use my code you need to add a line after declare...

set @Result = 0
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Is the field [t].[ID] bigint or varchar?

if it is a big integer then your parameter should be bigint

why scan the table twice?

    CREATE TABLE T
    	([ID] int, [UserID] varchar(5))
    ;
    	
    INSERT INTO T
    	([ID], [UserID])
    VALUES
    	(101, 'Fred'),
    	(102, 'Wilma')
    ;

**Query 1**:

    declare @Id varchar(38)
    
    set @Id = '101'
    
    
    SELECT count(ID) from [dbo].[t] where ID = @ID 
    union all
    SELECT count(UserID) from [dbo].[t] where ID = @ID 
    
    

**[Results][2]**:
    
    | COLUMN_0 |
    |----------|
    |        1 |
    |        1 |


**Query 2**:

    declare @Id varchar(38)
    
    set @Id = '101'
    
    SELECT
           count(id) + count(UserID)
    from [dbo].[t]
    where id = @id
    

**[Results][3]**:
    
    | COLUMN_0 |
    |----------|
    |        2 |



  [1]: http://sqlfiddle.com/#!3/05979/13

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

11 Experts available now in Live!

Get 1:1 Help Now