首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 求一trigger(update) ---看起来很简单,但是还是没搞好,在线期求大家解答啦! (续...) [已结贴,结贴人:sharpenabc]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    • 揭帖率:
    发表于:2008-05-16 08:33:57 楼主
    如果更新一tb1中多列,则tb2中的多列也自动更新(tb2中的资料是tb1中的资料无重复项的写入),包括批量更新的情况,前台表现为多笔资料的更改,同一保存。
    40  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 08:42:331楼 得分:0
    其实昨天已经问过insert啦,今天发现自己还懂的太少啦,多向大家学习啦,谢谢!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yrwx001
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 08:54:352楼 得分:0
    SQL code
    create trigger tr_tb1_u on tb1 for update as update tb2 set tb2.col1 = inserted.col1,tb2.col2 = inserted.col2.. from tb2,inserted where tb2.keycol = inserted.keycol
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:04:463楼 得分:0
    好像不行,存在的问题是:tb1中如果有笔重复(重复两笔或以上)的资料,在tb2中显示为一笔而已。但如果改啦重复资料中的一笔(设定为不重复),则意味着tb2中要插入一笔新的,也就是改过后的资料,但由于tb1中重复的那笔资料还存在,所以tb2中原先的资料不能动.如果像你这样就意味着丢失资料.具体例子:如tb1中的一列A值为{1,1,2,1,1},此时tb2中A值为{1,2},但如果改啦tb1中的A值为{1,3,2,4,5},则此时tb2中应为{1,3,2,4,5}
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yrwx001
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:21:294楼 得分:5
    SQL code
    --那再加個add動作就可以了. create trigger tr_tb1_u on tb1 for update as update tb2 set tb2.col1 = inserted.col1,tb2.col2 = inserted.col2.. from tb2,inserted where tb2.keycol = inserted.keycol insert tb2 select * from inserted where not exsits (select 1 from tb2 A where A.keycol = inserted.keycol and inserted.col1 = A.col1)

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zhou968
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:36:455楼 得分:5
    SQL code
    create triggeron tb1 for update as if exists(select A from tb1 group by A having count(A)>1) begin insert into tb2 select * from inserted where tb2.主键 = inserted.主键 end update tb2 set tb2.字段 = inserted.字段,…… from tb2,inserted where tb2.主键 = inserted.主键
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • cson_cson
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:41:586楼 得分:0
    SQL code
    create trigger tr_tb1_u on tb1 for update as insert tb2 select c1,c2,c3 from inserted a,tb2 b where checksum(a.c1,a.c2,a.c3)<>checksum(b.c1,b.c2,b.c3)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • cson_cson
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:43:037楼 得分:5
    SQL code
    create trigger tr_tb1_u on tb1 for update as insert tb2 select c1,c2,c3 from inserted a where not exists(select 1 from tb2 where checksum(a.c1,a.c2,a.c3)=checksum(c1,c2,c3)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • utpcb
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:44:458楼 得分:0
    用循环UPDATE
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 09:56:369楼 得分:0
    7楼是不是没有体现update,只是说明一下当更改时插入tb2中的情况
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • GDC_ZhaoYZ0304360
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 10:01:2610楼 得分:0
    SQL code
    create table tb1 (a int ,b int ,c int ,d int) go create table tb2 (a int ,b int ,c int ,d int) go insert into tb1 values(1,2,3,4) go create trigger t_update on tb1 for update as set nocount on insert into tb2 select * from inserted i where not exists ( select 1 from tb2 where a=i.a and b=i.b and c=i.c and d=i.d ) go update tb1 set c=100 go select * from tb2 go drop table tb1,tb2
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • GDC_ZhaoYZ0304360
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 10:13:5011楼 得分:0
    我知道你的意思了
    我想想啊
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 10:37:1012楼 得分:0
    哦,还有如果这些列是主key列,然后更新的就是他们该怎么办呢?5,7楼的写法能实现吗?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 10:39:2013楼 得分:0
    哦,特别4楼的写法,针对我刚刚提的情况,好像不能满足吧,如果非主key列的话还差不多,不过依然谢谢你们热心的解答
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yrwx001
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 10:44:0914楼 得分:0
    你key 列的話,不管如何都是不能更新的.要滿足約束條件.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 10:55:2115楼 得分:0
    这个应该没关系吧,如果我tb1中的主key是有4列组成,但我要改其中一列是完全没问题啊,只要他们的组合成一个整体不重复就没问题啊! 例如:由于前台主档做成主明细结构的那种(比如:三层)那这样的话我建主key就考虑建成一个组合列组成,因为这样一方面为外健的建立所考虑,另一方面也显示层次比较分明.但如果我改第三层的主key只要参照第二层的话,就没问题啊.此时对应第三层的table做一个汇总表的话就出现我刚刚提出要改主key的情况,但这完全不影响第一,第二层,因为是参照完整性的外key存在.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • GDC_ZhaoYZ0304360
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 11:45:0016楼 得分:15
    SQL code
    create table tb1(a int ,b int ,c int ,d int)--a is the primary key go create table tb2(b int ,c int, d int) go create trigger t_insert on tb1 for insert as set nocount on insert into tb2(b,c,d) select distinct b,c,d from inserted t where not exists (select 1 from tb2 where a=t.a and b=t.b and c=t.c and d=t.d) set nocount off go insert into tb1 values(1,1,2,3) insert into tb1 values(2,1,2,3) insert into tb1 values(3,3,3,4) insert into tb1 values(4,4,4,5) go select * from tb1 /* a b c d 1 1 2 3 2 1 2 3 3 3 3 4 4 4 4 5 */ select * from tb2 /* b c d 1 2 3 3 3 4 4 4 5 */ go create trigger t_update on tb1 for update as set nocount on if exists (select 1 from tb1 inner join deleted d on tb1.b=d.b and tb1.c=d.c and tb1.d=d.d where tb1.a<>d.a) begin insert into tb2(b,c,d) select distinct i.b,i.c,i.d from inserted i where not exists ( select 1 from (select tb2.* from tb2 inner join deleted d on tb2.b=d.b and tb2.c=d.c and tb2.d=d.d) x where x.b=i.b and x.c=i.c and x.d=i.d ) end else update tb2 set b=i.b,c=i.c,d=i.d from tb2 inner join deleted d on tb2.b=d.b and tb2.c=d.c and tb2.d=d.d inner join inserted i on i.a=d.a set nocount off go update tb1 set c=100 where a=2--insert go select * from tb2 /* b c d 1 2 3 3 3 4 4 4 5 1 100 3 */ go update tb1 set c=100 where a=3--update go select * from tb2 /* b c d 1 2 3 3 100 4 4 4 5 1 100 3 */ go drop table tb1,tb2



    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • GDC_ZhaoYZ0304360
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 11:47:2917楼 得分:0
    trigger t_insert 是昨天的那个


    trigger t_update 是今天是问的这个


    关注好的解决办法


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 13:19:1818楼 得分:0
    昨天的贴子在这里
    http://topic.csdn.net/u/20080515/19/92c063d4-2b3b-42fe-8d37-33e253355f38.html
    不过已经结贴啦,呵呵,但我还是想看看17楼是不是其它的实现方式,以便增进更进一步学习,没分啦,不过纯粹是学习,谢谢啦!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • sharpenabc
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 13:25:2819楼 得分:0
    哈哈,不好意思,你把insert写出来啦啊,谢谢你们的热心回答,我仔细看一下,共同学习!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Herb2
    • 等级:
    • 可用分等级:
    • 总技术专家分:
    • 总技术专家分排名:
    发表于:2008-05-16 13:57:4220楼 得分:10
    SQL code
    /******************************************/ /*回复:代码20080516002 总:00000000015 */ /*主题:通过触发器自动记录原表非重复数据 */ /*作者:二等草 */ /******************************************/ set nocount on /************例子数据 begin****************/ create table ta(a int,b int,c int)--a,b为主键 create table tb(b int,c int) --记录ta表存在的b,c不同的值 go /************例子数据 end******************/ /************代码 begin***************/ create trigger t_ab on ta for insert,update as begin delete a from tb a where exists(select 1 from deleted b where a.b = b and a.c = c and not exists(select 1 from ta where b.b=b and b.c = c)) insert tb select distinct b,c from inserted a where not exists(select 1 from tb where a.b=b and a.c = c) end go --测试 insert ta select 1,2,3 union select 2,2,3 union select 3,2,3 union select 1,3,2 select distinct 'a', b,c from ta select 'b',* from tb update ta set c = 6 where a =1 or a = 2 select distinct 'a',b,c from ta select 'b',