存储过程要传入一个参数决定用那个字段排序,但是这个参数可能有几十中,存储过程中怎么实现啊

解决方案 »

  1.   

    搜一下不定参数存储过程还是要在存储过程里判断吧这是一个例子
    create table test
    (a int, 
     b varchar(10),
     c varchar(20),
     d int)insert test
    select 1,'a','b',2 union
    select 3,'c','d',4
    gocreate proc VarProc
    (@b varchar(10)=null,
     @c varchar(20)=null,
     @d int=null)
    as
    declare @sql varchar(100)
    select @sql='select * from test where 1=1 '
    +case when @b is null then '' else 'and b like '''+@b+'''' end
    +case when @c is null then '' else 'and c='''+@c+'''' end
    +case when @d is null then '' else 'and d='+rtrim(@d) end
    print @sql
    exec(@sql)goexec VarProc 'a','b'
    drop table test
    drop proc VarProc