Home » SQL & PL/SQL » SQL & PL/SQL » triggers (oracle 12c)
triggers [message #606579] Mon, 27 January 2014 03:20 Go to next message
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 Go to previous messageGo to next message
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 #606581 is a reply to message #606580] Mon, 27 January 2014 03:30 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Also please read and follow How to use [code] tags and make your code easier to read?
Re: triggers [message #606582 is a reply to message #606581] Mon, 27 January 2014 03:31 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And why declare a varchar variable for what is presumably a number column?
Re: triggers [message #606588 is a reply to message #606579] Mon, 27 January 2014 03:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
danny432 wrote on Mon, 27 January 2014 14:50
creating 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 #606596 is a reply to message #606580] Mon, 27 January 2014 04:16 Go to previous messageGo to next message
danny432
Messages: 6
Registered: January 2014
Junior Member
Oh yes the select statement on zakaz produced the error but i did a select on tovar which worked but example i did this
insert into zakaz (kod_tovar,kod_zaka,quantity) values(11,2,5)

given that in the table tovar quantity of the same item was 10 after inserting it becomes zero
Re: triggers [message #606601 is a reply to message #606596] Mon, 27 January 2014 04:26 Go to previous messageGo to next message
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 #606607 is a reply to message #606596] Mon, 27 January 2014 04:46 Go to previous messageGo to next message
danny432
Messages: 6
Registered: January 2014
Junior Member
create or replace 
trigger trig4 after update of quantity   on  orders for each row
declare
quantity1 VARCHAR(25); 
begin
select quantity into quantity1 from products where prod_id = :new.prod_id;
update products set quantity = quantity - quantity1  where prod_id = :new.prod_id;
end trig4;

select * from products;

PROD_ID PROD_NAME QUANTITY PRICE
---------- -------------------------------------------------- ---------- ----------
1 telek 6 50
insert into orders values(1,1,3,150)

1 rows inserted.
then again i run this
select * from products;

PROD_ID PROD_NAME QUANTITY PRICE
---------- -------------------------------------------------- ---------- ----------
1 telek 6 50

Nothing changed ... Please Note that i have created the tables with english names to make it easier for everyone to understand


Re: triggers [message #606608 is a reply to message #606601] Mon, 27 January 2014 04:47 Go to previous messageGo to next message
danny432
Messages: 6
Registered: January 2014
Junior Member
No i don't have an insert trigger on zakaz
Re: triggers [message #606610 is a reply to message #606608] Mon, 27 January 2014 04:53 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Then why would you expect anything to happen to your table after an insert? You probably need to go do a bit of reading. Here's a good start
Re: triggers [message #606611 is a reply to message #606608] Mon, 27 January 2014 04:54 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
 after update of quantity   on  orders 

Quote:
insert into orders values(1,1,3,150)

Quote:
Nothing changed ...


Why do you think something has to change? You have a trigger on UPDATE and you execute an INSERT.

Re: triggers [message #606612 is a reply to message #606607] Mon, 27 January 2014 04:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In addition:
Quote:
select quantity into quantity1 from products where prod_id = :new.prod_id;
update products set quantity = quantity - quantity1  where prod_id = :new.prod_id;


This will ALWAYS lead to 0 in "quantity" (assuming no other session modifies this column between SELECT and UPDATE).

Re: triggers [message #606617 is a reply to message #606612] Mon, 27 January 2014 08:21 Go to previous messageGo to next message
danny432
Messages: 6
Registered: January 2014
Junior Member
And could you shade more light on that, that it will result into 0 quantity coz it just happened , i tried an after insert on orders trigger and yes it resulted in the products table to have 0 quantity
Re: triggers [message #606618 is a reply to message #606617] Mon, 27 January 2014 08:29 Go to previous messageGo to next message
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.
Re: triggers [message #606638 is a reply to message #606618] Mon, 27 January 2014 12:24 Go to previous messageGo to next message
danny432
Messages: 6
Registered: January 2014
Junior Member
I get the point ,thanks people . i think i should get back to triggers to get more data on them, this was helpful
Re: triggers [message #606639 is a reply to message #606638] Mon, 27 January 2014 12:30 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Database Application Developer's Guide - Fundamentals
Chapter 9 Coding Triggers

Previous Topic: Success event log
Next Topic: Extracting sub-string in reverse way
Goto Forum:
  


Current Time: Thu Apr 25 02:30:48 CDT 2024