如果参数 num = 1 或者 2  就查询 select * from table where num = 1 or num = 2
其他情况就查询 select * from table where num = num如果不用动态sql怎么做?sql语句太长了,用动态sql后面看起来很麻烦

解决方案 »

  1.   


    DECLARE @num INT
    IF @num=1 
    select * FROM table where num = 1 or num = 2
    ELSE
    select * FROM table where num = @num
      

  2.   

    DECLARE @num INT
    IF @num=1 or @num=2 
    select * FROM table where num = 1 or num = 2
    ELSE
    select * FROM table where num = @num
      

  3.   


    DECLARE @num INT
    IF @num=1 or @num=2 
    select * FROM table where num = 1 or num = 2
    ELSE
    select * FROM table where num = @num
      

  4.   

    是因为sql语句太长了,所有不想写成 if else 的形式,要写两次
      

  5.   


    if num =1 or num =2 
    set num = '1,2'select * FROM table where num in num
      

  6.   

    还有什么好的办法没有?应为这样也只能用exec Sp_QueryReportByPeriod 执行
      

  7.   

    exec sp_executesql  打错
      

  8.   

    要么if
    要么动态sql
    其他没有办法了
      

  9.   

    用了个临时表  条件 加上了一个 num = @num or num = 1 or num=2然后再把临时表中的删除,效率可能不是很好
      

  10.   

    num是参数还是字段?你写的没看明白,where num = num?