CREATE OR REPLACE TRIGGER TRG_INVOICE
AFTER INSERT
ON INVOICE
FOR EACH ROW
DECLARE
V_SERVICE_COST FLOAT;
V_SPARE_PART_COST FLOAT;
V_TOTAL_COST FLOAT;
V_INVOICE_DATE DATE;
V_DUEDATE DATE;
V_REQ_ID INVOICE.SERVICE_REQ_ID%TYPE;
V_INV_ID INVOICE.INVOICE_ID%TYPE;
BEGIN
V_REQ_ID := :NEW.SERVICE_REQ_ID;
V_INV_ID := :NEW.INVOICE_ID;
SELECT SUM(S.SERVICE_COST) INTO V_SERVICE_COST
FROM INVOICE I, SERVICE_REQUEST SR, SERVICE S, SERVICE_REQUEST_TYPE SRT
WHERE I.SERVICE_REQ_ID = SR.SERVICE_REQ_ID
AND SR.SERVICE_REQ_ID = SRT.SERVICE_REQ_ID
AND SRT.SERVICE_ID = S.SERVICE_ID
AND I.SERVICE_REQ_ID = V_REQ_ID;
SELECT SUM(SP.PRICE) INTO V_SPARE_PART_COST
FROM INVOICE I, SERVICE_REQUEST SR, SERVICE S, SERVICE_REQUEST_TYPE SRT,
SPARE_PART_SERVICE SRP,
SPARE_PART SP
WHERE I.SERVICE_REQ_ID = SR.SERVICE_REQ_ID
AND SR.SERVICE_REQ_ID = SRT.SERVICE_REQ_ID
AND SRT.SERVICE_ID = S.SERVICE_ID
AND S.SERVICE_ID = SRP.SERVICE_ID
AND SRP.SPARE_PART_ID = SP.SPARE_PART_ID
AND I.SERVICE_REQ_ID = V_REQ_ID;
V_TOTAL_COST := V_SERVICE_COST + V_SPARE_PART_COST;
SELECT SYSDATE INTO V_INVOICE_DATE FROM DUAL;
SELECT ADD_MONTHS(SYSDATE, 1) INTO V_DUEDATE FROM DUAL;
UPDATE INVOICE
SET COST_SERVICE_REQ = V_SERVICE_COST, COST_SPARE_PART =
V_SPARE_PART_COST,
TOTAL_BALANCE = V_TOTAL_COST, PAYMENT_DUEDATE = V_DUEDATE, INVOICE_DATE =
V_INVOICE_DATE
WHERE INVOICE_ID = V_INV_ID;
END;
I'm trying to calculate some columns after the user inserts a row.
Using the service_request_id I want to calculate the service/parts/total cost. Also, I would like to generate the creation and due dates. But, I keep getting
INVOICE is mutating, trigger/function may not see it
Not sure how the table is mutating after the insert statement.
Not sure how the table is mutating after the insert statement.
Imagine a simple table:
create table x(
x int,
my_sum int
);
and an AFTER INSERT FOR EACH ROW trigger, similar to yours, which calculates a sum of all values in the table and updates my_sum column.
Now imagine this insert statement:
insert into x( x )
select 1 as x from dual
connect by level <= 1000;
This single statement basically inserts 1000 records, each one with 1 value, see this demo: http://sqlfiddle.com/#!4/0f211/7
Since in SQL each individual statement must be ATOMIC (more on this here: Statement-Level Read Consistency, Oracle is free to perform this query in any way as long as the final result is correct (consistent). It can save records in the order of execution, maybe in reverse order, it can divide the batch into 10 threads and do it in parallel.
Since the trigger is fired individually after inserting each row, and it cannot know in advance the "final" result, then considering the above all the below results are possible depending on "internal" method choosed by Oracle to execute this query. As you see, these result do not meet the definition of consistency. And Oracle prevents this issuing mutating table error.
In other words - your assumption are bad and your design is flawed, you need to change it.
| X | MY_SUM |
|---|--------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
...
...
or maybe :
| X | MY_SUM |
|---|--------|
| 1 | 1000 |
| 1 | 1000 |
| 1 | 1000 |
| 1 | 1000 |
| 1 | 1000 |
| 1 | 1000 |
| 1 | 1000 |
...
or maybe:
| X | MY_SUM |
|---|--------|
| 1 | 4 |
| 1 | 8 |
| 1 | 12 |
| 1 | 16 |
| 1 | 20 |
| 1 | 24 |
| 1 | 28 |
...
...
Related
I have two tables; on update of table 1 (cars bought), I need to update the other table with the sum of total cars bought by a specific customer.
When I try it, I encountered a mutating trigger. I have tried to convert it to a compound trigger, however I am encountered a vast number of errors, null index etc.
detail_table:
+-----------+---------+--------+------+------+
| customore | car | number | cost | sold |
+-----------+---------+--------+------+------+
| josh | mustang | 2 | 5 | y |
| josh | ford | 3 | 2 | y |
| josh | tesla | 1 | 3 | n | -->to update to y
| john | chevy | 4 | 1 | y |
| john | chevy | 5 | 2 | y |
+-----------+---------+--------+------+------+
On update of sold from n to y, the rows must roll up and sum into this summary table
summary_table
+----------+------------+------------+
| customer | total cars | total cost |
+----------+------------+------------+
| josh | 5 | 7 | -- > before update on detail
+----------+------------+------------+
+----------+------------+------------+
| customer | total cars | total cost |
+----------+------------+------------+
| josh | 6 | 10 | -- > after update on detail
+----------+------------+------------+
In the end when the user updates n to y for josh.total cars supposed to become 1 and total cost 10
trigger code
CREATE OR REPLACE TRIGGER update_summary_table
AFTER UPDATE ON detail_table FOR EACH ROW
referencing old as old new as new
begin
for i in (select sum(number) s_car, sum(cost) s_cost
from detail_table
where customer = :new.customer
and sold = 'y') loop
update summary_table
set total_cars = i.s_car,
total_cost = i.s_cost
where customer = :new.customer;
end loop;
end;
end update_summary_table;
Why would you use a for loop for this? Just use a single update:
update summary_table
set total_cars = coalesce(total_cars, 0) + :new.number - :old.number,
total_cost = coalesce(total_cost, 0) + :new.cost - :old.cost
where customer = :new.customer;
This does an incremental change to the summary rather than entirely recalculating the row.
The use of coalesce() is just in case no default value is given for new rows.
There is a table containing hierarchical data, e.g.:
| table "attribute_instances" |
+----+----------+------------+---------------+----------+
| id | tree_ref | parent_ref | attribute_ref | data_ref |
+----+----------+------------+---------------+----------+
| 1 | 1 | -1 | 1 | 1 |
| 2 | 1 | 1 | 2 | 2 |
| 3 | 2 | -1 | 1 | 3 |
| 4 | 2 | 3 | 2 | 2 |
It contains many separate trees (see tree_ref), each of them instantiating some attributes (see attribute_ref) and have a data reference data_reference, where data might be referenced in other trees, too.
Now, those trees should be merged into a single tree, in which (by now) up to 5 attributes may be chosen as level for that tree, e.g.:
attribute => level
------------------
2 => 1
1 => 2
What I need is one or more queries, that collects the data from table attribute_instances and gives a result as follows:
| table "merged_attribute_instances" |
+----+------------+---------------+----------+
| id | parent_ref | attribute_ref | data_ref |
| 5 | -1 | 2 | 2 |
| 6 | 5 | 1 | 1 |
| 7 | 5 | 1 | 3 |
This is the desired merged tree:
id:5 - data_ref:2
id:6 - data_ref:1
id:7 - data_ref:3
Note, that attribute_ref = 2 occurs only once in the resulting tree, as all instances of it have same data_ref value (that is 2).
I've tried some joins like
select *
from attribute_instances a
join attribute_instances b on a.tree_ref = b.tree_ref
But that seems to me being bad for having user-defined tree depth. I'm sure there is a better solution.
UPDATE: I should add, that table merged_attribute_instances is a temporary table. And the collecting query is iterated with for..do. In the loop the collected attribute_instances are then added to the temporary table.
OK, then use this:
SET TERM ^ ;
create or alter procedure GETTREENODES
returns (
ID integer,
TREE_REF integer,
PARENT_REF integer,
ATTRIBUTE_REF integer,
DATA_REF integer)
as
declare variable DATAREFEXISTS varchar(4096);
begin
DATAREFEXISTS = ',';
for
Select id, tree_ref, parent_ref, attribute_ref, data_ref from attribute_instances
into :id, :tree_ref, :parent_ref, :attribute_ref, :data_ref
do begin
IF (position(',' || data_ref || ',', DATAREFEXISTS) =0) THEN
begin
suspend;
DATAREFEXISTS = DATAREFEXISTS || data_ref || ',' ;
end
end
end^
SET TERM ; ^
/* Following GRANT statetements are generated automatically */
GRANT SELECT ON ATTRIBUTE_INSTANCES TO PROCEDURE GETTREENODES;
/* Existing privileges on this procedure */
GRANT EXECUTE ON PROCEDURE GETTREENODES TO SYSDBA;
Call it like this:
Select * from gettreenodes
order by tree_ref, parent_ref
Given data structure:
I have the following table My_List, where Sup_ID is Primary Key
My_List
+--------+----------+-----------+
| Sup_ID | Sup_Name | Sup_Code |
+--------+----------+-----------+
| 1 | AA | 23 |
| 2 | BB | 87 |
| 3 | CC | 90 |
+--------+----------+-----------+
And the following table _MyList_details, where Buy_ID is Primary Key and Sup_ID is Foreign Key points at My_List.Sup_ID
My_List_details
+--------+--------+------------+------------+------------+
| Buy_ID | Sup_ID | Sup_Detail | Max_Amount | Min_Amount |
+--------+--------+------------+------------+------------+
| 23 | 1 | AAA | 1 | 10 |
| 33 | 2 | BBB | 11 | 20 |
| 43 | 3 | CCC | 21 | 30 |
+--------+--------+------------+------------+------------+
Finally, I have the table My_Sequence as follow:
My_Sequence
+-----+------+
| Seq | Name |
+-----+------+
| 4 | x |
| 5 | y |
| 6 | z |
+-----+------+
---------------------------------------------------
Objectives
Write PL/SQL script to:
Using a cursor, I need to copy My_List records and re-insert it with the new Sup_ID copied from My_Sequence.Seq.
I need to copy My_List_details records and re-insert them with the new Sup_ID foreign key.
------------------------------------------------------------------------------
Expected Outcome
My_List
+--------+----------+----------+
| Sup_ID | Sub_Name | Sub_Code |
+--------+----------+----------+
| 1 | AA | 23 |
| 2 | BB | 87 |
| 3 | CC | 90 |
| 4 | AA | 23 |
| 5 | BB | 87 |
| 6 | CC | 90 |
+--------+----------+----------+
My_List_details
+--------+--------+------------+------------+------------+
| Buy_ID | Sup_ID | Sub_Detail | Max_Amount | Min_Amount |
+--------+--------+------------+------------+------------+
| 23 | 1 | AAA | 1 | 10 |
| 33 | 2 | BBB | 11 | 20 |
| 43 | 3 | CCC | 21 | 30 |
| 53 | 4 | AAA | 1 | 10 |
| 63 | 5 | BBB | 11 | 20 |
| 73 | 6 | CCC | 21 | 30 |
+--------+--------+------------+------------+------------+
What I have started with is the following:
DECLARE
NEW_Sup_ID Sup_ID%type := Seq;
c_Sup_Name Sup_Name%type;
c_Sup_Code Sup_Code%type;
c_Buy_ID Buy_ID%type;
c_Sup_Detail Sup_Detail%type;
c_Max_Amount Max_Amount%type
c_My_Min_Amount Min_Amount%type
CURSOR c_My_List
IS
SELECT * FROM My_List;
CURSOR c_My_List_details
IS
SELECT * FROM My_List_details
BEGIN
FETCH c_My_List INTO NEW_Sup_ID, c_Sup_Name, c_Sup_Code;
INSERT INTO My_List;
FETCH c_My_List_details INTO c_Buy_ID, NEW_Sup_ID, c_Sup_Detail, c_Max_Amount, c_Min_Amount
INSERT INTO My_List_details
END;
/
Aside from the syntax errors, I do not see my script copy row by row and insert them to both tables accordingly. Further, the number of My_Sequence records is bigger than the number of My_List records. So what I need is, if My_List records are 50, I need the script to copy the first 50 Seq from My_Sequence.
---------------------------------------------------------------------------------
Question
How to achieve this result? I have searched and found Tom Kyte for cascade update but I am not sure if I do need to use this package, I am a bit beginner in PL/SQL and it is a bit complicated for me to utilize such a comprehensive package. Further, it's for cascade update and my case is about re-insert. I'd appreciate any help
The following Sql Statements will perform the task on the schema defined at this SqlFiddle. Note that I have changed a couple of field and table names - because they clash with Oracle terms. SqlFiddle seems to have some problems with my code, but it has been tested on another (amphibious) client which shall remain nameless.
The crucial point (As I said in my comments) is deriving a rule to map old sequence number to new. The view SEQUENCE_MAP performs this task in the queries below.
You may be disappointed by my reply because it depends upon there being the exact same number of sequence records as LIST/LIST_DETAILS, and hence it can only be run once. Your final PL/SQL can perform the necessary checks, I hope.
Hopefully it is a matter of refining the sequence_map logic to get you where you want to be.
Avoid using cursors; ideally when manipulating relational data you need to think in terms of sets of data rather than rows. This is because if you use set-thinking Oracle can do its magic in optimising, parallelising and so-on. Oracle is brilliant at scaling up - If a table is split over multiple disks, for example, it may process your request with data from the multiple disks simultaneously. If you force it into a row-by-row, procedural logic you may find that the applications you write do not scale up well.
CREATE OR REPLACE VIEW SEQUENCE_MAP AS (
SELECT OLD_SEQ, NEW_SEQ FROM
(
( SELECT ROWNUM AS RN, SUP_ID AS OLD_SEQ FROM
(SELECT SUP_ID FROM LIST ORDER BY SUP_ID) ) O
JOIN
( SELECT ROWNUM AS RN, SUP_ID AS NEW_SEQ FROM
(SELECT SEQ AS SUP_ID FROM SEQUENCE_TABLE ORDER BY SEQ) ) N
ON N.RN = O.RN
)
);
INSERT INTO LIST
(
SELECT
NEW_SEQ, SUB_NAME, SUB_CODE
FROM
SEQUENCE_MAP
JOIN LIST L ON
L.SUP_ID = SEQUENCE_MAP.OLD_SEQ
);
INSERT INTO LIST_DETAILS
(
SELECT
BUY_ID, NEW_SEQ, SUB_DETAIL, MAX_FIELD, MIN_FIELD
FROM
SEQUENCE_MAP
JOIN LIST_DETAILS L ON
L.SUP_ID = SEQUENCE_MAP.OLD_SEQ
);
I would do 2 inner loops, and search the next sequence to use.
I imagine the new buy_id is assigned via trigger using a sequence, or something equivalent, else you'll have to generate it in your code.
I have no Oracle database available to test it, so don't pay attention to syntax.
DECLARE
NEW_Sup_ID Sup_ID%type := Seq;
c_Sup_ID Sup_ID%type := Seq;
c_Sup_Name Sup_Name%type;
c_Sup_Code Sup_Code%type;
c_Buy_ID Buy_ID%type;
c_Sup_Detail Sup_Detail%type;
c_Max_Amount Max_Amount%type;
c_My_Min_Amount Min_Amount%type;
CURSOR c_My_List
IS
SELECT * FROM My_List;
CURSOR c_My_List_details
IS
SELECT * FROM My_List_details where sup_id=c_Sup_ID;
BEGIN
for c_My_List IN c_Sup_ID, c_Sup_Name, c_Sup_Code loop
select min(seq) from My_sequence into NEW_Sup_ID;
INSERT INTO My_List (sup_id,...) values (NEW_Sup_ID,...);
for c_My_List_details IN c_Buy_ID, NEW_Sup_ID, c_Sup_Detail, c_Max_Amount, c_Min_Amount loop
INSERT INTO My_List_details (sup_id, ...) values (NEW_Sup_ID,...);
end loop;
deelte from from My_sequence where seq= NEW_Sup_ID;
end loop;
commit;
END;
/
I created a scholarship database with and apply table
applyid | studid | gpa | other | sch_id | date | sem | sy
---------+-----------+-----------+-------+----------+------------+-----+----
1 | 2010-0000 | 1.5 | | 1 | | |
2 | 2010-0001 | 1.5 | | 7 | 2014-03-13 | |
3 | 2010-0003 | | | 1 | 2014-03-13 | |
4 | 2010-0003 | | | 1 | 2014-03-13 | |
5 | 2010-0003 | | | 1 | 2014-03-13 | |
2308 | 2012-0004 | 1.5 | | 1 | 2014-03-19 | |
4593 | 2012-0004 | 1.5 | | 1 | 2014-03-19 | |
4596 | 2012-0004 | 1.5 | | 1 | 2014-03-19 | |
4597 | 2012-0004 | 1.5 | | 1 | 2014-03-19 | |
(9 rows)
and currently working on this trigger function that checks if a particular student has either a grade of INC, DRP, 5.00.
CREATE FUNCTION fail_check() RETURNS TRIGGER AS $$
DECLARE
one RECORD;
two RECORD;
BEGIN
SELECT * INTO one FROM grade, registration;
IF (SELECT COUNT(g.grade)::int
FROM grade g
INNER JOIN registration r ON r.grade_id = g.grade_id
WHERE g.grade IN ('INC', 'DRP', '5.00')
AND studid=new.studid) <= 1
THEN
SELECT studid, gpa, sch_name INTO two
FROM apply WHERE studid=new.studid;
INSERT INTO apply(studid, gpa, sch_name)
VALUES (new.studid, new.gpa, new.sch_name);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER fail
BEFORE INSERT ON apply
FOR EACH ROW
EXECUTE PROCEDURE fail_check();
but when I entered this:
INSERT INTO apply(studid, gpa, sch_name)
VALUES ('2012-0004', '1.5', 1);
The student with the student id of "2012-0004" has a grade of INC DRP and 5.00. the SELECT query works perfectly fine and returns the value 3. Since 3 is greater than 1, which is contrary to the IF statement that says IF .... <= 1, I'm expecting an error that says something like that it can't be inserted because the "student" has more than 1 grade of either INC, DRP, 5.00.
But instead I got this error:
ERROR: stack depth limit exceeded
HINT: Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.
CONTEXT: SQL statement "SELECT (SELECT COUNT(g.grade)
FROM grade g
INNER JOIN registration r ON r.grade_id = g.grade_id
WHERE g.grade IN ('INC', 'DRP', '5.00') AND studid=new.studid) <= 1" PL/pgSQL function fail_check() line 12 at IF SQL statement "INSERT INTO apply(studid, gpa, sch_name) VALUES (new.studid, new.gpa, new.sch_name)" PL/pgSQL function fail_check() line 21 at SQL statement SQL statement "INSERT INTO apply(studid, gpa, sch_name) VALUES (new.studid, new.gpa, new.sch_name)"
Where did I go wrong?? and what does this max_stack_depth exactly mean?? Which part of my code caused this max_stack_depth error??
Currently using PostgreSQL 9.3.2
Your trigger BEFORE INSERT triggers more INSERTs to the same table, which causes an endless loop. Hence the stack overflow. You seem to be under the impression that you need this in your trigger:
INSERT INTO apply(studid, gpa, sch_name)
VALUES (new.studid, new.gpa, new.sch_name);
But you don't. RETURN NEW; is enough to let the original INSERT go through.
The rest of your trigger doesn't seem to do anything useful, but that's probably just a simplification.
Given a table like below, is there a single-query way to update the table from this:
| id | type_id | created_at | sequence |
|----|---------|------------|----------|
| 1 | 1 | 2010-04-26 | NULL |
| 2 | 1 | 2010-04-27 | NULL |
| 3 | 2 | 2010-04-28 | NULL |
| 4 | 3 | 2010-04-28 | NULL |
To this (note that created_at is used for ordering, and sequence is "grouped" by type_id):
| id | type_id | created_at | sequence |
|----|---------|------------|----------|
| 1 | 1 | 2010-04-26 | 1 |
| 2 | 1 | 2010-04-27 | 2 |
| 3 | 2 | 2010-04-28 | 1 |
| 4 | 3 | 2010-04-28 | 1 |
I've seen some code before that used an # variable like the following, that I thought might work:
SET #seq = 0;
UPDATE `log` SET `sequence` = #seq := #seq + 1
ORDER BY `created_at`;
But that obviously doesn't reset the sequence to 1 for each type_id.
If there's no single-query way to do this, what's the most efficient way?
Data in this table may be deleted, so I'm planning to run a stored procedure after the user is done editing to re-sequence the table.
You can use another variable storing the previous type_id (#type_id). The query is ordered by type_id, so whenever there is a change in type_id, sequence has to be reset to 1 again.
Set #seq = 0;
Set #type_id = -1;
Update `log`
Set `sequence` = If(#type_id=(#type_id:=`type_id`), (#seq:=#seq+1), (#seq:=1))
Order By `type_id`, `created_at`;
I don't know MySQL very well, but you could use a sub query though it may be very slow.
UPDATE 'log' set 'sequence' = (
select count(*) from 'log' as log2
where log2.type_id = log.type_id and
log2.created_at < log.created_at) + 1
You'll get duplicate sequences, though, if two type_ids have the same created_at date.