正常运行时只有要判断@pixes @pType两个参数,现在想再加一个参数@sort;
要是再用if...else if... else if ... else 那还要写多长啊?
救命啊..............

解决方案 »

  1.   

    你用ISNILL.
    select count(pid) from m_pics 
    WHERE pPub=1 
    AND pType=isnull(@pType,pType) 
    AND pPixes=isnull(@pixes,pPixes)
    AND ........你再加!
      

  2.   

    使用case when 會簡單點
    简单 CASE 函数:CASE input_expression
        WHEN when_expression THEN result_expression
            [ ...n ]
        [ 
            ELSE else_result_expression
        ENDUSE pubs
    GO
    SELECT   Category = 
          CASE type
             WHEN 'popular_comp' THEN 'Popular Computing'
             WHEN 'mod_cook' THEN 'Modern Cooking'
             WHEN 'business' THEN 'Business'
             WHEN 'psychology' THEN 'Psychology'
             WHEN 'trad_cook' THEN 'Traditional Cooking'
             ELSE 'Not yet categorized'
          END,
       CAST(title AS varchar(25)) AS 'Shortened Title',
       price AS Price
    FROM titles
    WHERE price IS NOT NULL
    ORDER BY type, price
    COMPUTE AVG(price) BY type
    GO
      

  3.   

    tks: j9988(j9988) 
    是不是这个意思?
    sql语句中的ISNull(@var,yourRows)会自动判断参数情况,没有的话系统会不报错?
    呵呵,先测试一下啊..........
      

  4.   

    to:  progress99(如履薄冰)
    case语句对应于同一条件的不同值,而我的应该是不同条件下的吧;好象不好用哦;
    also tks (;-
      

  5.   

    ISNull(@var,yourRows) 你存储过程的参数是固定不变的吧.默认为NULL
    如果没有参数进去,它的值就是NULL
    where yourRows=ISNull(@var,yourRows)
    --当@VAR IS NULL 相当于 where yourRows=yourRows 也就是1=1 条件永远成立.
    --当@VAR is not NULL 相当于 where yourRows=@VAR 
      

  6.   

    十分谢谢:j9988(j9988) 
    按照你的提示,我懂了点isull(@var,row)函数,以前的if..else..也优化了;
    可现在还有点问题:
    比如,我默认按id排序,怎样通过客户的请求在存储过程中改变排序方式呢?
    这个问题应该简单吧,我做数据库开发很少,还是不会啊,怎么解决?这就是我存储过程中的@sort变量了。
    还有就是isnull()函数对于,数字、时间、字符的操作都一样吗,要不要转换?(这次学就学完全点,(-0: )
    像普通sql语句中的:
    ----------------------
    数字 id=myVarInt
    字符 temprow='+rowchars+'
    ---------------------------
    有没有这个规则呢...........
    insert into #pageindex(nid,sizeid) 
    select pid,pPixes from m_pics 
    WHERE pPub=1
    AND pPixes=isnull(@pixes,pPixes)
    AND pType=isnull(@pType,pType)
    ----------????????????????
    --@sort
    ----------????????????????????......
      

  7.   

    可以这样改一下你的判断条件 SET @countwhere='pPub=1'+
    (case when @pixes is null then '' else ' AND pPixes='+@pixes end )+
    (case when @pType is null then '' else ' AND pPType='+@pType end )+
    (case when @sort is null then '' else ' AND psort='+@sort end )
      

  8.   

    --其实你用动态SQL语句就比较方便IF EXISTS(Select name From sysobjects where name = 'joyes_temp' )
    DROP PROCEDURE joyes_temp
    GOCREATE procedure joyes_temp
    @pixes Int=null,
    @pType Int=null,
    @sort varchar(50),
    @pagesize int,
    @pageindex int,
    @docount bit
    as
    set nocount on
    declare @tj nvarchar(1000),@s nvarchar(4000)
    set @tj=' where pPub=1'
    +case when @pixes is null then ''
    else ' and pPixes=@pixes' end
    +case when @pType is null then ''
    else ' and pType=@pType' end
    --如果还有其他字段,照这样的规律加上去就行了
    if(@docount=1)
    begin
    set @s='SELECT count(pid) FROM [m_pics]'+@tj
    exec sp_executesql @s
    ,N'@pixes Int,@pType'
    ,@pixes,@pType
    end
    else
    begin
    declare @PageLowerBound int
    declare @PageUpperBound int
    set @PageLowerBound=(@pageindex-1)*@pagesize
    set @PageUpperBound=@PageLowerBound+@pagesize create table #pageindex(id int identity(1,1) not null,nid int,sizeid int) set @s='set rowcount @PageUpperBound
    insert into #pageindex(nid,sizeid) select pid,pPixes from m_pics 
    '+@tj+' order by '+@sort

    exec sp_executesql @s
    ,N'@pixes Int,@pType,@PageUpperBound int'
    ,@pixes,@pType,@PageUpperBound select O.pid,O.pTitle,O.pPath,O.pSize,m.name
    from [m_pics] O,#pageindex p,[member] m
    where O.pid=p.nid and p.id>@PageLowerBound and p.id<=@PageUpperBound 
    AND m.id=O.Uploader order by p.id
    end
    set nocount off
    GO
      

  9.   

    1.isnull()函数对于,数字、时间、字符的操作都一样吗!
     WHERE 列名=ISNULL(变量,列名)
    对于NULL值一样.2.我默认按id排序,怎样通过客户的请求在存储过程中改变排序方式呢?
    动态SQL.由客户端传入ORDER BY字符串.
      

  10.   

    --SQL;接帖啦~~~~~~~~
    /*--------------------------十分感谢各位的帮助,虽然还有些其他问题:
    就像动态sql语句中用 in (id1,id2,id3)语句等等.........
    以后再请各位帮忙叻 现在得赶紧补C#,有兴趣可以跟我一起交流哦~~
    ------------------------*/(_+_):(_+_)  哈哈
      

  11.   

    我没有仔细看你的代码,我想了一下,你的问题也就是两种情况。一是参数总个数定,只是有时候多传了几个参数而也,这种情况下,可以使用存储过程的缺省参数形式。第二种就是参数的个数都不定,这种情况下就只能使用动态执行SQL语句了,就是调用EXEC函数。
      

  12.   

    使用缺省参数或者动态SQL语句都可以。在使用动态SQL语句时,需要注意的一点就是在动态SQL语句中不能创建临时表,因为在动态SQL语句中创建的临时表在存储过程中是无法执行的。
      

  13.   

    to: 123456xjy
    "动态SQL语句中创建的临时表在存储过程中是无法执行的"
    谢谢 记着了