如果班级字段class为0,那么读取所有班级学生的数据
这个是查询条件嘛?  where class=0  ??

解决方案 »

  1.   

    用动态:
    declare @s nvarchar(200)
    set @s =case when class =0 then 1,2,3 else class endexec('select * from 表 where class in ('+@s+')')
    用判断:
    declare @class int
    if @class=0
    select * from 表 where class in(1,2,3)
    else
    exec('select * from 表 where class='+@class+')')
      

  2.   

    我理解楼主的意思好像是有一个学生数据表student,其中有个字段名叫class,class的字段值是1、2、3,现有一控制参数,当此参数为0,那么读取所有班级学生的数据,如果此为1、2或者3,那么读取指定班级学生的数据。如果是这样的话,设此参数为P,则条件可以这么写where class*p=p*p
      

  3.   

    declare @s varchar(1000)
    declare @class int
    set @class = 0 --1/2/3
    select @s=case when @class = 0 then '1=1' else 'class='+cast(@class as int) end
    exec('select * from 表 where '+@s)
      

  4.   

    设此参数为P,则条件可以这么写where class*p=p*p
    declare @s varchar(1000)
    declare @class int
    set @class = 0 --1/2/3
    select @s=case when @class = 0 then '1=1' else 'class'+cast(@class as int)'='+cast(@class as int) end
    exec('select * from 表 where '+@s)