在SQL触发器中使用更新的值

时间:2015-04-30 21:49:52

标签: sql plsql

我尝试制作一个简单的触发器,在付款时更新客户余额。在begin语句后面显示错误。我尝试在where语句中使用新插入的customerID,再次尝试使用几行来计算新的余额。有人可以用正确的语法帮助我吗?表位于底部。

--Whenever a payment is made, customer amount owed is adjusted
create or replace trigger paymentUpdate
after insert on payment
for each row
when (new.transactionID > 0)

declare
newAmount decimal(7,2);
currentBalance decimal(7,2);

begin
--Get the outstanding customer balance
select amountOwed into currentBalance
from customer
where customer.customerID=:new.customerID; --Does this do what I think it does?
-- ^^is any of this needed?

--Subtracts the recently made payment from the customer balance
newAmount := currentBalance - :new.amount;

--Update customer amount owed
update customer.amountOwed
set amountOwed = newAmount
where customer.customerID=:new.customerID;
dbms_output.put_line('Original account balance was: '||currentBalance);
dbms_output.put_line('Payment made was: $'||:new.amount); --Check this
dbms_output.put_line('Customer account balance is now: '||newAmount);

end paymentUpdate;
/
--commit needed?

TABLES:

INSERT INTO customer (customerID, name, address, insurance, contactInfo, customerType, licenseNumber, amountOwed)
VALUES (45124512, 'Bob Jones',  '232 Sycamore Ln.', 'Pekin', 3095555145, 'New', 'SSSSFFFYYDDD', 220.00);
INSERT INTO customer (customerID, name, address, insurance, contactinfo, customertype, licensenumber, amountOwed)
VALUES (12892222, 'Mike Tyson','100 Haters Rd.', 'Progressive', 2175555555, 'Regular', 'FGHJHHHHTYYY', 42.00);

INSERT INTO payment (customerID, transactionID, amount, method, payDate)
VALUES (45124512, 56785678, 220.00, 'Credit Card', '25-JUN-15');    
INSERT INTO payment (customerID, transactionID, amount, method, payDate)
VALUES (12892222, 68689000, 42.00, 'Cash', '25-JUN-15');

错误: 7/1 PL / SQL:SQL语句忽略 9/6 PL / SQL:ORA-00942表或视图不存在 16/1 PL / SQL:忽略SQL语句 16/17 PL / SQL:ORA-00942表或视图不存在

0 个答案:

没有答案