日期:2014-05-17  浏览次数:20509 次

sqlserver一作业问题!
大家好,我有这样一个表:

表名:A

编码 滞纳金截止日期 费用额 滞纳金天数 滞纳金额 标志  
001 2012-08-20 20 a
001 2012-08-22 10 a
001 2012-08-18 100 a

在sqlserver里写个作业。

每天8点自动计算 滞纳金天数 和 滞纳金额

滞纳金额 = 0.5% * 滞纳金天数 * 费用额

滞纳金天数= now -滞纳金截止日期
如果 标志 = a 则 
{如果 now 大于 滞纳金截止日期 则 滞纳金天数= now -滞纳金截止日期 ,滞纳金额 = 0.5% * 滞纳金天数 * 费用额
否则 滞纳金天数= 0 ,滞纳金额 = 0}


------解决方案--------------------
说一下个人意见:
我觉得这样的计算,大部分都是无用功。如果这个数值的读取不平凡,建议你把滞纳金的计算写成函数,然后在需要的时候再用函数写出。
------解决方案--------------------
写个函数或者存储过程
然后在作业里面调
------解决方案--------------------
发现刚才回复的数据没有了?怎么搞的啊

------解决方案--------------------
SQL code

IF exists (select 1 from sysobjects where type='P' and name like N'proc_A' )
drop procedure 'proc_A'
go
create proc  proc_A 
@nowdate datetime
as
   update A  set 滞纳金天数=case when @nowdate>滞纳金截止日期 then datediff(day,滞纳金截止日期,@nowdate) else 0 end ,滞纳金额=(isnull(滞纳天数,0)*费用额*0.005) from A where 标志=a
go

------解决方案--------------------
SQL code

IF exists (select 1 from sysobjects where type='P' and name like N'proc_A' )
drop procedure 'proc_A'
go
create proc  proc_A 
@nowdate datetime
as
   update A  set 滞纳金天数=case when @nowdate>滞纳金截止日期 then datediff(day,滞纳金截止日期,@nowdate) else 0 end ,滞纳金额=(isnull(滞纳天数,0)*费用额*0.005) from A where 标志=a
go

------解决方案--------------------
SQL code

IF exists (select 1 from sysobjects where type='P' and name like N'proc_A' )
drop procedure 'proc_A'
go
create proc  proc_A 
@nowdate datetime
as
   update A  set 滞纳金天数=case when @nowdate>滞纳金截止日期 then datediff(day,滞纳金截止日期,@nowdate) else 0 end ,滞纳金额=(isnull(滞纳天数,0)*费用额*0.005) from A where 标志=a
go

------解决方案--------------------
我的回复也到哪去了? 无语。。。

SQL code

-->sql语句供参考,存储自己写
select 编码,滞纳金截止日期,
 case when datediff(dd,滞纳金截止日期,getdate())>0 and 标志=a then datediff(dd,滞纳金截止日期,getdate()) else 0 end 滞纳天数,
 case when datediff(dd,滞纳金截止日期,getdate())>0 and 标志=a then cast(datediff(dd,滞纳金截止日期,getdate()) as money)*0.05*费用额 else 0 end 滞纳金额 
from 表A