Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Wobeca
Starting Member
12 Posts |
Posted - 2011-05-27 : 15:38:06
|
| Hi all,I have a table called 'TblPayments'.This table contains a 'PayID', a 'PayedAmount' (what's been payed by customer), another 'AmountToBePayed' (what needs to be paid) and an 'Order' (foreign key to orderID in TblOrders).I want to log the actions on this table, by putting in a new record in a table 'TblLog' which contains 'LogID, LogDate, Action, PayID, OldValue, NewValue.When the amount in the TblPayments.PayedAmount is adapted, a new record in TblLogs should be inserted.For example:The amount to be payed is 200, the customer pays 300 --> Log (debet)The amount to be payed is -100 because he payed 300 --> Log (credit)Now, how do I find the last payed amount (OldValue), if another amount (NewValue) is entered?Can this be done with a so called "temporary table"? If so, how does it work...?Thanks in advance!Wobeca |
|
|
Wobeca
Starting Member
12 Posts |
Posted - 2011-05-27 : 15:39:43
|
| Sorry, I forgot to mention that I want to do this with a trigger.So if an amount is inserted/adapted, an insert/update trigger should be fired to insert the data in TblLog... |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2011-05-28 : 15:38:50
|
Wobeca,try this. this is trigger only in case of invoice amount change. in case of invoice delete or insert trigger is not charged.create table tbl_invoice(id int identity(1,1),invoice_id int,amount money);create table tbl_invoice_log(logID int identity(1,1),LogDate smalldatetime,log_action char(20),invoice_id int,current_amount money,new_amount money,change_amount money);insert into tbl_invoiceselect 51341, 431.53 union allselect 51342, 1251.00 union allselect 51345, 52--(3 row(s) affected)--only update triggercreate trigger trg_tblInvoice_updateon tbl_invoiceafter updateasinsert into tbl_invoice_log (LogDate, Log_action, invoice_id, current_amount, new_amount, change_amount)select getdate() as LogDate ,'UPDATE' as Log_action ,d.invoice_id as Invoice_Id ,d.amount as current_amount ,i.amount as new_amount ,i.amount-d.amount as change_amountfrom inserted as i, deleted as dwhere i.invoice_id = d.invoice_id --intial dataselect * from tbl_invoiceselect * from tbl_invoice_log-- perform testupdate tbl_invoiceset amount = 1300whereinvoice_id = 51342--after changeselect * from tbl_invoiceselect * from tbl_invoice_log |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-05-28 : 23:20:29
|
| Please post real DDL. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. We have asked you to follow basic netiquette before; why are you still being rude? >> I have a table called 'TblPayments'. <<Unless this table deals with furniture, take that silly “tbl-” prefix off the name. This is SQL and not 1960's BASIC. Let's try to fix your narrative.CREATE TABLE Something_Payments(order_nbr CHAR(10) NOT NULL REFERENCES Orders(order_nbr), payment_nbr INTEGER NOT NULL, actual_payment_date DATE, expected_payment_date DATE NOT NULL, PRIMARY KEY (order_nbr, payment_nbr), actual_payment_amt DEFAULT 0.00 DECIMAL (10,2) NOT NULL, expected_payment_amt DECIMAL (10,2) NOT NULL);>> I want to log the actions on this table, by putting in a new record [sic] in a table 'TblLog' [sic]which contains 'LogID, LogDate, Action, PayID, OldValue, NewValue.<<WHY? If you will learn to design a schema properly, there is no need for this kind of kludge. Triggers? We hate triggers because they are procedural and not declarative. And how can you confuse records and rows? They are totally different concepts. Design the table to look like a payment coupon book. Keep the entire set of payment histoty one place. What you do need is a VIEW that will summarize the payments made to date, not this 1950's ledger book. --CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|
|
|
|