triggers [message #606579] |
Mon, 27 January 2014 03:20 |
|
danny432
Messages: 6 Registered: January 2014
|
Junior Member |
|
|
creating this trigger
create or replace
trigger trig3 after update of quantity on ZAKAZ for each row
declare
quantity1 VARCHAR(25);
begin
select quantity into quantity1 from zakaz;
update tovar set quantity = quantity - quantity1 where kod_tovar = :new.kod_tovar;
end trig3;
i want it to reduce quantity from tovar with same amount i put in zakaz but it deletes every quantity from the tovar table
|
|
|
Re: triggers [message #606580 is a reply to message #606579] |
Mon, 27 January 2014 03:29 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
That trigger will not modify tovar at all. What it will do is fail with one of these errors:
ORA-04091: table %s.%s is mutating, trigger/function may not see it
ORA-01422: exact fetch returns more than requested number of rows
You know you can reference the values of the updated row directly - you've done it in the where clause of the update - so why have you written a select to get a value you can access directly?
|
|
|
|
|
Re: triggers [message #606588 is a reply to message #606579] |
Mon, 27 January 2014 03:41 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
danny432 wrote on Mon, 27 January 2014 14:50creating this trigger
i want it to reduce quantity from tovar with same amount i put in zakaz but it deletes every quantity from the tovar table
I don't believe the trigger even worked at all. The following query has messed up everything in your code :
select quantity into quantity1 from zakaz;
Cookiemonster already pointed out the data type design problem.
However, another thing is, why VARCHAR and not VARCHAR2?
|
|
|
|
Re: triggers [message #606601 is a reply to message #606596] |
Mon, 27 January 2014 04:26 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Well the trigger in your initial post is after update, so it doesn't fire on insert does it.
So do you also have an insert trigger on zakaz?
|
|
|
|
|
|
|
|
|
Re: triggers [message #606618 is a reply to message #606617] |
Mon, 27 January 2014 08:29 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
You need to read your code more carefully, you're setting products.quantity = products.quantity - products.quantity.
I assume that last products.quantity should actually be orders.quantity.
|
|
|
|
|