Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server scalar function

Posted on 2014-04-28
4
Medium Priority
?
469 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 the fundamental information of how to create a table.

618 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