March 4, 2013 at 3:39 am
Hi
Can anyone help improving a query i have
I have a table with a list of phone numbers
Table1
Telephone
01212234567
01212234568
01212234569
01212234544
01212234517
01212234527
01212234537
01212234547
01212234557
Iam given 5 phone numbers and i need to check if each one exists in Table1
At the moment i am doing 5 individual querys
set @existsTel1 = 0
select @existsTel1 = 1 from Table1 where Telephone=@telephone1
I would like to do this in one query, can anybody help at all
thanks
Simon
March 4, 2013 at 3:46 am
You didn't specified how exactly these "5 phone numbers" are given to you and didn't specified how you want to return the result. Based on you description looks like you have five input parameters and you want to return five output parameters...
DECLARE @existsTel1 INT = 0, @existsTel2 INT = 0, @existsTel3 INT = 0,
@existsTel4 INT = 0, @existsTel5 INT = 0
SELECT @existsTel1 = case when Telephone = @telephone1 then 1 else @existsTel1 end
,@existsTel2 = case when Telephone = @telephone2 then 1 else @existsTel2 end
,@existsTel3 = case when Telephone = @telephone3 then 1 else @existsTel3 end
,@existsTel4 = case when Telephone = @telephone4 then 1 else @existsTel4 end
,@existsTel5 = case when Telephone = @telephone5 then 1 else @existsTel5 end
FROM Table1
WHERE Telephone IN (@telephone1,@telephone2,@telephone3,@telephone4,@telephone5)
March 4, 2013 at 3:54 am
that helps
thanks
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy