日期:2014-05-18  浏览次数:20418 次

关于派生表更新问题
我做了一个存储结构,结果运行的时候提示 派生表 不可更新,因为派生表中的某一列是派生的或是常量
程序如下

ALTER PROCEDURE dbo.月考勤计算1
(@ny char(4))
AS 

update b
set b.kkg = case when b.d>=240 then 0.5 else 0 end + case when b.t>=240 then 0.5 else 0 end,
b.ksj = case when b.d>15 and b.d< 240 then 0.25 else 0 end + case when b.t>15 and b.t <240 then 0.5 else 0 end
from (
select a.kkg,a.ksj,datediff(mi,a.rq,a.sd)-datediff(mi,0,a.yd) as d,
datediff(mi,0,yt)-datediff(mi,a.rq,a.st) + case when a.blb = 3 then 1440 else 0 end as t
from 考勤表 a
where a.ny = @ny and a.sd is not null and a.kzz = 0
) b

如果直接用考勤表来更新的话,是没有问题,不过涉及到两个计算公式的重复使用,感觉有些繁琐,所以想使用派生表来简化下
不知道怎么更改下会比较有效率点,谢谢

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


insert #test
select a.kkg,a.ksj,datediff(mi,a.rq,a.sd)-datediff(mi,0,a.yd) as d,
datediff(mi,0,yt)-datediff(mi,a.rq,a.st) + case when a.blb = 3 then 1440 else 0 end as t
from 考勤表 a
where a.ny = @ny and a.sd is not null and a.kzz = 0

update    b
set    b.kkg = case when b.d>=240 and b.t>=240  then 1 else 0 end ,
b.ksj = case when (b.d>15 and b.d< 240) and (b.t>15 and b.t <240) then 0.75 else 0 end 
from #test