一个广告表
字段有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()
字段有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()
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表达
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
(
@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
建议你安我上面写的做
我在用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时出现语法错误。不知道怎么修改。我
也是个菜鸟啊。调的都快疯掉了。
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() 这样做效率慢,只是代码容易写
谢谢你用的那个就好了。帮我消除了个‘怕做法不好的意识’。还有谁能帮我解决上面我那个贴出来的语法错误的。如果调试可以的化。我再补上100分。
以后顶、sf、接分者、学习者等没心帮忙的。概不给分。
(
@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格式
我马上调试下,可以的化,24小时后我再加贴100分
============
采用union查询效率确实有点不好,不过上面有人提出的解决方法你也可以看看。可能对你以后这种查询会有帮助。呵呵
谢谢你帮我
这个用or条件就可以了吧
SELECT SawAds FROM SawAdsTable
WHERE SawCtrType = 0 OR
(SawCtrType = 1 AND SawMaxHit>=SawHits ) OR
(SawCtrType = 2 AND SawMaxDate>=GetDate())
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
根据yrwx001 的那个红色部分改进了下
set @strFilter= @strFilter+' and SawAdsMaxDate>='+CONVERT(char(12), GETDATE(), 3)
就可以解决那个字符串转成日期的错误问题。谢谢各位。
期待有更好的写法。我是觉得duset已经写的很精简了。呵呵
是 varchar 還是 Datetime