还说触发器

wjx_0_2001 2009-01-15 07:18:19
触发器已经写好

环境参数:

同台机器上有两个数据库A和B

先在A中有触发器,当A中的A1表中有新的数据的时候,就触发,把这个记录写到数据库B中的B1表中


---------------
测试结果:

A中A1表中,有新记录2000条

但在:
B中B1表中,只有记录100条

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

问题:

应该是这个两个表中的记录数是一样的,都为2000条,

但现在不知道为什么只写入了100条记录到数据库B中的B1表里


急……
...全文
81 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjx_0_2001 2009-02-13
  • 打赏
  • 举报
回复
搞顶了

揭贴…………


附注:触发器,是越短小越好,但没有说不能写大,再说,多大算大,写多少才算合适,没的定论
跟着感觉走就好……



我现在就是按照上面写的还是完成了……

为什么开始有问题呢?那是因为触发器写的不正确


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

wjx_0_2001 2009-02-06
  • 打赏
  • 举报
回复
年前的问题,到现在还没能解决掉

问好下大家,虽然晚了点
一条晚起的虫 2009-01-16
  • 打赏
  • 举报
回复
太大个了。
看看没有写入的是那些。
pbjcc 2009-01-16
  • 打赏
  • 举报
回复
mark
zhoujianhei 2009-01-16
  • 打赏
  • 举报
回复
触发器应该是短小精悍的,以便在极短时间内完成工作,书上讲解触发器的时候都是建议越快结束越好。
如果太复杂可以使用存储过程。
wjx_0_2001 2009-01-16
  • 打赏
  • 举报
回复
我也不想写这大啊


但没办法啊,不同的库,不同的表,不同的含义,这一切都要将其中一个表按照另一库里的一个表的意思进行转换


只有转换了,才能在新的系统中使用



哈哈


但就是奇怪,触发器写的数据与INSERT语句执行的写入的数据,在数目上不一样


只写了部分

就纳闷了
wjx_0_2001 2009-01-15
  • 打赏
  • 举报
回复
向你讨教……

别说你也知道

wjx_0_2001 2009-01-15
  • 打赏
  • 举报
回复
TO zhoujianhei:

我是第一次接触触发器,还不知道怎么写,

你说第一次见到写成这样,那你说,触发器应该怎么写


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

这个是不同库,不同表,将一个表中的数据翻译成或者说,转化成系统能使用的并且写入库中

由于有很多地方,涉及到转换,所以量是比较大的


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

TO zhoujianhei:触发器怎么写?

wjx_0_2001 2009-01-15
  • 打赏
  • 举报
回复
楼上的,我还真的是第一次开始写触发器

呵呵


那你说说,触发器应该怎么写?
zhoujianhei 2009-01-15
  • 打赏
  • 举报
回复
将触发器写成这样还是第一次见到。
wjx_0_2001 2009-01-15
  • 打赏
  • 举报
回复
else
begin
if(@forur_id=0)--三级部门
begin
--一级部门
--首先对当前的部门的一级部门进行筛选
select @rec_num= COUNT(*) from tld_card.dbo._group
--先插入一级部门
insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
0,
@rec_num+1,
@rec_num+1 ,
@rec_num+1 ,
i.DPNAME1,
i.DPNAME1
from inserted i
where i.DPNAME1 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=0)
--二级部门

declare @san_topid bigint
select @rec_num= COUNT(*) from tld_card.dbo._group

select @san_topid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME1

insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)
select @rec_num+1 ,
1,
@san_topid,
@rec_num+1 ,
@rec_num+1 ,
i.DPNAME2,
i.DPNAME1+' '+i.DPNAME2
from inserted i
where i.DPNAME2 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=1)
--
declare @oneid bigint
declare @twoid bigint
select @rec_num= COUNT(*) from tld_card.dbo._group

select @oneid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME1

select @twoid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME2

insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
2,
@oneid,
@twoid ,
@rec_num+1,
i.DPNAME3,
i.DPNAME1+' '+i.DPNAME2+' '+i.DPNAME3
from inserted i
where i.DPNAME3 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=2)
end
else --四级部门
begin
---先一级部门
select @rec_num= COUNT(*) from tld_card.dbo._group
--先插入一级部门
insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
0,
@rec_num+1,
@rec_num+1 ,
@rec_num+1 ,
i.DPNAME1,
i.DPNAME1
from inserted i
where i.DPNAME1 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=0)
---二级部门
declare @four_topid bigint
select @rec_num= COUNT(*) from tld_card.dbo._group

select @four_topid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME1

insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)
select @rec_num+1 ,
1,
@four_topid,
@rec_num+1 ,
@rec_num+1 ,
i.DPNAME2,
i.DPNAME1+' '+i.DPNAME2
from inserted i
where i.DPNAME2 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=1)
---三级部门
declare @four_oneid bigint
declare @four_twoid bigint
select @rec_num= COUNT(*) from tld_card.dbo._group


select @four_oneid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME1

select @four_twoid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME2

insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
2,
@four_oneid,
@four_twoid ,
@rec_num+1,
i.DPNAME3,
i.DPNAME1+' '+i.DPNAME2+' '+i.DPNAME3
from inserted i
where i.DPNAME3 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=2)
--四级部门
declare @si_oneid bigint
declare @si_twoid bigint
declare @si_sanid bigint
select @rec_num= COUNT(*) from tld_card.dbo._group

select @si_oneid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME1

select @si_twoid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME2

select @si_sanid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME3

insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
3,
@si_oneid,
@si_twoid ,
@si_sanid,
i.DPNAME4,
i.DPNAME1+' '+i.DPNAME2+' '+i.DPNAME3+' '+i.DPNAME4
from inserted i
where i.DPNAME4 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=3)
--
end
end

end
wjx_0_2001 2009-01-15
  • 打赏
  • 举报
回复
if(@sec_id=0 and @third_id=0 and @forur_id=0)--一级部门
begin
select @rec_num= COUNT(*) from tld_card.dbo._group
insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
0,
@rec_num+1 ,
@rec_num+1 ,
@rec_num+1,
i.DPNAME1,
i.DPNAME1
from inserted i
where i.DPNAME1 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=0)
end
else
begin ------二级部门
if( @third_id=0 and @forur_id=0)
begin
--首先对当前的部门的一级部门进行筛选
select @rec_num= COUNT(*) from tld_card.dbo._group
--先插入一级部门
insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
0,
@rec_num+1,
@rec_num+1 ,
@rec_num+1,
i.DPNAME1,
i.DPNAME1
from inserted i
where i.DPNAME1 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=0)
--二级部门
declare @topid bigint
select @rec_num= COUNT(*) from tld_card.dbo._group
select @topid=tld_card.dbo._group.id from tld_card.dbo._group,inserted i
where tld_card.dbo._group.name=i.DPNAME1
insert tld_card.dbo._group(id,lev,id_top,id_sec,id_thi,name,tname)

select @rec_num+1 ,
1,
@topid,
@rec_num+1 ,
@rec_num+1 ,
i.DPNAME2,
i.DPNAME1+' '+i.DPNAME2
from inserted i
where i.DPNAME2 NOT in (select name from tld_card.dbo._group where tld_card.dbo._group.lev=1)
end
wjx_0_2001 2009-01-15
  • 打赏
  • 举报
回复
CREATE TRIGGER group_insert ON [dbo].[BASE_CUSTDEPT]
for INSERT
AS

declare @one_name varchar(30)---一级部门名称
declare @one_id bigint---一级部ID
declare @one_strid varchar(30)

declare @sec_name varchar(30)----二级部门名称
declare @sec_id bigint---二级部门ID
declare @sec_strid varchar(30)

declare @third_name varchar(30)--三级部门名称
declare @third_id bigint ---三级部门ID
declare @third_strid varchar(30)


declare @forur_name varchar(30)--四级部门名称
declare @forur_id bigint ---四级部门ID
declare @forur_strid varchar(30)

declare @tmp_name varchar(30)
declare @tmp_id bigint

--判断级别
select @one_strid=DPCODE1 from inserted
select @sec_strid=DPCODE2 from inserted
select @third_strid=DPCODE3 from inserted
select @forur_strid=DPCODE4 from inserted

set @one_id=CAST(@one_strid AS bigint)
set @sec_id=CAST(@sec_strid AS bigint)
set @third_id=CAST(@third_strid AS bigint)
set @forur_id=CAST(@forur_strid AS bigint)
-------------------------------------------------------------
--先获取当前记录数目
declare @rec_num bigint

set NOCOUNT ON












littlefangMFC 2009-01-15
  • 打赏
  • 举报
回复
不应该啊,触发器里的语句没有问题吧?贴代码比较好,看你写的文字觉得没有问题

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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