Hi
i have 2 Table
first one : Customer
with 4 Fields : cst_no,cst_name,total_Debit,tot_credit
second one : Transaction
with 5 Fields : Trns_no,Trns_Date,cst_no,debit,credit
MY QUESTION:
HOW TO CREATE TRIGGER FOR UPDATE TOT_DEBIT AND TOT_CREDIT FILEDS IN
CUSTOMER TABLE FROM Transaction TABLE
Thank you 4 10211
SAEED BASUDAN (BA*****@YAHOO.COM) writes: i have 2 Table first one : Customer with 4 Fields : cst_no,cst_name,total_Debit,tot_credit second one : Transaction with 5 Fields : Trns_no,Trns_Date,cst_no,debit,credit
MY QUESTION: HOW TO CREATE TRIGGER FOR UPDATE TOT_DEBIT AND TOT_CREDIT FILEDS IN CUSTOMER TABLE FROM Transaction TABLE
Something like:
CREATE TRIGGER transtri ON Transaction FOR INSERT AS
UPDATE Customer
SET total_Debit = c.total_Debit + t.debit,
total_Credit = c.total_Credit + t.credit
FROM Customer c
JOIN (SELECT cst_no, debit = SUM(debit), credit = SUM(credit)
FROM inserted
GROUP BY cst_no) as t ON c.cst_no = t.cst_no
For simplcity, I have assumed that transactions are only added, never
updated or deleted.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Rather than attempting to use a trigger, a simpler solution might be to use
a view. I would redesign your tables something like this:
CREATE TABLE Customers (cust_no INTEGER PRIMARY KEY, cust_name VARCHAR(40)
NOT NULL UNIQUE)
CREATE TABLE Transactions (trns_no INTEGER PRIMARY KEY, trns_date DATETIME
NOT NULL, cust_no INTEGER NOT NULL REFERENCES Customers (cust_no), amount
NUMERIC (10,2))
Then create a view like this:
CREATE VIEW Customer_Transaction_Totals
AS
SELECT C.cust_no, C.cust_name,
COALESCE(T.total_debit,0) AS total_debit,
COALESCE(T.total_credit,0) AS total_credit
FROM
Customers AS C
LEFT JOIN
(SELECT cust_no,
SUM(CASE WHEN amount<0 THEN amount END) AS total_debit,
SUM(CASE WHEN amount>0 THEN amount END) AS total_credit
FROM Transactions
GROUP BY cust_no) AS T
ON C.cust_no = T.cust_no
--
David Portas
------------
Please reply only to the newsgroup
--
SAEED,
David Portas solution to use a view is a much better idea than using a
trigger to update your customer table. Triggers of this type can really
slow down your database when doing inserts and updates to a large database.
You might take a look at our videos on triggers and optimization at www.technicalvideos.net.
Best regards,
Chuck Conover www.TechnicalVideos.net
"SAEED BASUDAN" <BA*****@YAHOO.COM> wrote in message
news:49**************************@posting.google.c om... Hi
i have 2 Table first one : Customer with 4 Fields : cst_no,cst_name,total_Debit,tot_credit second one : Transaction with 5 Fields : Trns_no,Trns_Date,cst_no,debit,credit
MY QUESTION: HOW TO CREATE TRIGGER FOR UPDATE TOT_DEBIT AND TOT_CREDIT FILEDS IN CUSTOMER TABLE FROM Transaction TABLE
Thank you
"Chuck Conover" <cc******@commspeed.net> wrote in message news:<10***************@news.commspeed.net>... SAEED, David Portas solution to use a view is a much better idea than using a trigger to update your customer table. Triggers of this type can really slow down your database when doing inserts and updates to a large database. You might take a look at our videos on triggers and optimization at www.technicalvideos.net. Best regards, Chuck Conover www.TechnicalVideos.net
"SAEED BASUDAN" <BA*****@YAHOO.COM> wrote in message news:49**************************@posting.google.c om... Hi
i have 2 Table first one : Customer with 4 Fields : cst_no,cst_name,total_Debit,tot_credit second one : Transaction with 5 Fields : Trns_no,Trns_Date,cst_no,debit,credit
MY QUESTION: HOW TO CREATE TRIGGER FOR UPDATE TOT_DEBIT AND TOT_CREDIT FILEDS IN CUSTOMER TABLE FROM Transaction TABLE
Thank you
To :
Erland Sommarskog
David Portas
Chuck Conover
thank you, thank you, thank you soooooo much.
I am very happy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Darren |
last post by:
Hello,
I have some 'CREATE TRIGGER' definitions that work when cut/pasted
into SQL*Plus worksheet and execute separately but fail with a
'trigger created with compilation errors' when executed...
|
by: Lisa Tang |
last post by:
Hi,
I have a table t1 with a long raw column, and I need to create a view
v1 with a long raw column being a function ff1 of the long raw column
in t1. And I need to update t1 with reverse...
|
by: Jack |
last post by:
I created a trigger in the "source table" that will "feed" and second
table. The trigger is as follows:
CREATE TRIGGER ON dbo.FromUPS
FOR INSERT
AS
Declare @Count int
Select @Count =...
|
by: Tborn2b |
last post by:
DB2 V 7, Z/OS:
I receive an SQLCODE -104 for the following trigger :
CREATE TRIGGER TRSERED1
NO CASCADE BEFORE
INSERT
ON SEMINAR
REFERENCING
NEW AS ZUGANG
|
by: rdraider |
last post by:
I created and successfully tested a trigger on a test database. Now that I
want to put this on a production system, the create trigger statement takes
way too long to complete. I cancelled after...
|
by: FM |
last post by:
Hello:
I want to create a trigger wich performs a write-action to a text file.
I'm intending to create it with:
CREATE TRIGGER TR_ALARMA_TSPRE
AFTER INSERT ON T_ALARMA_TS
FOR EACH STATEMENT...
|
by: graju80 |
last post by:
Hey Gurus..I am trying to figures out where my syntax is incorrect in the following SQL to to create an after insert trigger... I am getting
SQL0206N "INSERTROW.MF_USERID" is not valid in the...
|
by: Andrea MF |
last post by:
Hi all,
i have a problem when creating a trigger on DB2 ver 7.2 on WINDOWS.
I have two tables MOVIMAG and BARCODE, the trigge when INSERT INTO
MOVIMAG
will be UPDATE a MOVIMAG field BARCODE...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |