how to make an update trigger query in pl sql - sql

I have 2 tables (1) product_warehouse and (2) Return_Vendor Invoice.
i have to update the quantity in product_warehouse by trigger according to the value of Return_Vendor Invoice Table.
where Item_code is unique key in both tables.
For example if the product_warehouse contain 3 quantities , and the shopkeeper returns 1 quantity to vendor then it should be 2 in the Product_warehouse. update query will also acceptable.

create or replace trigger tiuda_return_vendor after insert or update or delete
on return_vendor is
begin
update product_wherehouse
set
quantity = quantity - (:new.quantity - nvl(:old.quantity, 0)
where
item_code = nvl(:new.item_code, :old.item_code);
end;
This trigger works in most cases. You can insert update or delete the return line.
Only thing is: when updating, you cannot update the item_code itself, because the update statement doesn't take that into account. You could easily solve that, but I don't know if it's in your requirements. I usually don't change values like that, but rather remove the item and add a new line for a different item.
Updating the quantity works fine. If you update the quantity, the difference between old and new is calculated and that difference is used to modify the stock quantity in the wherehouse.

create or replace
TRIGGER "WR_RETURN_INVOICE_UPDATE_TRG"
AFTER UPDATE ON RETURN_INVOICE
FOR EACH ROW
BEGIN
UPDATE PRODUCT_WAREHOUSE
SET QUANTITY=QUANTITY-:OLD.QUANTITY
WHERE ITEM_CODE=:OLD.ITEM_CODE;
END WR_RETURN_INVOICE_UPDATE_TRG;
Try this one it will works.

Related

Is it possible to update 2 columns of 2 tables using triggering in oracle?

I create 3 table whose name are customers,payments and orders.They have a same column name customerNumber.customerNumber is a primary key of customers table and a foreign of payments and orders table.Now I want to make a trigger whose works is to update customerNumber in payments and orders table before updating customerNumber in customers table.
my code is here:
create or replace trigger customers_update
before update of customerNumber on customers
for each row
begin
update payments,orders
set
payments.customerNumber = :new.customerNumber
orders.customerNumber = :new.customerNumber
where (payments.customerNumber = :old.customerNumber)
and
(orders.customerNumber = :old.customerNumber);
end;
/
but it shows some problem like this
Error at line 2: PL/SQL: SQL Statement ignored
create or replace trigger customers_update
before update of customerNumber on customers
for each row
begin
So my question is how I fix the problem ?
Just use two update statements:
update payments
set customerNumber = :new.customerNumber
where payments.customerNumber = :old.customerNumber;
update orders
set customerNumber = :new.customerNumber
where orders.customerNumber = :old.customerNumber;
Oracle doesn't allow updates to two tables in the same statement.
"Oracle doesn't allow updates to two tables in the same statement."
That's true, but if you really like, you can create a view and update the view.
Using a trigger with type INSTEAD OF, you can update both, but still two updates internally, but one update a functional level.
Let me know if you have any questions.
Thanks.

Insert or Replace with sum of old values

I am building Inventory Application using PhoneGap.In that i have one module STOCK for stock management.
Stock Table Query
CREATE TABLE STOCK (
sto_id INTEGER PRIMARY KEY AUTOINCREMENT,
pro_id INTEGER FOREIGNKEY REFERENCES PRODUCT(pro_id) UNIQUE,
quantity TEXT
)
INSERT OR REPLACE Query
INSERT OR REPLACE INTO STOCK (pro_id,quantity) VALUES ("1","5");
There is not a single issue with this query its working perfectly but i want to update SUM of OLD VALUES WITH NEW ONE.
Example:
pro_id quantity
1 5
This is existing record so now when i will fire above query for new transaction which have 3 quantity then quantity should be (5 (old) + 3 (new) ) = 8.
So after updateing record it looks like.
pro_id quantity
1 8
How can i solve this any idea. or let me know if i am on wrong way.
Thanks.
Actually I am not real founder of this solution. The real hero is Daverandom with the help of him i have solve my issue.
He has suggest me to follow this solution and that is really helpful to find my solution.
Older query looks like as
INSERT OR REPLACE INTO STOCK (pro_id,quantity) VALUES ("1","5");
New Query looks like as
INSERT OR IGNORE INTO STOCK (pro_id,quantity) VALUES (1,0) ;
UPDATE STOCK SET quantity = quantity + 2 WHERE pro_id=1;
Update:
If you will not add WHERE pro_id="val" in UPDATE then it will UPDATE all rows.
So that will generate will appropriate result.
When user fire query first time then quantity will be 2 and when you fire same query second time it will 4.
So, We can change that value in each update.
Again thanks to Daverandom.
In SQLite, you can create a trigger to handle such functionality:
CREATE TRIGGER t1 BEFORE INSERT ON STOCK WHEN NEW.pro_id IN (SELECT pro_id FROM STOCK) BEGIN
UPDATE STOCK SET quantity=COALESCE(NEW.quantity, 0)+COALESCE((SELECT quantity FROM STOCK WHERE pro_id=NEW.pro_id), 0) WHERE pro_id=NEW.pro_id;
SELECT RAISE(IGNORE); -- Ignore INSERT
END;
From now on, whenever you try to insert an existing pro_id, update on quantity is done instead. Conflict clause (OR REPLACE) doesn't matter as trigger will handle it (for pro_id).
Yet another, this time without triggers, using single statement solution:
INSERT OR REPLACE STOCK(pro_id, quantity)
SELECT npro_id, COALESCE(nqty, 0)+COALESCE(quantity,0) FROM (
SELECT 123 AS npro_id, 9999 AS nqty
) LEFT JOIN STOCK ON npro_id=pro_id;
Just replace 123 with new prod_id and 9999 with new quantity.

Creating SQL trigger that only affects specific row

I have two tables created inventory and customer_sales. Individuals working in the warehouse can't have access to the customer_sales table.
So I added an additional field to inventory table called num_sales.
I want to create a trigger functions that whenever a new customer sale is added. It will correspond to the specific inventory_item that was sold's row and not the entire table.
This is what I have so far.
ALTER TABLE inventory
ADD num_sales INT
UPDATE movies SET num_sales = 0;
ALTER TABLE movies ALTER COLUMN num_rentals INT NOT NULL;
GO
CREATE TRIGGER tr_movies_num_rentals_add
ON customer_sales FOR INSERT
AS
BEGIN
UPDATE inventory
SET num_sales = num_sales + (SELECT sale_status_code FROM INSERTED)
WHERE 1 = (SELECT sale_status_code FROM INSERTED);
END;
Note:
sale_status_code values: 1=sold, 2=reserved, 3=lost.
UPDATE: I am using Microsoft SQL server management studio. I am newbie and this is a question I have for school.
First assume inserted willhave multiple rows. SQL server triggers do not operate row by row. YOu need to join to inserted not use a subquery. Next if you want this to happen on insert, update and delete, then you need to use more than an INSert trigger. And the delete trigger should use a different formula than inserted and updated becasue you will be subtracting the value from the deleted table from the inventory total.

How to create SQL trigger

I have the following table: product(id, price, is_deleted)
I want to create a trigger that, when someone deletes a row from the PRODUCT table, instead of deleting it, changes the value of the column 'is_deleted' in that row to '1'.
The row that is to be deleted during the execution of a DELETE task must be temporary in a table called 'deleted'. It must also be the only row that is in the table 'deleted' on that moment.
This is what I thought:
CREATE TRIGGER is_deleted
ON product
FOR INSERT
AS
BEGIN
UPDATE Product, is_deleted = 1
WHERE is_deleted = NULL
END
Is this a good way to do it?
Thanks in advance!

SQL update function doesn't throw any error message but it returns no rows

I am trying to update a table value from another table. I want to update pstatus from productcode.
Here is my code (which gives no error and results 0 rows):
UPDATE pstatus
SET code=(select code FROM productcode)
besides this, I can't re-run any update in sql - it gives no error, but returns 0 rows (I have more than 5 rows in productcode table).
In this case you have a two problem
you say you dont have any rows in pstatus if no record in table how can table row update ?
your inner query select code FROM productcode return more then one row then how sql decide to set with value in wich field?
An update statement doesn't return rows. You should have used something more like
Update pstatus
Set code=a.code
from productcode a
where a.*somekeyfield* = *value*
You have to determine which single record from productcode you wish to use as the basis for the update. What somekeyfield should be is something you need to determine.
An update statemend doesn't add records, it only updates records that already exist in the table.
The update query does actually udpate all the records in the table, but as there are no records, the number of updated records is zero.
If you want to add records, you should use an insert query instead:
insert into pstatus (code)
select code from productcode