with as 更新表无效

班长老六哥 2015-11-06 11:08:42
我想把每个表的第一笔数据的一个值存到另一个表里,更新第一个好使,第二个就不好使了,求高手帮忙
with t1 as
(
SELECT TOP 1 TAV
FROM Tin_FengXiangYuan order by 时间 desc

),

t2 as
(
SELECT TOP 1 TAV
FROM Tin_YiPinYuan order by 时间 desc

)
update ATable_Output set TinAV= t1.TAV from t1 where ATable_Output.StationID=0
update ATable_Output set TinAV= t2.TAV from t2 where ATable_Output.StationID=1


...全文
248 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
comma212164 2015-11-06
  • 打赏
  • 举报
回复
update test1 set test1.shl=t2.shl from 
      (select test2.shl,test2.id from test2,test1 where test2.id=test1.id)t2 
   where test1.id=t2.id 
给你参考
道玄希言 2015-11-06
  • 打赏
  • 举报
回复
引用 3 楼 zk12668 的回复:
那我想实现把每个表的查询结果更新的新表里怎么做呢
这么写可以不? update ATable_Output set TinAV= t1.TAV from ( SELECT TOP 1 TAV FROM Tin_FengXiangYuan order by 时间 desc) t1 where ATable_Output.StationID=0 update ATable_Output set TinAV= t2.TAV from (SELECT TOP 1 TAV FROM Tin_YiPinYuan order by 时间 desc) t2 where ATable_Output.StationID=1
tcmakebest 2015-11-06
  • 打赏
  • 举报
回复
with其实只是一条完整语句的一部分, 并不是单独存在的, 所以第二个update跟第一个with毫无关系.
班长老六哥 2015-11-06
  • 打赏
  • 举报
回复
那我想实现把每个表的查询结果更新的新表里怎么做呢
aoye6202 2015-11-06
  • 打赏
  • 举报
回复
with 后的SQL语句只能有一句, 使用过一次后 with 将失效
aoye6202 2015-11-06
  • 打赏
  • 举报
回复
引用 1 楼 aoye6202 的回复:
with 后的SQL语句只能有一句, 使用过一次后 with 将失效
http://blog.csdn.net/sprita1/article/details/16857111 看看这个,可以结贴了
班长老六哥 2015-11-06
  • 打赏
  • 举报
回复
7楼和5楼都可以实现,谁的方法和效率更高呢
Tiger_Zhao 2015-11-06
  • 打赏
  • 举报
回复
两个更新语句要用两个With
with t1 as
(
SELECT TOP 1 TAV
FROM Tin_FengXiangYuan order by 时间 desc

),
update ATable_Output set TinAV= t1.TAV from t1 where ATable_Output.StationID=0

;with t2 as
(
SELECT TOP 1 TAV
FROM Tin_YiPinYuan order by 时间 desc

)
update ATable_Output set TinAV= t2.TAV from t2 where ATable_Output.StationID=1

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧