对数据进行批量操作

linjie0003 2009-09-10 10:51:43
我有2张表
TB1
DAH LSQF
2 2.00
3 24.00
4 5.00
5 6.00
6 8.00

TB2
DAH YCYE
2 3.00
3 4.00
4 55.00
5 63.00
6 2.00

我想做的是
TB2的YCYE的数值比TB1的LSQF大的时候 进行批量的删除TB1对应的数,修改TB2中YCYE的值(这个值是TB2的YCYE-TB1的LSQF)

TB2的YCYE的数值比TB1的LSQF小的时候 进行批量的修改TB1的LSQF值和TB2的YCYE值(这个值是TB2的YCYE和TB1的LSQF相减的数)
...全文
62 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
linjie0003 2009-09-10
  • 打赏
  • 举报
回复
恩! 明白意思了...我具体的试一下,谢谢
feixianxxx 2009-09-10
  • 打赏
  • 举报
回复
--删除
--更新

分2步走
华夏小卒 2009-09-10
  • 打赏
  • 举报
回复
还是这样?
华夏小卒 2009-09-10
  • 打赏
  • 举报
回复

declare @tb1 table(DAH int, LSQF decimal(5,2))
insert @tb1 select
2 , 2.00 union all select
3 , 24.00 union all select
4 , 5.00 union all select
5 , 6.00 union all select
6 , 8.00

declare @tb2 table(DAH int, YCYE decimal(5,2))
insert @tb2 select
2, 3.00 union all select
3 , 4.00 union all select
4 , 55.00 union all select
5 , 63.00 union all select
6 , 2.00

select a.* into #t from @tb1 a,@tb2 b where a.dah=b.dah and ycye>lsqf

update @tb2 set YCYE=ycye-lsqf
from @tb2 b,#t a where a.dah=b.dah

delete from @tb1 where dah in (select dah from #t)

----------TB2的YCYE的数值比TB1的LSQF大 -------------------

select a.dah,a.lsqf,b.ycye into #t1 from @tb1 a,@tb2 b where a.dah=b.dah and ycye<lsqf

update @tb1 set lsqf=b.ycye-b.lsqf from @tb1 a,#t1 b where a.dah=b.dah
update @tb2 set ycye=b.ycye-b.lsqf from @tb2 a,#t1 b where a.dah=b.dah

----------TB2的YCYE的数值比TB1的LSQF小--------------------



select * from @tb2

DAH YCYE
----------- ---------------------------------------
2 1.00
3 -20.00
4 50.00
5 57.00
6 -6.00

(5 行受影响)

select * from @tb1


DAH LSQF
----------- ---------------------------------------
3 -20.00
6 -6.00

(2 行受影响)


drop table #t
drop table #t1
linjie0003 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xuejiecn 的回复:]
写两条语句。
[/Quote]

如果用存储过程的话,应该要用游标!

楼上说的,要写两条语句,

是怎么实现的啊
xuejiecn 2009-09-10
  • 打赏
  • 举报
回复
写两条语句。
fanzhouqi 2009-09-10
  • 打赏
  • 举报
回复
我估摸着需要游标
linjie0003 2009-09-10
  • 打赏
  • 举报
回复
存储过程?
黄_瓜 2009-09-10
  • 打赏
  • 举报
回复
用存储过程吧
linjie0003 2009-09-10
  • 打赏
  • 举报
回复
意思是,要循环对每一条进行操作??
华夏小卒 2009-09-10
  • 打赏
  • 举报
回复
一句搞不定

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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