一个广告表
字段有SawID(主键)  SawAds(广告内容)SawCtrlTyp(广告过期类型0---无限制;
1---根据最大日期来判断;
2----根据最大点击率来判断
)SawMaxDate(最大日期,必须在SawCtryType=1的时候填入本值)SawMaxHit(最大点击率,必须在SawCtryType=2的时候填入此值)
SawHit 点击率
SawAds广告的具体内容===============
表结构就是那样
那我要怎么根据那个广告限制策略---SawCtrlTyp来查询SawAds广告内容
比如当SawCtryTyp=0的时候,查询条件是  1=1
当SawCtryType=1的时候  SawHit<= SawMaxHit
当SawCtyType=2的时候SawMaxDate>=GetDate() 

解决方案 »

  1.   

    SQL在程序中传递的参数只能限定为比如:SawID=125
      

  2.   

    我举个算法:
    Create proc SawAdsWordInfo
    (
    @SawID  int
    )
    as 
    select SawAds
    from SawAdsTable
    where 如果SawCtrType的值=0   1=1
          如果SawCtrType的值=1   SawMaxHit>=SawHits
          如果SawCtrType的值=2   SawMaxDate>=GetDate()
    and  SawID=@SawID现在的关键问题是我不知道怎么用SQL表达
      

  3.   

    大家能否有比较好的查询方法。最好不要用临时变量先查SawCtrType然后在执行一遍查询结果。
      

  4.   


    select SawAds 
    from SawAdsTable 
    where case SawCtrType when 0 then 1=1 
          when 1 then SawMaxHit>=SawHits 
          when 2 then SawMaxDate>=GetDate()  end
    and  SawID=@SawID 
      

  5.   

    Create proc SawAdsWordInfo 

    @SawID  int 

    as  
    begin
       declare @SawCtrType int 
       select @SawCtrType=SawCtrlTyp from 广告表 where SawID=@SawID
       if(@SawCtrType=0)
         begin
            select SawAds from SawAdsTable where  SawID=@SawID
         end
       else if(@SawCtrType=1)
         begin
            select SawAds from SawAdsTable where  SawID=@SawID and SawMaxHit>=SawHits
         end
       else 
         begin
            select SawAds from SawAdsTable where  SawID=@SawID and SawMaxDate>=GetDate()
         end
    end
      

  6.   

    还可以构造出最后要执行的t_sql语句,用exec()来执行但无论怎么做都要分条件来构造
      

  7.   

    不能用1=1,因为case when 中的then返回的是结果,不能是表达式
    建议你安我上面写的做 
      

  8.   

    唉,终于又有人帮忙了。谢谢啊。这年代回帖的人真不容易。csdn怎么感觉人气怪怪的。
    我在用case不行的时候是用这个方案的
    ALTER proc SawAdwordsShow
    (
      @SawID int
    )
    as
    declare @CtrlType int
    declare @strFilter nvarchar(50)
    set @strFilter=' (SawID='+CAST ( @SawID  AS NVarChar)+')' select @CtrlType=SawCtrlType
    from SawAdwords
    where SawID=@SawIDif(@CtrlType=0)
    set @strFilter=@strFilter+'  AND  1=1'if(@CtrlType=1)
    set @strFilter=@strFilter+' and  SawMaxHits>=SawHits'if(@CtrlType=2)
    set @strFilter=Cast( @strFilter+' and  SawAdsMaxDate>='+GetDate()+'' as nvarchar)exec
    ('
    select SawAds
    from SawAdwords
    where '+@strFilter
    )
    但调试的时候,如果按时间查询的化。那老是提示说string转化为datetime时出现语法错误。不知道怎么修改。我
    也是个菜鸟啊。调的都快疯掉了。
      

  9.   

    select SawAds from SawAdsTable where  SawID=@SawID and SawCtrlTyp = '0'
    UNION ALL
    select SawAds from SawAdsTable where  SawID=@SawID and SawCtrlTyp = '1' and SawMaxHit>=SawHits 
    UNION ALL
    select SawAds from SawAdsTable where  SawID=@SawID and SawCtrlTyp = '2' and SawMaxDate>=GetDate() 这样做效率慢,只是代码容易写
      

  10.   

    JL99000 
    谢谢你用的那个就好了。帮我消除了个‘怕做法不好的意识’。还有谁能帮我解决上面我那个贴出来的语法错误的。如果调试可以的化。我再补上100分。
    以后顶、sf、接分者、学习者等没心帮忙的。概不给分。
      

  11.   

    ALTER proc SawAdwordsShow 

      @SawID int 

    as 
    declare @CtrlType int 
    declare @strFilter nvarchar(50) 
    set @strFilter=' (SawID='+CAST ( @SawID  AS NVarChar)+')'  select @CtrlType=SawCtrlType 
    from SawAdwords 
    where SawID=@SawID 
    if(@CtrlType=0) 
    set @strFilter=@strFilter+'  AND  1=1' if(@CtrlType=1) 
    set @strFilter=@strFilter+' and  SawMaxHits>=SawHits' if(@CtrlType=2) 
    set @strFilter= @strFilter+' and  SawAdsMaxDate>='+CONVERT(VARCHAR(8),GetDate(), 112)exec 
    (' 
    select SawAds 
    from SawAdwords 
    where '+@strFilter 
    )--適用日期格式存為 99991231 varchar格式 
      

  12.   

    谢谢yrwx001 
    我马上调试下,可以的化,24小时后我再加贴100分
      

  13.   


    ============
    采用union查询效率确实有点不好,不过上面有人提出的解决方法你也可以看看。可能对你以后这种查询会有帮助。呵呵
    谢谢你帮我
      

  14.   

    不好意思,没仔细看.
    这个用or条件就可以了吧
    SELECT SawAds FROM SawAdsTable 
    WHERE SawCtrType = 0 OR
    (SawCtrType = 1 AND SawMaxHit>=SawHits ) OR
    (SawCtrType = 2 AND SawMaxDate>=GetDate()) 
      

  15.   

    [Quote=引用 14 楼 yrwx001 的回复:]
    SQL codeALTER proc SawAdwordsShow 

      @SawID int 

    as 
    declare @CtrlType int 
    declare @strFilter nvarchar(50) 
    set @strFilter=' (SawID='+CAST ( @SawID  AS NVarChar)+')'  ====================
    将 expression 转换为数据类型 smalldatetime 时发生算术溢出错误。
    存储过程: T_cnsaw.dbo.SawAdwordsShow
    返回代码 = 0
      

  16.   

    分享下:我自己写的那个答案结果:
    根据yrwx001 的那个红色部分改进了下
    set @strFilter= @strFilter+' and  SawAdsMaxDate>='+CONVERT(char(12), GETDATE(), 3)
    就可以解决那个字符串转成日期的错误问题。谢谢各位。
    期待有更好的写法。我是觉得duset已经写的很精简了。呵呵
      

  17.   

    這個跟你的SawMaxDate 的字段類型\有關係的.
    是 varchar 還是 Datetime
      

  18.   

    SawMaxDate   smallDateTime