我的存储过程有两个参数A 和 B
A是一个int,B是一个string 'a','b','c',将来用作判断 fieldB in (B)有两个问题
1,这个B参数带逗号和单引号 怎么传给存储过程P
   P 1,'''a','b','c''' --这样对吗,以前弄过,忘了,顺便在这里问一下2,这个是我问的主要问题
如何把where条件中的in和case一起使用select ......
where fieldA= case when A is null then fieldA else A end --这是对A参数和字段FieldA的写法,应该没问题
and fieldB in case when,,,,,--针对in的怎么写??

解决方案 »

  1.   

    1.select '''a''','''b''','''c'''/*
                   
    ---- ---- ---- 
    'a'  'b'  'c'(所影响的行数为 1 行)*/
      

  2.   

    2case when charindex(''''++'''',参数B)>0 then  else end
      

  3.   

    第二个的大概意思就是
    我先判断B参数是不是null,如果不是null就进行 fieldB in 判断,否则的话就这个条件就不成立前面的select那堆 我不想写多次次,所以尽量使用case这样的语句搞定
      

  4.   


    您好,我没看明白,您可以写个全一点的吗
    例如where fieldA= case when A is null then fieldA else A end
    这样完整一点的,非常感谢
      

  5.   

    1,这个B参数带逗号和单引号 怎么传给存储过程P 
      P 1,'''a','b','c''' --这样对吗,以前弄过,忘了,顺便在这里问一下 P 1,'''a'',''b'',''c'''
      

  6.   

    select ...... 
    where fieldA= case when A is null then fieldA else A endand fieldB in case when,,,,,--针对in的怎么写??用CHARINDEX
      

  7.   

    ----楼主:看这个是不是你想要的效果:
    drop table a;create table a(id int, score int, names varchar(10))
    insert into a(id, score, names)
    select
    1,44,'a' union all select
    2,68,'b' union all select
    3,77,'c' union all select
    4,95,'d' union all select
    5,77,'e' union all select
    6,77,'f' union all select
    7,95,'g';
    -----------------------------------------------
    drop procedure test_proc;alter procedure test_proc @score int, @names varchar(max)
    as
    /*
    exec test_proc 77,'e,f'
    */
    begin
    declare @sql varchar(max)
    set @names=''''+replace(@names,',',''',''')+''''
    print (@names)
    set @sql='select id, score, names from a where 1=1 '
    if(isnull(@score,0)<>0) --
    set @sql=@sql+' and score='+convert(varchar(12),@score)+''
    if(isnull(@names,'')<>'')
    set @sql=@sql+' and names in('+@names+')'
    print (@sql)
    exec (@sql)
    end
      

  8.   

    ----楼主:看这个是不是你想要的效果:
    drop table a;create table a(id int, score int, names varchar(10))
    insert into a(id, score, names)
    select
    1,44,'a' union all select
    2,68,'b' union all select
    3,77,'c' union all select
    4,95,'d' union all select
    5,77,'e' union all select
    6,77,'f' union all select
    7,95,'g';
    -----------------------------------------------
    drop procedure test_proc;alter procedure test_proc @score int, @names varchar(max)
    as
    /*
    exec test_proc 77,'e,f'
    */
    begin
    declare @sql varchar(max)
    set @names=''''+replace(@names,',',''',''')+''''
    print (@names)
    set @sql='select id, score, names from a where 1=1 '
    if(isnull(@score,0)<>0) --
    set @sql=@sql+' and score='+convert(varchar(12),@score)+''
    if(isnull(@names,'')<>'')
    set @sql=@sql+' and names in('+@names+')'
    print (@sql)
    exec (@sql)
    end
      

  9.   

    to 楼上
    没错,我现在就是使用这种动态拼接sql的方式了看来也只能使用这种方式了上面有朋友提出使用CHARINDEX,感觉应该可以,我再研究一下看看,谢谢各位了