Re: Triggers - Inserted and Deleted Tables

From: Narayana Vyas Kondreddi (answer_me_at_hotmail.com)
Date: 07/15/04


Date: Thu, 15 Jul 2004 15:55:39 +0100

If you have a DML statement that affects more than one row, and if you are
using a SELECT statement to assign a variable from the 'deleted' table, your
trigger will fail - I mean, you won't get an error, but the trigger action
will be logically incorrect.

The 'deleted' table will indeed contain two rows in your case, but your
SELECT query is going to assign the variable to only one of those rows'
values.

If you want to process all rows, you should join the 'deleted' table with
whatever table, using the key values. Or if needed, you'll have to loop
through all the rows of 'deleted' table.

-- 
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Richard J" <RichardJ@discussions.microsoft.com> wrote in message
news:A54003E1-9896-4C36-8803-5C9BC7ABFDC2@microsoft.com...
Hi group,
I read the BOL concerning the inserted and deleted tables, but the text
seemed to be missing one point that I am attempting to reconcile.
Say I have the following in my trigger:
SELECT @OldBalance  = Balance
FROM deleted
I then turn around and do the following:
SELECT @Account = AccountNumber,
            @Share    = ShareNumber,
            @NewBalance = Balance
FROM inserted
I then turn around and process an overdraft table if the new balance is >= 0
and the old balance is < 0.
How does SQL Server manage the inserted and deleted tables if more than one
row is affected?  For example, the above example is a financial transaction
and I update an account holders checking and savings account.  The SELECT
from the deleted table will pull back two rows.  How does SQL Server know
which record in deleted goes with which record in inserted?  I ask this
because the trigger successfully works, but I don't understand how.
Any insight given will be most appreciated.  Thanks.
Richard


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: Really bad table design...
    ... there could be two membership fees at the start of every year ... The balance forward system simply lists all these, ... > dues are paid once a year based a pre-set date for all board members. ... >> and account activity, ...
    (microsoft.public.access.tablesdbdesign)
  • Interest rate offer "bait and switch"?
    ... I took advantage and transferred 5k to the account ... which was at a zero balance. ... started all over at 5k minus the charges etc but it didn't add up or balance ... Capital One Credit Card Offer ...
    (misc.legal)
  • Re: Egg Money
    ... In other words, if it *is* a single account, I would not really ... appeared in the -ve balance column with nothing in the +ve. ... another purchase *and* paid some money in. ... "Egg Money customers will get up to 50 days' interest-free credit on ...
    (uk.finance)
  • Re: Egg Money
    ... My wife has recently opened an Egg Money account - which I understood to ... 50-odd days interest free credit on credit card purchases. ... a +ve and a -ve balance. ...
    (uk.finance)