Solved

SQL Server scalar function

Posted on 2014-04-28
4
464 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

808 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