After delete Trigger on Transaction



Please Advise ..After delete Trigger on Transaction

I have 3 table

1. JournalParent
1. counter int (used for relation to journalDetail)
2. date datetime

2. journalDetail
1. counter int (used for relation to journalparent)
2. account nchar
3. debit money
4. credit money

3. account
1. account nchar
2. balance money


then, i create form to maintain journal transaction (insert & delete),
at that form i used transaction mode so if both journalDetail &
journalParent will be dill properly

and then i create 2 trigger to add or minus balance on account table

1. *** Trigger after inserted to add account balance
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [AddAccountBalance] ON [dbo].[journalChild] AFTER INSERT
AS
BEGIN
UPDATE accountBalance
SET balance = balance + (SELECT debit-credit FROM INSERTED)
WHERE code = (SELECT account FROM INSERTED)
END

*** this trigger working well (update balance on account table
properly)

2. *** Trigger after delete to minus account balance
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [AddAccountBalance] ON [dbo].[journalChild] AFTER DELETE
AS
BEGIN
UPDATE accountBalance
SET balance = balance - (SELECT debit-credit FROM DELETED)
WHERE code = (SELECT account FROM DELETED)
END

*** this trigger not working well (error message SUBQUERY RETURED MORE
THAN 1 VALUE)


I add 1 transaction like below:

counter : 1
date : 24 december 2005

account debit credit
001 1,000
002 1,000

When i add this transaction after inserted trigger run properly (so
account balance for 001=1,000 and 002= -1000)

but when i delete this transaction, come out error message "SUBQUERY
RETURED MORE THAN 1 VALUE"

***(below delete command from form)
try
{
myAtomCmd.CommandText = string.Format("DELETE FROM JournalParent WHERE
counter = '{0}'", myRecordKey);
myAtomCmd.ExecuteNonQuery();
myAtomCmd.CommandText = string.Format("DELETE FROM JournalChild
WHERE counter = '{0}'", myRecordKey);
myAtomCmd.ExecuteNonQuery();
myAtom.Commit();
}
....so on



So i open journalDetail table and delete record one by one the after
delete trigger run properly

Please advise from you all... thank



regards
Sandi Antono

.



Relevant Pages

  • Re: Design Question - Accounts/Transactions
    ... 'Balance' column to the transaction, ... Further, when you need to compute the balance for an account, you only need to scan the part of the table for that account. ... A trigger is certainly a possibility. ...
    (microsoft.public.sqlserver.programming)
  • Re: SQL 7 vs. 2000 issue -trigger and nulls
    ... >We're having trouble with a trigger updating some tables. ... course be locked by the current transaction, ... locking data and updating rows when the COMMIT inside the trigger is ... I seriously hope that SQL Server 7.0 simply disregarded these two ...
    (microsoft.public.sqlserver.mseq)
  • Re: Auditing changes made to table design (syscolumns table)
    ... With out using profiler the only way I know of is to get this info from the ... Keep in mind I know the database and the tables I would ... I attempted to write a trigger to log when a change was made to the ... > certain criteria against every transaction. ...
    (microsoft.public.sqlserver.security)
  • Re: Alternative to Transactional Replication
    ... Basically the insert is wrapped in a transaction when you fire a trigger. ... Looking for a SQL Server replication book? ... with various WAN links. ...
    (microsoft.public.sqlserver.replication)
  • Re: Transaction log and trigger
    ... You would have to "recover" the database to re-create the trigger. ... and then apply transaction log which was backed up for the next hour(by this ...
    (microsoft.public.sqlserver.server)