2011-07-17 29 views
-5

之間如果10個變量

ALTER procedure FullSearch(@txtSearch as nvarchar(500),@a1 as bit,@a2 as bit,@a3 as bit, 
@a4 as bit,@a5 as bit) 
as 
begin 
    if(@a1='true' and @a2='false' and @a3='false' and @a4='false' and @a5='false') 
    begin 
     select a1 from b where a1 like '%'[email protected]+%' 
    end 
    if(@a1='false' and @a2='true' and @a3='false' and @a4='false' and @a5='false') 
    begin 
     select a2 from b where a2 like '%'[email protected]+%' 
    end 
    if(@a1='false' and @a2='false' and @a3='true' and @a4='false' and @a5='false') 
    begin 
     select a3 from b where a3 like '%'[email protected]+%' 
    end 
    if(@a1='false' and @a2='false' and @a3='false' and @a4='true' and @a5='false') 
    begin 
     select a4 from b where a4 like '%'[email protected]+%' 
    end 
    if(@a1='false' and @a2='false' and @a3='false' and @a4='false' and @a5='true') 
    begin 
     select a5 from b where a5 like '%'[email protected]+%' 
    end 
    if(@a1='true' and @a2='true' and @a3='false' and @a4='false' and @a5='true') 
    begin 
     select a1,a2 from b where a1 like '%'[email protected]+%' or a2 like '%'[email protected]+%' 
    end 
    if(@a1='true' and @a2='false' and @a3='true' and @a4='false' and @a5='true') 
    begin 
     select a1,a3 from b where a1 like '%'[email protected]+%' or a3 like '%'[email protected]+%' 
    end 
    if(@a1='true' and @a2='false' and @a3='false' and @a4='true' and @a5='true') 
    begin 
     select a1,a4 from b where a1 like '%'[email protected]+%' or a4 like '%'[email protected]+%' 
    end 
    . 
    . 
    . 
    . 
end 
我在搜索框中A1-A5的複選框,用戶可以選擇A1和A2和A3和A4和A5

如果所選字段A1,A1領域數據庫搜索

如果所選擇的場A2; A2字段DB搜索

如果所選擇的場A1,A2; A1,A2字段DB搜索

如果所選擇的場A2,A5; A2,A5字段DB搜索

如果所選字段A2,A4,A5,A2,A4,A5領域數據庫的搜索,如果是5階乘是非常非常大的,如果使用A1-A9

所有州檢查由更簡單的方法來做到這一點

這是一個可以在任何C語言,C#來完成,VB

+2

這與C#有什麼關係?這裏有什麼問題? –

+0

不是。顯然是TSQL。 –

回答

2

你需要使用動態SQL,併產生句子的算法。檢查出this鏈接。請特別注意使用sp_executesql。類似的東西幾乎就是你需要的東西。

+0

請詳細說明這個 – ashkufaraz

+0

請給我寫樣品 – ashkufaraz

+0

閱讀我發佈的鏈接。你需要的只是在那裏。 –