if between 10 variable [closed]
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 '%'+@txtSearch+%'
end
if(@a1='false' and @a2='true' and @a3='false' and @a4='false' and @a5='false')
begin
select a2 from b where a2 like '%'+@txtSearch+%'
end
if(@a1='false' and @a2='false' and @a3='true' and @a4='false' and @a5='false')
begin
select a3 from b where a3 like '%'+@txtSearch+%'
end
if(@a1='false' and @a2='false' and @a3='false' and @a4='true' and @a5='false')
begin
select a4 from b where a4 like '%'+@txtSearch+%'
end
if(@a1='false' and @a2='false' and @a3='false' and @a4='false' and @a5='true')
begin
select a5 from b where a5 like '%'+@txtSearch+%'
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 '%'+@txtSearch+%' or a2 like '%'+@txtSearch+%'
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 '%'+@txtSearch+%' or a3 like '%'+@txtSearch+%'
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 '%'+@txtSearch+%' or a4 like '%'+@txtSearch+%'
end
.
开发者_如何学运维 .
.
.
end
I am in search box a1-a5 to the check box That user can choose a1 and a2 and a3 and a4 and a5
If the selected field a1؛ a1 field DB Search
If the selected field a2؛ a2 field DB Search
If the selected field a1,a2؛ a1,a2 field DB Search
If the selected field a2,a5؛ a2,a5 field DB Search
If the selected field a2,a4,a5؛ a2,a4,a5 field DB Search
All states check by if is 5 factorial that very very very large if use a1-a9
There are easier ways to do this.
This is an algorithm that can be done in any language c,c#,vb
You need to use dynamic SQL and generate the sentence. Check out this link. Pay special attention to section Using sp_executesql
. Something like that is almost exactly what you need.
精彩评论