string sql="select * from table where 1=1 " +条件1+条件2 ...

解决方案 »

  1.   

    string sql="select * from table where 1=1 "
    if 选择年龄 sql=sql+" and 年龄=24 "
    if 选择身高 sql=sql+" and 身高=216 "
    if 选择性别 sql=sql+" and 性别='男' "
      

  2.   

    一个表,如:年龄,身高,性别,住处等信息.
    string sql="select * from table where "
    if 年龄不為空 then
        sql=sql & "年龄= " & values1
    end if
    if 身高不為空 then
        sql=sql & "身高= " & values2
    end ifif 性别不為空 then
        sql=sql & "性别= " & values3
    end if
    if 住处不為空 then
        sql=sql & "住处= " & values4
    end if
    。。
      

  3.   

    一个表,如:年龄,性别等信息.
    String sql="select * from table ";
    String subsql="";
    if(!Age.equals("")&&Age!=null){
        subsql+="age="+Age;
    }
    if(!Sex.equals("")&&Sex!=null){
        subsql+="sex="+Sex;
    }
    ......
    if(!subsql.equals("")){
      sql+="where "+subsql;
    }
    execute you sql
      

  4.   

    string sql="select * from table where 1=1"
    if 年龄不為空 then  sql=sql & "年龄= " & values1
    if 身高不為空 then  sql=sql & "身高= " & values2
    if 性别不為空 then  sql=sql & "性别= " & values3
    if 住处不為空 then  sql=sql & "住处= " & values4
      

  5.   

    string sql="select * from table where 1=1"
    if 年龄不為空 then
        sql=sql & " and 年龄= " & values1
    end if
    if 身高不為空 then
        sql=sql & " and 身高= " & values2
    end ifif 性别不為空 then
        sql=sql & " and 性别= " & values3
    end if
    if 住处不為空 then
        sql=sql & " and 住处= " & values4
    end if
      

  6.   

    --用动态SQL语句--示例存储过程
    create proc p_qry
    @年龄 int=null,
    @身高 int=null,
    @性别 varchar(10)=null,
    @住处 varchar(100)=null
    as
    declare @s nvarchar(4000)
    select @s
    =case when @年龄 is null then ''
    else ' or 年龄=@年龄' end
    +case when @身高 is null then ''
    else ' or 身高=@身高' end
    +case when @性别 is null then ''
    else ' or 性别=@性别' end
    +case when @住处 is null then ''
    else ' or 住处=@住处' end
    ,@s='select * from 表'
    +case @年龄 when '' then ''
    else ' where '+stuff(@s,1,4,'') end
    exec sp_executesql @s
    ,N'@年龄 int,
    @身高 int,
    @性别 varchar(10),
    @住处 varchar(100)'
    ,@年龄,@身高,@性别,@住处
    go--调用示例
    exec p_qry @身高=100,@住处='aa'
      

  7.   

    sp_executesql执行可以多次重用或动态生成的 Transact-SQL 语句或批处理。
    Transact-SQL 语句或批处理可以包含嵌入参数。每个条件做一个参数,zjcxc的最好