问题提出:
客户端录入数据记录的时候,为了检查这条记录的SN号码是否做过《接收流程》,录入的表是mlog表,接收的表是infor表。触发器代码如下:
CREATE TRIGGER [checkexistinfor] ON [dbo].[MaintainLog]
FOR INSERT
AS
if exists(select MobileType,sn from inserted where sn not in(select sn from MaintainInfor where SMTline is not null and state<>'已修复' and EventFinish=0 ))
begin
insert into MaintainInfor(InforID,MobileType,SN,MalCode,whereIs,currentPos,Analyst,State,InforDT,Number,EventFinish)
select LogID,MobileType,SN,MaiCode,Location,'在线',Analyst,DealRes,LogDT,Number,1 from inserted
end原来mlog表数据录入批量提交语句是
insert in to mlog
select * from mtemp(临时表,结构和mlog一样)现在我暂时解决方法是用一个小循环(强制10条记录就批量提交到SQL),用一个
while not EOF do
begin
with Minforquery do
begin
Close;
SQL.Clear;
tempsql:='insert into MaintainLog select * from '+tempMLogtable+' where logid=:p1';
sql.Add(tempsql);
Parameters[0].Value:=fieldquery.fieldbyname('LogID').AsString;
Prepared:=true;
ExecSQL;
end;
fieldquery.Next;
end;
这样读10条,每读一次写入一条。insert in to mlog select * from mtemp这样的语句不能执行触发器代码?用这样的语句可能一次INSERT进去10条以上记录。还是我的触发器代码写的有问题,应该循环读inserted的记录,查询infor中满足条件的记录做处理。
如何在触发器代码中写循环,请教
客户端录入数据记录的时候,为了检查这条记录的SN号码是否做过《接收流程》,录入的表是mlog表,接收的表是infor表。触发器代码如下:
CREATE TRIGGER [checkexistinfor] ON [dbo].[MaintainLog]
FOR INSERT
AS
if exists(select MobileType,sn from inserted where sn not in(select sn from MaintainInfor where SMTline is not null and state<>'已修复' and EventFinish=0 ))
begin
insert into MaintainInfor(InforID,MobileType,SN,MalCode,whereIs,currentPos,Analyst,State,InforDT,Number,EventFinish)
select LogID,MobileType,SN,MaiCode,Location,'在线',Analyst,DealRes,LogDT,Number,1 from inserted
end原来mlog表数据录入批量提交语句是
insert in to mlog
select * from mtemp(临时表,结构和mlog一样)现在我暂时解决方法是用一个小循环(强制10条记录就批量提交到SQL),用一个
while not EOF do
begin
with Minforquery do
begin
Close;
SQL.Clear;
tempsql:='insert into MaintainLog select * from '+tempMLogtable+' where logid=:p1';
sql.Add(tempsql);
Parameters[0].Value:=fieldquery.fieldbyname('LogID').AsString;
Prepared:=true;
ExecSQL;
end;
fieldquery.Next;
end;
这样读10条,每读一次写入一条。insert in to mlog select * from mtemp这样的语句不能执行触发器代码?用这样的语句可能一次INSERT进去10条以上记录。还是我的触发器代码写的有问题,应该循环读inserted的记录,查询infor中满足条件的记录做处理。
如何在触发器代码中写循环,请教
如何我mtemp临时表中有1条以上记录这样就不能执行触发器代码了。
改成读一条insert一条就可以执行触发器代码了。晕啊。
FOR INSERT
AS
insert into MaintainInfor(InforID,MobileType,SN,MalCode,whereIs,currentPos,Analyst,State,InforDT,Number,EventFinish)
select LogID,MobileType,SN,MaiCode,Location,'在线',Analyst,DealRes,LogDT,Number,1 from inserted
where sn not in(select sn from MaintainInfor where SMTline is not null and state<>'已修复' and EventFinish=0 )不过最好将IN改成Exists子语,那发生的死锁概率会小很多:
CREATE TRIGGER [checkexistinfor] ON [dbo].[MaintainLog]
FOR INSERT
AS
insert into MaintainInfor(InforID,MobileType,SN,MalCode,whereIs,currentPos,Analyst,State,InforDT,Number,EventFinish)
select LogID,MobileType,SN,MaiCode,Location,'在线',Analyst,DealRes,LogDT,Number,1 from inserted a
where not Exists(select sn from MaintainInfor where SMTline is not null and state<>'已修复' and EventFinish=0 and sn=a.sn)
还有
可以把你的操作放到存储过程中,开事务,完成两个表的批量插入