Query optimisation

  • 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

  • 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)

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • 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