用存储过程写:
条件:根据3个条件查询一张表ListGoods的信息: size、type、load。类型全部为varchar(50)
实现:3个查询页面。第一页:3个查询条件。第二页2个查询条件,第三页1个查询条件,要实现条件模糊查询。
如果没选中条件。就是变量为NULL时,则查询全部信息。要有足够的逻辑判断。
可以的话就直接调用一个存储过程。不行的话就写3个存储过程吧。貌似一个行不通。好像条件一定要用and连。or的话逻辑行不通。不用考虑我的想法。只要能实现我要的结果。一定判断每一个变量为NULL与NOT NULL。
急死人了。请高人解救。

解决方案 »

  1.   


    select * from ListGoods
     where size like '%'+isnull(@size, '')+'%' 
     and type like  '%'+isnull(@type, '')+'%'
     and load like  '%'+isnull(@load, '')+'%';declare @sql varchar(8000);
    set @sql='select * from ListGoods where 1=1 '+
    case when @size is null then '' else 'and size like %'+@size+'%' end+
    case when @type is null then '' else 'and size like %'+@type+'%' end+
    case when @load is null then '' else 'and size like %'+@load+'%' end;
    exec(@sql);
      

  2.   


    -- 动态的改一下
    declare @sql varchar(8000);
    set @sql='select * from ListGoods where 1=1 '+
    case when @size is null then '' else 'and size like ''%'+@size+'%''' end+
    case when @type is null then '' else 'and size like ''%'+@type+'%''' end+
    case when @load is null then '' else 'and size like ''%'+@load+'%''' end;
    exec(@sql)
      

  3.   


    第一个就错了。按照你的用了下。。首先什么都不选,就默认查询全部,但是用了and后 好像我的19条数据只能查出9条了 没有查出全部的来,只有用or的时候才把全部查了出来。但是用了or后好像后面的条件又成立不了?怪哉!
      

  4.   

    这个只是简要的演示一下 SQL 方法,要精确的语句,需要提供数据和说明。
      

  5.   


    create proc sp_wsp
    @size varchar(50),
    @type varchar(50),
    @load varchar(50)
    as
    declare @where varchar(1000)
    set @where=case isnull(@size,'') when '' then '' else ' and size='''+@size+'''' end
    set @where=@where+case isnull(@type,'') when '' then '' else ' and type='''+@type+'''' end
    set @where=@where+case isnull(@load,'') when '' then '' else ' and load='''+@load+'''' end
    exec('select * from ListGoods where 1=1 '+@where)
    go
      

  6.   

    ListGoods产品表:
    id     主键,自增长,标识1,1
    name   产品名
    size   尺寸
    type   类型
    load   负重
    BigID产品大系列Id,主外键
    SmallID小系列ID,主外键
    查询页面1:根据尺寸、类型、负重3个单选按钮进行选择,不选直接点查询按钮则忽视条件,查询全部
    如果选中一项就模糊查询那一项,其他2个条件就相当于不存在
    如果选中2项,就根据2项查询。另外一个条件忽视
    差不多就是这样其他几个页面都是类似的。
      

  7.   


    -- size, type, load 列的数据类型都是 varchar(50) ?
    -- 如果是数值类型,则需要将类型转换,like 更改为 = 。
    create procedure usp_get_listgoods
    @size varchar(50)=null, @type varchar(50)=null, @load varchar(50)=null
    as
    declare @sql varchar(8000);set @sql='select * from ListGoods where 1=1 '+
    case when @size is null then '' else 'and size like ''%'+@size+'%''' end+
    case when @type is null then '' else 'and size like ''%'+@type+'%''' end+
    case when @load is null then '' else 'and size like ''%'+@load+'%''' end;exec(@sql)
    go
      

  8.   

    你确定下面3个都是size like?不是对应的字段 like?是varchar(50)类型。
      

  9.   

    复制错了。改一下吧。create procedure usp_get_listgoods
    @size varchar(50)=null, @type varchar(50)=null, @load varchar(50)=null
    as
    declare @sql varchar(8000);set @sql='select * from ListGoods where 1=1 '+
    case when @size is null then '' else 'and size like ''%'+@size+'%''' end+
    case when @type is null then '' else 'and type like ''%'+@type+'%''' end+
    case when @load is null then '' else 'and load like ''%'+@load+'%''' end;exec(@sql)
    go
      

  10.   

    为什么要改为?啊你是要ole调用吗
      

  11.   

    cmd.CommandText = "usp_get_listgoods";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("")...
      

  12.   


    create proc sp_wsp
    @size varchar(50),
    @type varchar(50),
    @load varchar(50)
    as
        declare @where varchar(1000)
        set @where=case isnull(@size,'') when '' then '' else ' and size like ''%'+@size+'%''' end
        set @where=@where+case isnull(@type,'') when '' then '' else ' and type like ''%'+@type+'%''' end
        set @where=@where+case isnull(@load,'') when '' then '' else ' and load  like ''%'+@load+'%''' end
        exec('select * from ListGoods where 1=1 '+@where)
    go
      

  13.   

    这样应该行,我经常用
    create proc sp_query_ListGoods
    @size varchar(50),
    @type varchar(50),
    @load varchar(50)
    as
        select * from ListGoods 
        where( @size = '' or @size is null or [size] like "%"+@size+"%"  )
        and ( @type = '' or @type is null or [type] like "%"+@type+"%"  )
        and ( @load = '' or @load is null or [load] like "%"+@load+"%"  )
    go
      

  14.   

    CREATE PROC SP_QUERY_LISTGOODS
    @SIZE VARCHAR(50)=NULL,
    @TYPE VARCHAR(50)=NULL,
    @LOAD VARCHAR(50)=NULL
    AS
        SELECT * 
        FROM LISTGOODS 
        WHERE( ISNULL(@SIZE,'') = '' OR [SIZE] LIKE '%'+@SIZE+'%' )
    AND ( ISNULL(@TYPE,'') = ''  OR [TYPE] LIKE '%'+@TYPE+'%'  )
    AND ( ISNULL(@LOAD,'') = '' OR [LOAD] LIKE '%'+@LOAD+'%'  )
    GO