库有个字段ispositive
如果-1表示这个类型得记录是负得
如果0表示可能正可能负
如果1表示肯定正得如果它要找ispositive=0或者1得,我where后面这么写
select * from table where ispositive=0 or 1
它要找ispositive=0
select * from table where ispositive=0
现在问题是我想把这句放在存储过程里面
那该怎么写呢,她可能是ispositive=0 or 1也可能是ispositive=0 不大想用动态查询语句得情况下该怎么做呢?而且,传入得参数最好也只是一个
比如我要搜索全部得话,如果传三个参数0,1,-1进去岂不是太丑陋了。

解决方案 »

  1.   

    create proc proc_name
    @type int
    as
    begin
       
       if (@type = 0 ) 
          ..
       else 
          if ()
          ...
    end
      

  2.   

    create proc proc_tt (@id int default null)
    as
    beginselect * from tab 
    where ispositive=isnull(@id,Ispositive)end
      

  3.   

    在存储过程里面写IF判断,参数为string.固定为以下几种
    '0'
    '1'
    '-1'
    '-1,0'
    '-1,1'
    '0,1'
    '-1,0,1'假设参数为@strif @str = '0'
       select * from from table where ispositive = 0
    else
       if @str = '1'
          select * from from table where ispositive = 1
       else
          if @str = '-1'
             select * from from table where ispositive = -1
          else
             if @str = '-1,0'
                select * from from table where ispositive = -1 or ispositive = 0
    ....自己写完.
             
      

  4.   

    create proc proc_name 
    @type int 
    as 
    begin 
        
       if (@type = 0 )   -- 0
          select * from table where ispositive=0 
       else  
          if ((@type = 1)  -- 1 or 0
              select * from table where ispositive >=0 
          else
              select * from table  --all
      
    end
      

  5.   

    create proc proc_tt (@id int default null)
    as
    beginselect * from tab 
    where ispositive=isnull(@id,Ispositive)end
      

  6.   

    不用存储过程charindex查找就行,@变量为' 1 -1'或者' 0 -1'或者' 0 1'或者' 1 -1 0'或者' 1'等等
    select * from table where charindex(' '+ispositive,@变量)>0
      

  7.   


    create procedure test_Tispositive
    (
          @ispositive varchar(10) -- @ispositive为'0,1,-1' 
    )
    as
    begin
         select * from table 
         where charindex(','+cast(ispositive as varchar(2))+',',','+@ispositive+',') > 0
    end
    go