首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 这个触发器怎么写?
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 14:55:49 楼主
    表a
    id  档案编号  人数
    1  10001    2
    2  10002    4
    3  10005    3

    表b
    id  档案编号  成员编号
    1  10001    10001-001
    2  10001    10001-002
    3  10002    10002-001
    4  10002    10002-002
    5  10002    10002-003
    6  10002    10002-004
    7  10005    10005-001
    8  10005    10005-002
    9  10005    10005-003
    b表中的成员编号为按照一定格式自动生成.
    表a中的档案编号改变时候,b表中的档案编号要跟着改变,同时b表中的成员编号也要改变.
    如:将a表中的10005该成10003,b表中的第7.8.9行的数据应改变为
    7  10003  10003-001
    8  10003  10003-002
    9  10003  10003-003

    还有,改变b表中的档案编号的时候,a表中的'人数',和b表中的成员编号也应该相应改变
    如:将b表中的第4行的档案编号改为10001,则a表和b表分别变化为
    id  档案编号  人数
    1  10001    3
    2  10002    3
    3  10005    3

    id  档案编号  成员编号
    1  10001    10001-001
    2  10001    10001-002
    3  10002    10002-001
    4  10001    10001-003
    5  10002    10002-002
    6  10002    10002-003
    7  10005    10005-001
    8  10005    10005-002
    9  10005    10005-003
    删除a表中的一行记录b表中对应的档案编号的记录也要全删除掉
    在b表中填加一条记录10005,a表中10005记录的人数应该增加1,b表中填加的记录格式应该为
    10  10005    10005-004
    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fa_ge
    • 等级:
    发表于:2007-12-07 15:02:161楼 得分:0
    楼主应建立约束,实现级联更新,级联删除

    SQL code
    alter table t add constraint 约束名 foreign key(id) references t1(id) on delete cascade alter table t add constraint 约束名 foreign key(id) references t1(id) on update cascade


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2007-12-07 15:03:412楼 得分:0
    这么麻烦?还不如程序搞了.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:05:493楼 得分:0
    级联
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:09:554楼 得分:0
    有个表有160多个字段,和别的表建立不上关联...别的表都建立了关联了的,,,
    其实我要的就是因为建立了关联,表a修改一条后,表b一次更新了多条记录,表b中的哪个成员编号相应改变的触发器,
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:11:135楼 得分:0
    么说级联,,,你用级联能实现?说话卡都不负责
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:18:556楼 得分:0
    不会没人知道吧,,
    我写的这个是2表建立了级联后的
    create trigger d
    on b
    for delete,update,insert
    as
    begin
      update t
      set
          成员编号=t.档案编号+'-'+right('000'+rtrim((select count(*) from b where 档案编号=t.档案编号 and 成员编号 <=t.成员编号)),3)
      from
          b t
      where
          exists(select 1 from deleted where 档案编号=t.档案编号 and 成员编号 <t.成员编号)
    end

    但是改了a表后,b表的成员编号还是没变
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fa_ge
    • 等级:
    发表于:2007-12-07 15:24:597楼 得分:0


    SQL code
    create table t1 (id int, 档案编号 varchar(10)primary key , 人数 int ) insert into t1 select 1 , '10001', 2 union all select 2 , '10002', 4 union all select 3 , '10005', 3 go create table t2 (id int, 档案编号 varchar(10) , 成员编号 varchar(20)) insert into t2 select 1, '10001', '10001-001' union all select 2, '10001', '10001-002' union all select 3, '10002', '10002-001' union all select 4 , '10002', '10002-002' union all select 5, '10002', '10002-003' union all select 6, '10002', '10002-004' union all select 7, '10005', '10005-001' union all select 8, '10005', '10005-002' union all select 9, '10005', '10005-003' --drop table t1 alter table t2 add constraint cons_update foreign key(档案编号) references t1(档案编号) on update cascade alter table t2 add constraint cons_delete foreign key(档案编号) references t1(档案编号) on delete cascade --如:将a表中的10005该成10003,b表中的第7.8.9行的数据应改变为 update t1 set 档案编号='10003' where 档案编号='10005' select * from t2 /* id 档案编号 成员编号 ----------- ---------- -------------------- 1 10001 10001-001 2 10001 10001-002 3 10002 10002-001 4 10002 10002-002 5 10002 10002-003 6 10002 10002-004 7 10003 10005-001 8 10003 10005-002 9 10003 10005-003 (所影响的行数为 9 行) */ delete t1 where 档案编号='10001' select * from t2 /* id 档案编号 成员编号 ----------- ---------- -------------------- 3 10002 10002-001 4 10002 10002-002 5 10002 10002-003 6 10002 10002-004 7 10003 10005-001 8 10003 10005-002 9 10003 10005-003 (所影响的行数为 7 行) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • qiuming0306
    • 等级:
    发表于:2007-12-07 15:25:178楼 得分:0
    SQL code
    create TRIGGER [dbo].[aUpdate] on [dbo].[a] for update as begin if update(a.'档案编号') update b set b.'档案编号'=a.'档案编号', b.'成员编号'=dbo.hanshu(a.'档案编号')--编号函数你有吧! From a,Deleted d ,Inserted i --Deleted和Inserted临时表 where b.'档案编号'=d.'档案编号' end create trigger [dbo].[aDelete] on [dbo].[a] for delete as begin if delete(a.id) delete b From a,Deleted d ,Inserted i --Deleted和Inserted临时表 where b.'档案编号'=d.'档案编号' end

    --改变b表的正在写
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fa_ge
    • 等级:
    发表于:2007-12-07 15:28:139楼 得分:0
    当建好下面这级联时,问题就解决了一大半了,但对于A 的人数和B 表的成员编号,只要用触发器写就可以搞定了,
    你说呢
    SQL code
    alter table t2 add constraint cons_update foreign key(档案编号) references t1(档案编号) on update cascade alter table t2 add constraint cons_delete foreign key(档案编号) references t1(档案编号) on delete cascade
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:30:4510楼 得分:0
    鹤啸九天...你那就是做了个关联列..没用,,你看你把10005改成10003了后,,,后面的成员编号还是10005-001,10005-002,10005-003
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:31:3411楼 得分:0
    我要的就是要后面的哪个触发器的....前面的级联偶知道的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • qiuming0306
    • 等级:
    发表于:2007-12-07 15:37:3212楼 得分:0
    SQL code
    create trigger [dbo].[bAdd] on [dbo].[b] for update ,delete,insert as begin if update(b.'档案编号') begin update a set a.'人数'=a.'人数'+1 where a.'档案编号'=b.'档案编号' update a set a.'人数'=a.'人数'-1 From a,Deleted d ,Inserted i --Deleted和Inserted临时表 where a.'档案编号'=d.'档案编号' end if insert(b.id) begin update a set a.'人数'=a.'人数'+1 where a.'档案编号'=b.'档案编号' insert into b ( '成员编号' nvarchar(20) ) VALUES ( dbo.hanshu(b.'档案编号')---你的成员编号生成函数 ) end if delete(b.id) begin update a set a.'人数'=a.'人数'-1 where a.'档案编号'=b.'档案编号' end end
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fa_ge
    • 等级:
    发表于:2007-12-07 15:37:5013楼 得分:0
    SQL code
    --创建触发器 create trigger t1_update on t1 after update as begin update t2 set 成员编号=replace(成员编号,left(成员编号,5),档案编号) where 档案编号 in (select 档案编号 from inserted) end select * from t2 update t1 set 档案编号='10008' where 档案编号='10002' /* id 档案编号 成员编号 ----------- ---------- -------------------- 3 10008 10008-001 4 10008 10008-002 5 10008 10008-003 6 10008 10008-004 7 10003 10003-001 8 10003 10003-002 9 10003 10003-003 (所影响的行数为 7 行) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fa_ge
    • 等级:
    发表于:2007-12-07 15:39:3514楼 得分:0
    鹤啸九天...你那就是做了个关联列..没用,,你看你把10005改成10003了后,,,后面的成员编号还是10005-001,10005-002,10005-003

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

    我刚没有创建触发器,你现在再试下我上面这个
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • qiuming0306
    • 等级:
    发表于:2007-12-07 15:41:0715楼 得分:0
    成员编号用b.'档案编号'生成,每次都调用函数就好了,不要每次都写sql语句
    给你一个例子:我就不写了
    SQL code
    ALTER function [dbo].[AutoPerson_Code](@Family_ID int) returns varchar(30) as begin declare @Family_Code nvarchar(30) declare @str varchar(30) select @Family_Code = Family_Code from FamilyInfo where Family_ID=@Family_ID --获取家庭编码 select @str=isnull(max(PersonCode), right('00000000000000000'+right(@Family_Code,17),17)+'00') from PersonInfo where Family_ID=@Family_ID set @str=left(@str,17) + right('00'+convert(varchar(2),convert(int,right(@str,2))+1),2) return @str end

    在函数内可以实现号码连续
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:41:1916楼 得分:0
    ..............你试过没啊?就写成这样?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:44:2217楼 得分:0
    鹤...就按你写的哪个,,.你实验一下,看能不能实现我要的要求
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2007-12-07 15:45:4618楼 得分:0
    SQL code
    ]--试试 create table a(id int,档案编号 varchar(20),人数 int) insert a select 1, '10001', 2 union all select 2, '10002', 4 union all select 3, '10005', 3 create table b(id int,档案编号 varchar(20), 成员编号 varchar(20)) insert b select 1, '10001', '10001-001' union all select 2, '10001', '10001-002' union all select 3, '10002', '10002-001' union all select 4, '10002', '10002-002' union all select 5, '10002', '10002-003' union all select 6, '10002', '10002-004' union all select 7, '10005', '10005-001' union all select 8, '10005', '10005-002' union all select 9, '10005', '10005-003' sp_configure 'nested triggers',0 create trigger triaa on a for update as if update(档案编号) begin update b set b.档案编号=c.档案编号,成员编号=replace(成员编号,b.档案编号,c.档案编号) from deleted a,inserted c where a.id=c.id and a.档案编号=b.档案编号 update a set 人数=(select count(1) from b where b.档案编号=a.档案编号) end go create trigger triab on a for delete as delete from b where 档案编号 in(select 档案编号 from deleted) go create trigger triba on b for update as if update(档案编号) begin update b set b.成员编号=replace(b.成员编号,a.档案编号,c.档案编号) from deleted a,inserted c where a.id=c.id and b.id=a.id update a set 人数=(select count(1) from b where b.档案编号=a.档案编号) end go create trigger tribb on b for insert as if exists(select 1 from a,b where a.档案编号=b.档案编号) update a set 人数