triggers

  • Hello everyone, i have 2 tables table1: "Transactions" and table2: "TransactionLog",

    In table1 i have the following fields::::::::::::::::

             

     TransactionID ,

     BuyerID,

     TransactionTypeID,

     TransactionStatusCode,

     PaymentModeID,

     SubAccountNumber,

     IsDebit,

     TransactionDate,

     DispatchDate,

     ExpireDate,

     LastStatusUpdate,

     TotalAmount,

     Discount,

     Tax,

     PaymentServiceFees,

     InstrumentNumber,

     Comments

    In table2 i have following fields::::::::::::::::::

    TransactionID,

    TransactionStatusCode,

    LogDate,

    ErrorCode,

    Comments.

    ......................................................................

     Now i want to write a trigger for Insert and Update on table1. As if any insertion is made in the table1 or update anything in tha table1 , for this a new entry is always added in table2. ( for making log activity)

    i never used triggers so plz give me solution with code

    its very urgent

    plz  plz plz reply me asap

  • something like:

    CREATE TRIGGER tr1 ON Transactions

    AFTER INSERT,UPDATE AS

    INSERT INTO TransactionLog(TransactionID, TransactionStatusCode, LogDate, ErrorCode, Comments )

    --the table [INSERTED] is  the data being affected by insert/update:

    SELECT TransactionID, TransactionStatusCode, LogDate, ErrorCode, Comments FROM [INSERTED]

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Only I would use GetDate() for LogDate. So, it would seems like the following:

    CREATE TRIGGER tr1 ON Transactions

    AFTER INSERT,UPDATE AS

    INSERT INTO TransactionLog(TransactionID, TransactionStatusCode, LogDate, ErrorCode, Comments )

    SELECT TransactionID, TransactionStatusCode, GetDate(), ErrorCode, Comments FROM [INSERTED]

    Matter of taste, really.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply