Solved

SQL Server scalar function

Posted on 2014-04-28
4
466 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 500 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 48

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

749 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