| Author |
Topic |
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-17 : 18:16:33
|
| What is the correct syntax to write a trigger to update a CUST_BALANCE column in a CUSTOMER table when a new invoice record is entered?I have the command if using Oracle:CREATE OR REPLACE TRIGGER TRG_UPDATECUSTBALANCEAFTER INSERT ON INVOICEFOR EACH ROWBEGIN UPDATE CUSTOMER SET CUST_BALANCE = CUST_BALANCE + :NEW.INV_AMOUNT WHERE CUST_NUM = :NEW.CUST_NUM;END;But this isn't helpful for SQL Server. Any ideas? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-17 : 20:20:37
|
| Not sure what DB2 is. I'm VERY new to this. I'm in an online class and our text uses Oracle to explain commands, but we're all working with SQL Server. I've got a question in to the intructor and his TA's and every other student in the class, but not a single person has responded with anything helpful for triggers. I'd like it spelled out so I can learn how to create this trigger and a few others. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-17 : 21:08:34
|
| Thanks for trying to help. I've seen the microsoft site, but I only started learning sql last week, so all the syntax it gives still leaves me scratching my head. I'm not sure what HAS to be included in the trigger and what's optional. I'll keep at it though and hope the instructor gets on the board and answers the question soon. |
 |
|
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-17 : 23:52:59
|
| Okay, I think I have the syntax for the trigger to work, but now I'm running into a problem with my SQL statement. Is there SQL Server syntax that does the same thing as :New in Oracle? If I want to use the statement:UPDATE CUSTOMER SET CUST_BALANCE = CUST_BALANCE + :NEW.INV_AMOUNT WHERE CUST_NUM = :NEW.CUST_NUM;SQL Server doesn't recognize the :New portion. Does that make sense? I want a new balance to be calculated with the addition of a new invoice for a given customer. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-18 : 15:25:39
|
in sql server the corresponding statement would beUPDATE cSET c.CUST_BALANCE = c.CUST_BALANCE + i.INV_AMOUNTFROM CUSTOMER cINNER JOIN INSERTED iON i.CUST_NUM = c.CUST_NUM; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2012-02-18 : 16:29:51
|
| In SQL Server triggers you have the inserted table which contain the new values (inserted rows or updated rows) and the deleted table which contains the old values (deleted rows or previous values for updated rows), and just like any other table you'd join them in to get the rows that were affected by the operation that fired the trigger.--Gail ShawSQL Server MVP |
 |
|
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-18 : 16:58:03
|
| Beautiful! Thanks visakh16! That was what I was looking for. And Gail, thanks for the additional explanation. That makes sense now! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-18 : 18:05:04
|
quote: Originally posted by lisateg Not sure what DB2 is. I'm VERY new to this. I'm in an online class and our text uses Oracle to explain commands, but we're all working with SQL Server. I've got a question in to the intructor and his TA's and every other student in the class, but not a single person has responded with anything helpful for triggers. I'd like it spelled out so I can learn how to create this trigger and a few others.
Ask for your money backBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-18 : 18:48:19
|
| No kidding. While I'm at it, I created another trigger, this time to update the quantity on hand for each product sold after a new line is added to my LINE table. Here's my trigger:CREATE TRIGGER TRG_LINE_PRODON LINEAFTER INSERT, UPDATEASUPDATE PSET P.P_QOH=(P.P_QOH - L.LINE_UNITS)FROM PRODUCT P, LINE LWHERE P.P_CODE=L.P_CODE;But when I check it, it subtracts twice the amount!? Is there something wrong with my command somewhere that anyone can see? |
 |
|
|
lisateg
Starting Member
10 Posts |
Posted - 2012-02-18 : 20:39:52
|
| Okay, figured it out. CREATE TRIGGER TRG_LINE_PRODON LINEAFTER INSERTASUPDATE PSET P.P_QOH=(P.P_QOH - I.LINE_UNITS)FROM PRODUCT P INNER JOIN INSERTED ION I.P_CODE=P.P_CODE;Worked for me. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-20 : 12:00:23
|
quote: Originally posted by lisateg Beautiful! Thanks visakh16! That was what I was looking for. And Gail, thanks for the additional explanation. That makes sense now!
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|