当前位置: 代码迷 >> Sql Server >> 更新表揭示对象名无效,求高手帮忙
  详细解决方案

更新表揭示对象名无效,求高手帮忙

热度:100   发布时间:2016-04-24 08:46:42.0
更新表提示对象名无效,求高手帮忙
with 
tb as
(
  select StationID,DValve1,
  case 
    when DT>5 then 10000
    when DT>=4 and DT<=5 then 5
    When DT>=3 and DT<4 then 4
    When DT>=2 and DT<3 then 3
    When DT>=1 and DT<2 then 2
    When DT>0.5 and DT<1 then 1
    When DT>=-0.5   and DT<0.5 then 0
    When DT>=-1 and DT<-0.5 then -1
    When DT>=-2 and DT<-1 then -2
    When DT>=-3 and DT<-2 then -3
    When DT>=-4 and DT<-3 then -4
    When DT>=-5 and DT<-4 then -5
    When DT<-5 then -10000
   end as DT
  from  A0401CtrOutput

)

select t1.StationID,t1.DT,t2.DValve1
from tb as t1
left join A0104QueryTable as t2 
on t1.DT=t2.DT 

 update A0401CtrOutput set DValve1= tb1.DValve1 from t1 where t1.StationID=A0401CtrOutput.StationID


------解决思路----------------------
with后面只能跟一个操作依据,你的例子中,CTE最后一个操作是select,之后就释放tb和头t1了,你可以这么写:
with tb as
(
  select StationID,DValve1,
  case 
    when DT>5 then 10000
    when DT>=4 and DT<=5 then 5
    When DT>=3 and DT<4 then 4
    When DT>=2 and DT<3 then 3
    When DT>=1 and DT<2 then 2
    When DT>0.5 and DT<1 then 1
    When DT>=-0.5   and DT<0.5 then 0
    When DT>=-1 and DT<-0.5 then -1
    When DT>=-2 and DT<-1 then -2
    When DT>=-3 and DT<-2 then -3
    When DT>=-4 and DT<-3 then -4
    When DT>=-5 and DT<-4 then -5
    When DT<-5 then -10000
   end as DT
  from  A0401CtrOutput
)
,t1 as

select t1.StationID,t1.DT,t2.DValve1
from tb 
left join A0104QueryTable as t2 
on tb.DT=t2.DT 

 update A0401CtrOutput set DValve1= tb1.DValve1 from t1 where t1.StationID=A0401CtrOutput.StationID
  相关解决方案