SELECT ZID,zscore_id ,ZVIP_ID,ZMemberName_Chn,zsex,ZRegisterDate2,ZIDCard  from ZSMemberInfo  where   ZVIP_ID != '' 
and 
case when @b is null then 'zscore_id< @c '
                 when @c is null then 'zscore_id> @b'
                 when @b is null and @c is null then '0'
                 ELSE 'beween @b and @c'
end
改成:
exec('SELECT ZID,zscore_id ,ZVIP_ID,ZMemberName_Chn,zsex,ZRegisterDate2,ZIDCard  from ZSMemberInfo  where   ZVIP_ID != '' 
and 
case when '+@b +' ........

解决方案 »

  1.   

    declare @a  nvarchar(255),
    @b  char(255),
    @c  char(255),
    @s nvarchar(8000)set @a='普通会员'
    set @b='001'
    set @c='009'
      
    if @a = '普通会员'
    begin
    set @s='SELECT ZID,zscore_id ,ZVIP_ID,ZMemberName_Chn,zsex,ZRegisterDate2,ZIDCard 
    from ZSMemberInfo  
    where   ZVIP_ID != ''''
    and  '+case when @b is null then 'zscore_id< @c '
                     when @c is null then 'zscore_id> @b'
                     when @b is null and @c is null then '0'
                     ELSE 'beween @b and @c' end
    exec sp_executesql @s
    ,N'@b  char(255),@c char(255)'
    ,@b,@c
    end