Home All Groups Group Topic Archive Search About

data disappearing in a trigger

Author
14 Sep 2006 10:09 PM
rengeek33
I have a table that has a trigger that loads data into other tables.  When I
run records into this table, there is one field in particular that sometimes
loads and sometimes does not (@DstIndx ).  It is a strange aberration because
the same value in two different records may get loaded for one and not the
other. If I run the single failed record through individually (instead of
part of a number or records) it loads fine.  Since the trigger works in some
instances and at other times does not, I'm a little confused.

The trigger looks like this:

CREATE TRIGGER trig_addDistribution ON dbo.CTL_Distributions INSTEAD OF
INSERT AS

  DECLARE @SeqNum int
  DECLARE @DstIndx int
  DECLARE @DefActRecv int
  DECLARE @DocNum varchar (21)

  SELECT @SeqNum = ((SELECT Count(SEQNUMBR) FROM RM10101 WHERE DOCNUMBR =
(SELECT DocNumbr FROM Inserted)) + 1) * 16384
  SELECT @DstIndx = (SELECT ACTINDX FROM GL00105 WHERE ACTNUMST = (SELECT
Account FROM Inserted))
  SELECT @DefActRecv = (SELECT ACTINDX FROM SY01100 WHERE PTGACDSC =
'Accounts Receivable')
  SELECT @DocNum = DocNumbr FROM Inserted

  --DELETE RM10101 WHERE DOCNUMBR = @DocNum

  INSERT INTO RM10101 (POSTED, PSTGSTUS, DCSTATUS, RMDTYPAL, SEQNUMBR,
DOCNUMBR, CUSTNMBR, DSTINDX, DISTTYPE, DEBITAMT, CRDTAMNT)
    SELECT 0,3,1, DocSourc, @SeqNum, DocNumbr, CustNmbr, @DstIndx, DistType,
DebitAmt, CrdtAmnt
    FROM Inserted


  INSERT INTO RM10101 (POSTED, PSTGSTUS, DCSTATUS, RMDTYPAL, SEQNUMBR,
DOCNUMBR, CUSTNMBR, DSTINDX, DISTTYPE, DEBITAMT, CRDTAMNT)
    SELECT 0,3,1, DocSourc, @SeqNum+16384, DocNumbr, CustNmbr, @DefActRecv,
'3', CrdtAmnt, DebitAmt
    FROM Inserted

Anything you see that would cause this?

Don DeVeux

Author
14 Sep 2006 10:47 PM
Anith Sen
Based on the code you posted, your trigger doesn't consider multi-row
inserts. You may want to rewrite the inserts as:

INSERT INTO rm10101 ( posted, pstgstus, dcstatus, rmdtypal,
     seqnumbr, docnumbr, custnmbr, dstindx, disttype,
     debitamt, crdtamnt )
SELECT 0,3,1, DocSourc,
       ( SELECT COUNT( seqnumbr ) + 1
           FROM rm10101 r1
          WHERE r1.DocNumbr = i.DocNumbr ) * 16384,
        DocNumbr, CustNmbr,
       ( SELECT actindx
           FROM GL00105 g1
          WHERE g1.actnumst = i.Account ),
        DistType,
        DebitAmt, CrdtAmnt
   FROM Inserted i ;

--
Anith

AddThis Social Bookmark Button