2

I have been trying to use stored procedure for searching data.

ALTER PROCEDURE [dbo].[SP_Something]
(
@Search_Text VARCHAR(4000),
@FILTER INT
)
AS
BEGIN  
SELECT  Customer_Information

FROM Customer
LEFT OUTER JOIN Something K ON  K.Something _Id= Something_Id2

LEFT OUTER JOIN  Something3 KS ON KS.Something_Id =Something_Id3

WHERE

// If @FILTER=1 i want to search below

 Customer_İnformation LIKE '%' + @Search_Text + '%'

// If @FILTER=2 i want to search below

Customer_name LIKE '%' + @Search_Text + '%'

// If @FILTER=3 i want to search below

Customer_Adress LIKE '%' + @Search_Text + '%'

How can i use Where with If conditions ?

Any help will be appreciated

Thank you.

1
  • 1
    use case when then ... else logic Commented Nov 8, 2013 at 8:34

2 Answers 2

2
ALTER PROCEDURE [dbo].[SP_Something]
(
    @Search_Text VARCHAR(4000),
    @FILTER INT
)
AS
BEGIN  
    SELECT  Customer_Information
    FROM    Customer
    LEFT OUTER JOIN Something K 
            ON  K.Something _Id= Something_Id2
    LEFT OUTER JOIN  Something3 KS 
            ON KS.Something_Id =Something_Id3
    WHERE
        (Filter = 1 AND Customer_İnformation LIKE '%' + @Search_Text + '%')
        OR
        (Filter = 2 AND Customer_name LIKE '%' + @Search_Text + '%')
        OR
        (Filter = 3 AND Customer_Adress LIKE '%' + @Search_Text + '%')
END
Sign up to request clarification or add additional context in comments.

Comments

0
ALTER PROCEDURE [dbo].[SP_Something]
(
    @Search_Text VARCHAR(4000),
    @FILTER INT
)
AS
BEGIN  
  SELECT  Customer_Information
  FROM Customer
    LEFT OUTER JOIN Something K ON  K.Something _Id= Something_Id2
    LEFT OUTER JOIN  Something3 KS ON KS.Something_Id =Something_Id3
  WHERE
  CASE @FILTER 
     WHEN 1 THEN Customer_İnformation
     WHEN 2 THEN Customer_Name
     WHEN 3 THEN Customer_Address
  END LIKE '%' + @Search_Text + '%';
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.