怎么更新2个表呢?

刘欣的博客 2007-12-24 03:58:54
农田表,里面有个字段:rice
仓库表,里面也有个字段:rice


怎么把农田里的rice,搬到仓库的rice中去呢???
...全文
495 40 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
gimse7en 2007-12-25
  • 打赏
  • 举报
回复
mark
cannycsy 2007-12-25
  • 打赏
  • 举报
回复
不能做个事件触发,让它搬过去吗?当条件满足时就执行

update 农田表 set rice=0 where whid in (select id from 仓库)
迷失的空间 2007-12-24
  • 打赏
  • 举报
回复
使用事务,类似于银行的转账。
先更新农田表,将其记录取出来保存为一变量,并更新表为空,
然后把变量赋值给仓库,
更新成功提交,否则回滚。
有些困了,等待高手写点代码吧。
还有:
楼主提问时最好一次把要求发齐。
wangkaiyang 2007-12-24
  • 打赏
  • 举报
回复
顶,学习学习
ddstrue 2007-12-24
  • 打赏
  • 举报
回复
hao ba
yanhangfei1983 2007-12-24
  • 打赏
  • 举报
回复
不好意思
create procedure my_proc
as
begin
update a
set a.rice=a.rice + b.rice
from warehouse a
left join
(select whid,sum(rice) rice from millfarm group by whid) b
on a.id=b.whid

update millfarm set rice=0
end

exec my_proc
dawugui 2007-12-24
  • 打赏
  • 举报
回复
他不是保护数据,是将一组SQL语句.

简单的说就是一句不能搞定数据的多个SQL语句组合在一起的东西.
刘欣的博客 2007-12-24
  • 打赏
  • 举报
回复
请问存储过程可以保护数据吗?
dawugui 2007-12-24
  • 打赏
  • 举报
回复
不用事务,建立个存储过程即可.
dawugui 2007-12-24
  • 打赏
  • 举报
回复
create procedure my_proc
as
begin
update a
set a.rice=a.rice + b.rice
from warehouse a
left join
(select whid,sum(rice) rice from millfarm group by whid) b
on a.id=b.whid

update millfarm set rice=0
end

exec my_proc
fcuandy 2007-12-24
  • 打赏
  • 举报
回复
begin tran
update a set a.rice=isnull(b.rice,0) + a.rice
from 仓库 a
left join
(select whid,sum(rice) rice from 农田 group by whid) b
on a.id=b.whid

--如果只清rice字段
update 农田 set rice=0
if @@error!=0
rollback tran
else
commit tran
刘欣的博客 2007-12-24
  • 打赏
  • 举报
回复
感谢fcuandy ,fa_ge ,happyflystone .....

fcuandy的方法可以用了,呵呵.


还有我写到数据库的定时作业中,是不是要用事务包起来?



update a

set a.rice=a.rice + b.rice

from warehouse a

left join
(select whid,sum(rice) rice from millfarm group by whid) b

on a.id=b.whid



update millfarm set rice=0

事务包起来怎么写呢?

ltc79 2007-12-24
  • 打赏
  • 举报
回复
fcuandy 2007-12-24
  • 打赏
  • 举报
回复
把我24楼的表名换成你的表名即可. 关于清数据,你选一种方法
刘欣的博客 2007-12-24
  • 打赏
  • 举报
回复
仓库表:warehouse
id ,rice

农田表:millfarm
id,rice,whid


能给个完整点的sql吗?

十一月猪 2007-12-24
  • 打赏
  • 举报
回复
update t
set t.rice = t1.rice
from a t , b t1
where t.id = t1 .id

fcuandy 2007-12-24
  • 打赏
  • 举报
回复
修正一下.


update a set a.rice=isnull(b.rice,0) + a.rice
from 仓库 a
left join
(select whid,sum(rice) rice from 农田 group by whid) b
on a.id=b.whid

--如果是删除所有农田记录
truncate table 农田
--如果只清rice字段
update 农田 set rice=0
fa_ge 2007-12-24
  • 打赏
  • 举报
回复
现在我有3块田,每块有200的rice,都指定whid=23的仓库. 仓库id=23

运行sql后, 仓库的rice只增加了200.应该加600的???

---------------------------



update 仓库
set rice=rice+b.rice
from 仓库 a join (
select whid,sum(rice)as rice from 农田表 group by whid
)b
on a.id=b.whid
where a.id=23

areswang 2007-12-24
  • 打赏
  • 举报
回复
不好意思,是按12楼无枪狙击手的
fcuandy 2007-12-24
  • 打赏
  • 举报
回复
update a set a.rice=isnull(b.rice,a.rice)
from 仓库 a
left join
(select whid,sum(rice) rice from 农田 group by whid) b
on a.id=b.whid

--如果是删除所有农田记录
truncate table 农田
--如果只清rice字段
update 农田 set rice=0
加载更多回复(20)

34,838

社区成员

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

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