insert,update触发器求助

SunnyerLiu 2011-08-30 10:02:26
有两个表:a表字段,no,itm,price,cost。b表字段,no,itm,up.
需求:当b表中插入一条或多条,更新a表中no和itm对应的字段price=up,cost=up.当b表中的up更新时,也同时更新a表中no和itm对应的字段price=up,cost=up.
...全文
238 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
aixli520 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 h2osio2 的回复:]

请高手指点啊
[/Quote]

你说的插入、更新触发器不是已经给你代码了(10楼的),你还要做什么啊,多条也是一样的吗,你放到你的数据库里执行了没啊,不用写的连数据都和你的一样吧。把字段改成你表中的字段不就OK了吗。
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
请高手指点啊
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
对了,那个可能一下插入多条记录并可能同时更新多条记录
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 h2osio2 的回复:]
另外我还想把这个查询结果放到临时表中,也是学人家的,但就是放不进去,帮帮我.
with f as (select * from up_def where price_id='3' and e_dd>=getdate())
select * from f t where not exists(select 1 from f where prd_no=t.prd_no and sys_date>getdate())
我现在是用啊,学的和用的差太多了,学习中
geniuswjt 2011-08-30
  • 打赏
  • 举报
回复
列队欢迎晴儿mm加入[Quote=引用 15 楼 h2osio2 的回复:]
这个网站是我看到的最好的中文开发网站,真的太好了,以后要天天来了
[/Quote]
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
这个网站是我看到的最好的中文开发网站,真的太好了,以后要天天来了
geniuswjt 2011-08-30
  • 打赏
  • 举报
回复
吃个饭看看,先闪
geniuswjt 2011-08-30
  • 打赏
  • 举报
回复
初学就玩cte?你吃得消么- -[Quote=引用 12 楼 h2osio2 的回复:]
另外我还想把这个查询结果放到临时表中,也是学人家的,但就是放不进去,帮帮我.
with f as
(select * from up_def where price_id='3' and e_dd>=getdate())
select * from f t where not exists(select 1 from f where prd_no=t.prd_no and sys_d……
[/Quote]
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
另外我还想把这个查询结果放到临时表中,也是学人家的,但就是放不进去,帮帮我.
with f as
(select * from up_def where price_id='3' and e_dd>=getdate())
select * from f t where not exists(select 1 from f where prd_no=t.prd_no and sys_date>t.sys_date)
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
我不懂的,补充.比如原来B表:a12,12,0.78,a表:a12,12,0.78,0.78.当B表中的0.78变为0.68时,更新a表中的两个0.78为0.68
aixli520 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 h2osio2 的回复:]
有两个表:a表字段,no,itm,price,cost。b表字段,no,itm,up.
需求:当b表中插入一条或多条,更新a表中no和itm对应的字段price=up,cost=up.当b表中的up更新时,也同时更新a表中no和itm对应的字段price=up,cost=up.
[/Quote]
楼主,下面是写的触发器更新的代码,可以解决你的问题。
--建表
create table tb6
(
no int primary key,
itm varchar(20),
price int,
cost int
)
create table tb7
(
no int primary key,
itm varchar(20),
up int
)
--建立触发器
create trigger insert_Trigger
on tb7
after insert,update
as
update tb6 set price=tb7.up,cost=tb7.up
from tb7,tb6
where tb6.no=tb7.no and tb6.itm=tb7.itm
--测试数据
insert tb6 values(100,'test1',20,30)
insert tb7 values(100,'test1',40)
update tb7 set up=90 where no=100
--小F-- 2011-08-30
  • 打赏
  • 举报
回复
当b表中插入一条或多条,更新a表中no和itm对应的字段price=up,cost=up.当b表中的up更新时,也同时更新a表中no和itm对应的字段price=up,cost=up


红色的部分是怎么更新的??
--小F-- 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nbdba 的回复:]
b表
会不会同时插入两条no,itm相同的纪录,这时up如何计算,加在一起?还是随便取一个
[/Quote]
这个需要同问
NBDBA 2011-08-30
  • 打赏
  • 举报
回复
大概:

create trigger tr_b
on b
for insert,update
as

update a set
price= t.up
,cost = t.up
from a,(
select no,itm
,sum(up) as up -- 或者max(up) as up
from inserted
group by no,itm
) as t
where a.no = t.no
and a.itm = t.itm

insert a(no,itm,price,cost)
select no,itm
,sum(up) as up -- 或者max(up) as up
,sum(up) as up -- 或者max(up) as up
from inserted i
where not exists (
select 1
from a
where a.no =i.no
and a.itm = i.itm
)
group by no,itm

go
geniuswjt 2011-08-30
  • 打赏
  • 举报
回复

--当然只是触发条件要你自己判断下选取,写法还是如下:
update a set price='up',cost='up' from a,inserted b
where a.no=b.no and a.itm=b.itm
--因为update实际相当于先delete再insert
--固从inserted表里取no,itm,其实从deleted表里取也可以
geniuswjt 2011-08-30
  • 打赏
  • 举报
回复

--第二个的话,update触发器要对应到更新具体哪个字段的,你要更新哪个字段然后触发?
geniuswjt 2011-08-30
  • 打赏
  • 举报
回复

--第一个insert触发器里这么写
update a set price='up',cost='up' from a,inserted b
where a.no=b.no and a.itm=b.itm
NBDBA 2011-08-30
  • 打赏
  • 举报
回复
b表
会不会同时插入两条no,itm相同的纪录,这时up如何计算,加在一起?还是随便取一个

SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
等这么久都每人帮忙吗?
SunnyerLiu 2011-08-30
  • 打赏
  • 举报
回复
有人帮忙看看吗?

22,294

社区成员

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

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