触发器异常处理
我写了一个触发器,每当有数据插入A时触发,并将这数据插入B表。
如果插入B时出错,如何让你能正常的插入A表。我是这么写的
如何做异常处理?
ALTER trigger [dbo].[backup_pending_job_20110120]on [dbo].[_pending_job]after insert asbegindeclare @var sysname select @var=ltrim(JobID) from inserted insert dbo._pending_job_backup_20110119(JobID,SystemID,Owner,ParentOwner,JobName,URL,IssuedTime,JobType,ActionType,SyncToGMCCPortal,CreateTime,LastUpdate,GMCCPortalID,GMCCAppID) select JobID,SystemID,Owner,ParentOwner,JobName,URL,IssuedTime,JobType,ActionType,SyncToGMCCPortal,CreateTime,LastUpdate,GMCCPortalID,GMCCAppID from _pending_job where JobID=@var end
create trigger insert_In on In_Productafter insertasbegindeclare @code1 varchar(10) --对inserted中的procode进行判断select @code1 = procode from insertedif @code1 not in (select procode from ProductStore) --入库产品在库存表中不存在时 begin insert into ProductStore select * from inserted endelse --入库产品在库存中存在时 begin update ProductStore set quantity = quantity + (select quantity from inserted where procode = @code1) endendgo
[解决办法]