-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

我从Customer Table获取所有数据。

我需要从PRODUCT_TABLE获取以下代码的数据,

(Filter = 3 AND Customer_Adress LIKE '%' + @Search_Text + '%')
from PRODUCT_TABLE

我怎样才能在 Sql 女士的“哪里”中实现这一点?

任何帮助将不胜感激。

谢谢。

4

2 回答 2

1
ALTER PROCEDURE [dbo].[SP_Something]
(
    @Search_Text VARCHAR(4000),
    @FILTER INT
)
AS
BEGIN  
    IF Filter IN (1,2)
    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 + '%')

    IF Filter = 3
    SELECT  YourField
    FROM    ProductTable
    WHERE
        (Filter = 3 AND YourSearchField LIKE '%' + @Search_Text + '%')

END
于 2013-11-08T09:54:34.527 回答
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
    LEFT OUTER JOIN  PRODUCT_TABLE P
            ON P.Something_Id = KS.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 + '%')
        OR
        (P.Filter = 3 AND P.Customer_Adress LIKE '%' + @Search_Text + '%')

END

我认为您在这里缺少的另一件事是,在您的 Where 子句中,您没有提到有关哪个过滤器来自哪个表的任何内容,在列名之前使用表别名,就像我拥有的​​那样,它使代码更易于阅读,更易于维护和解析列模棱两可。

于 2013-11-08T09:52:53.720 回答