SQL Trigger After Insert not Updating correctly -
i've inherited trigger use populate field based on related row. basically, sequence of 10, 20, 30 gets predecessor popluated 10, predecessor 0, 20, predecessor 10, etc. max(seq) seq < currentrowseq.
pretty straightforward, except when our sequence inserted, predecessors populating 0s. if piece out code , separately, numbers expect. figure must wrong in trigger firing. note trigger firing on child table insert. predecessor field in child ud table sequence in parent table. wonder if order in writes happening problem. i'm not sure. here's trigger:
alter trigger [erp].[trgrjoboperudinsert] on [erp].[joboper_ud] after insert set nocount on declare @jobnum varchar(100), @assemblyseq int, @oprseq int, @foreignsysrowid varchar(100), @pred varchar(100) set @pred = '' declare preds cursor local select isnull(predecessor_c, ''), foreignsysrowid inserted open preds fetch next preds @pred, @foreignsysrowid while @@fetch_status = 0 begin select @jobnum = jobnum, @assemblyseq = assemblyseq, @oprseq = oprseq joboper (nolock) sysrowid = @foreignsysrowid if @pred = '' or @pred = '0' begin update erp.joboper_ud set predecessor_c = ( select isnull( max(oprseq) ,0) joboper joboper.jobnum = @jobnum , joboper.assemblyseq = @assemblyseq , joboper.oprseq < @oprseq ) foreignsysrowid = @foreignsysrowid end fetch next preds @pred, @foreignsysrowid end close preds deallocate preds set nocount off
the cursor added, exact same minus cursor before. i've played trigger in every way can think of achieve same result. breaks down @ update in predecessor_c = (select isnull(...)...). can put want isnull replacement , every time. why getting null inner select? said, if run select after fact result expect.
Comments
Post a Comment