我的开发平台是VS2008 + SQL Server 2005
现在我的页面要做一个产品查询功能
页面上有txt_ProductName   --产品名称
        txt_Classify      --产品分类
        txt_minPrice      --价格范围
        txt_maxPrice      --价格范围
现在想写一个存储过程来实现查询,数据库中有表t_Products
条件是:当以上4个参数有任意几个传入时,都执行相应的查询对于这种参数个数不固定的查询,该怎么写?求指教,谢谢

解决方案 »

  1.   

    不固定你想如何查询?
    最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
    如果有多表,表之间如何关联?
    发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  2.   


    create proc test
    @name     varchar(100),
    @Classify  varchar(100),
    @minPrice  float,
    @minPrice  float
    as
    begin
         declare @strsql varchar(2000),
                 @strWhere varchar(1000)
         set @strWhere  = ' 1 = 1'
         if @name is not null
         begin
           set @strWhere  = @strWhere  + ' and txt_ProductName ='''+ @name  +''''
         end
         if @Classify is not  null
         begin
            set @strWhere  = @strWhere  + ' and txt_Classify  ='''+ @Classify   +''''
         end
         if @minPrice is not  null
         begin
            set @strWhere  = @strWhere  + ' and txt_minPrice>='+ convert(varchar(30),minPrice )+' '
         end
         if @maxPrice is not  null
         begin
            set @strWhere  = @strWhere  + ' and txt_maxPrice <='+ convert(varchar(30),maxPrice )+' '
         end
         set @strsql  = 'select * from [表] where ' + @strWhere
         print @strsql
         exec (@strsql)end
      

  3.   


    我查询的条件就是,只有选择分类没有其他条件的时候,查询分类
                      只有产品名称的时候,模糊查询产品名称
                      有选择分类和产品名称的时候,查询符合产品分类和like%产品名称%的产品
                    有选择分类和价格范围的时候,查出符合分类和价格范围的产品
                      
    用一个存储过程可以实现吗?
                    
      

  4.   

    4个的话也不多嘛 排列组合也就16个 写16个语句 嘿嘿 开个玩笑
    其实方法很多,我比较喜欢一个方法是定义个表变量或者临时表
    一个个过滤
    把不符合的要求剔除(delete),剩下的就是符合要求的,再用一句语句输出
    还有一个方法就是拼接sql语句,然后用exec调用
      

  5.   

    假设我在存储过程中定义4个变量@ProductName,@Classify,@minPrice,@maxPrice
    能否用判断其是否有值来拼接sql?以前写在后台代码中的
                string sql = "select * from t_Products where 1=1";
                if (ddl_Classify.SelectedIndex != -1)
                {
                    sql += " and Classify='" + ddl_Classify.SelectedItem.Value.ToString().Trim() + "'";
                }
                if (txt_ProductName.Text != "")
                {
                    sql += " and ProductName like '%" + txt_ProductName.Text.ToString().Trim() + "%'";
                }
                if (txt_minPrice.Text != "" && txt_maxPrice.Text != "")
                {
                    sql += " and Price>='" + Convert.ToDecimal(txt_minPrice.Text.ToString()) + "' and Price<='" + Convert.ToDecimal(txt_maxPrice.Text.ToString().Trim()) + "'";
                }
    像这样的能写到存储过程中去么?
      

  6.   

    create proc test
    @name     varchar(100) = null,
    @Classify  varchar(100) = null,
    @minPrice  float = null,
    @minPrice  float = null
    as
    begin
         declare @strsql varchar(2000),
                 @strWhere varchar(1000)
         set @strWhere  = ' 1 = 1'
         if @name is not null
         begin
           set @strWhere  = @strWhere  + ' and txt_ProductName ='''+ @name  +''''
         end
         if @Classify is not  null
         begin
            set @strWhere  = @strWhere  + ' and txt_Classify  ='''+ @Classify   +''''
         end
         if @minPrice is not  null
         begin
            set @strWhere  = @strWhere  + ' and txt_minPrice>='+ convert(varchar(30),minPrice )+' '
         end
         if @maxPrice is not  null
         begin
            set @strWhere  = @strWhere  + ' and txt_maxPrice <='+ convert(varchar(30),maxPrice )+' '
         end
         set @strsql  = 'select * from [表] where ' + @strWhere
         print @strsql
         exec (@strsql)end