?
Solved

SQL Server scalar function

Posted on 2014-04-28
4
Medium Priority
?
468 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
[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
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 2000 total points
ID: 40028595
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
ID: 40028603
Ty very much for your prompt response. :)
0
 
LVL 21

Expert Comment

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

set @Result = 0
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40029109
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

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…
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
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

777 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