sql疑惑。。大神们请教

lcqtgb 2010-06-17 05:02:20
create table #temp
(ke nvarchar(20),
d1 int default 0)

insert into #temp
select '1课1',0 union
select '2课1',0


select * from #temp 得到
1课1 0
2课1 0

update #temp set d1=(select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr
where ke='1课1' and exists(select 1 from wr_route where wr_mch='1050'
and wr_nbr=fgrd_nbr and wr_lot=fgrd_lot) and left(fgrd_char2,2)='1课' and fgr_date='20100402')


update #temp set d1=(select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr
where ke='2课1' and exists(select 1 from wr_route where wr_mch='1050'
and wr_nbr=fgrd_nbr and wr_lot=fgrd_lot) and left(fgrd_char2,2)='1课' and fgr_date='20100401')
--两者一起执行。。得到结果
1课1 NULL
2课1 7586

正常1课1是有值的。。
...全文
88 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcqtgb 2010-06-18
  • 打赏
  • 举报
回复
结贴了。。。
中国风 2010-06-18
  • 打赏
  • 举报
回复
看看是不是要这样的效果


--d1为null或0时才更新
update #temp set d1= isnull((select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr
where ke='1课1' and exists(select 1 from wr_route where wr_mch='1050' and ke=#temp.ke--加上这个
and wr_nbr=fgrd_nbr and wr_lot=fgrd_lot) and left(fgrd_char2,2)='1课' and fgr_date='20100402'),d1)
where ISNULL(d1,0)=0




update a set d1= b.fgrd_qty
from #temp a,
(select ke, sum(fgrd_qty) as fgrd_qty from fgrd_det join fgr_mstr on fgrd_fgr=fgr_fgr
where ke='1课1' and exists(select 1 from wr_route where wr_mch='1050'
and wr_nbr=fgrd_nbr and wr_lot=fgrd_lot) and left(fgrd_char2,2)='1课' and fgr_date='20100402' group by ke)b
where a.ke=b.ke and isnull(a.d1,0)=0
中国风 2010-06-18
  • 打赏
  • 举报
回复
--left(fgrd_char2,2)=LEFT(#temp.ke,2)/ke=#temp.ke--把这两个地方改改
update #temp set d1=(select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr
where ke=#temp.ke and exists(select 1 from wr_route where wr_mch='1050'
and wr_nbr=fgrd_nbr and wr_lot=fgrd_lot) and left(fgrd_char2,2)=LEFT(#temp.ke,2) and fgr_date='20100402'
lcqtgb 2010-06-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xys_777 的回复:]
引用 4 楼 nightmaple 的回复:
--两者一起执行。。得到结果
1课1 NULL
2课1 7586

正常1课1是有值的。。


执行能通过??? 以下不是有语法错误吗???

update #temp set d1=(select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr
whe……
[/Quote]

就是因为是临时表的字段。。。。怎么个更新法。。。
沙徐 2010-06-17
  • 打赏
  • 举报
回复
太乱了!!!看着累
永生天地 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 nightmaple 的回复:]
--两者一起执行。。得到结果
1课1 NULL
2课1 7586

正常1课1是有值的。。


执行能通过??? 以下不是有语法错误吗???

update #temp set d1=(select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr
where ke='1课1' and exists(……
[/Quote]
#temp和另两个表没有进行关联
  • 打赏
  • 举报
回复
需求不明确
nightmaple 2010-06-17
  • 打赏
  • 举报
回复
--两者一起执行。。得到结果
1课1 NULL
2课1 7586

正常1课1是有值的。。


执行能通过??? 以下不是有语法错误吗???

update #temp set d1=(select sum(fgrd_qty) from fgrd_det
join fgr_mstr on fgrd_fgr=fgr_fgr

where ke='1课1' and exists(select 1 from wr_route where wr_mch='1050'
and wr_nbr=fgrd_nbr and wr_lot=fgrd_lot) and left(fgrd_char2,2)='1课' and fgr_date='20100402')

dawugui 2010-06-17
  • 打赏
  • 举报
回复
最好给出完整的表结构,测试数据,计算方法和正确结果.


发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281

永生天地 2010-06-17
  • 打赏
  • 举报
回复
看起来有点乱,说说需求吧
lcqtgb 2010-06-17
  • 打赏
  • 举报
回复
其中的ke 是#temp中字段。。。其他都是非临时表中字段

22,207

社区成员

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

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