Procedure Working But Not Solving Mutating Table [message #575804] |
Mon, 28 January 2013 01:28 |
|
lukedba
Messages: 3 Registered: January 2013
|
Junior Member |
|
|
Hi, I'm working on a database for a chiropractor in Bath. Am using Oracle 11g to model the design but may port everything over to MySQL later. I have made a procedure below that works perfectly (it updates one table based on values from a column from another table) but the problem is that I have not been able to execute it as a batch process. It only only updates based on the 'last' entry in the invoice table using the MAX function. Here is the code.
create or replace
PROCEDURE update_customer_totalamt
AS
v_custid NUMBER;
v_amount NUMBER;
BEGIN
SELECT AMOUNT into v_amount
FROM invoice
WHERE invoiceid = (SELECT MAX(invoiceid) FROM invoice);
SELECT CUSTID into v_custid
FROM invoice
WHERE invoiceid = (SELECT MAX(invoiceid) FROM invoice);
UPDATE customer
set TOTALAMT = TOTALAMT + v_amount
where CUSTID = v_custid;
COMMIT;
END;
What I wanted to do was trigger the invoice table to automatically update the customer table but that resulted in a mutating table error. Also many forums online seemed to negate the need for triggers in Oracle claiming that procedures were a better solution. All I want to do now is to run the above code as a batch each day at 7pm and have it operate on each row in the invoice table that was added between 7am and 7pm of current day.
Am new to this forum but not new to SQL. Any help is greatly appreciated. I am running Oracle on Windows Server 2008.
Thanks in advance, Luke
|
|
|
Re: Procedure Working But Not Solving Mutating Table [message #575808 is a reply to message #575804] |
Mon, 28 January 2013 01:50 |
|
Michel Cadot
Messages: 68643 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
It is not clear what your procedure should do.
Please clarify and specify it.
If you want to update CUSTOMER table with all invoices that have occurred during the current day, just execute:
MERGE into CUSTOMER c
using (select CUST_ID, sum(AMOUNT) amount from INVOICE
where <invoice_date column> between trunc(sysdate)+7/24 and trunc(sysdate)+19/24
group by CUST_ID) i
on (i.CUST_ID = c.CUST_ID)
when matched then update set TOTALAMT = TOTALAMT + i.AMOUNT
when not matched then insert (CUST_ID, TOTALAMT) values (i.CUST_ID, i.AMOUNT)
/
You can put this in a job that will be executed each day at 7PM.
Regards
Michel
[Updated on: Mon, 28 January 2013 01:53] Report message to a moderator
|
|
|
|
|
|
|
Re: Procedure Working But Not Solving Mutating Table [message #575879 is a reply to message #575861] |
Mon, 28 January 2013 13:06 |
|
lukedba
Messages: 3 Registered: January 2013
|
Junior Member |
|
|
The MERGE solution seems to be closest to what I need. I had first thought to have the amounts as they are in the invoice table and just run queries on it obviously to get the total amount for each customer at any given time. However the client did specify that they wanted to see a 'running total' of how much eache customer has spent in the customer table so that is what I set out to do. My first attempt 2 days ago was to write a trigger which failed with a mutating table error. I found out quickly online that triggers are not always the best solution but it seemed to me to be the most logical. I then decided to write a procedure and use it as a batch process as I would have done many years ago when I was working on a Novel network doing things like backups. My procedure worked but only for the last record entered into the invoice table. So as it stands the MERGE solution updates my amount in the customer table accurately so I and the client can see the running total for each customer.
My customer table has the fields CUSTID, FIRSTNAME, LASTNAME, STREETADDRESS, POSTCODE, TELEPHONE, INJURY, NUMVISITS, TOTALAMT.
The client has been working in a clinic and only now just starting in her own clinic and all of her customer contact info is in a notebook. So this model will allow here to get contact info as well as sales data and cross reference it with location and type of injury so she is happy with that as a start. I am not doing any coding for an electronic payment system only the invoice table for legacy invoice data.
Regards Luke
|
|
|