Update a table when an specific column of another table is updated using trigger in SQL Server - sql

I am using this trigger:
create trigger UpdateCustomerOnWholeSellerIdChange
on Invoice after update
as
begin
set nocount on;
if update(WholeSellerId)
begin
update Customer
set Customer.WholeSellerId = d.WholeSellerId
from deleted d
where d.CustomerId = Customer.CustomerId
end
end
What I am trying to accomplish is: each CustomerId has his own WholeSellerId meaning each Customer has their own salesman. In the Invoice table, each Invoice has its own CustomerId and WholeSellerId.
So when I update the WholeSellerId in the Invoice table, I would like to update the WholeSellerId of the Customer table as well to the WholeSellerId of the Invoice table.
With my code above, even when I update my WholeSellerId in the Invoice table, it doesn't update the Customer's WholeSellerId.
Your help is appreciated.

You are using deleted table as source, which contain data BEFORE your update run. Replace it with inserted table which have the new data.
MSDN - Use the inserted and deleted Tables

Related

Trigger for update base on parent table condition

Hi I use SQL SERVER 2012 and I have two table named tbcustomer and tbcustomerdetail. Both tables have column customerID.
I manage only tbcustomerdetail. If I update tbcustomerdetail where customerID='001' then I need the trigger to update tbcustomer where customerID='001'
I try this but not found spelling.
CREATE TRIGGER Deletecustomer
ON tbcustomerdetail
FOR UPDATE
AS
BEGIN
update tbcustomer tbcustomer.status=0 where tbcustomer.customerID=updated.customerID;
END
//updated.customerID is the ID on column tbcustomerdetail where customerID is '001'.
I also tried AFTER UPDATE but it doesn't work.
Updated does not exist, in a trigger you have two views Deleted which contains previous data and Inserted contains the new one
As I mentioned they are views, and contains data for all the rows affected by the statement, so, you need to use them in a join
CREATE TRIGGER Deletecustomer
ON tbcustomerdetail
FOR UPDATE
AS
BEGIN
UPDATE C
SET Status = 0
FROM tbCustomer C
JOIN Inserted I ON C.CustomerID = I.CustomerID
END

Sql Trigger After Insert Update another table with conditions

I am creating After Insert trigger , its working fine, but I have certain conditions before executing the statements inside the trigger
Based on Different CustomerId Run the trigger, I want check which CustomerId got inserted in my LoyaltyDetailsTable, say if last insert
was Customerid=2 then pass that Customerid in where condition then run
the trigger , or if Customerid = 1 then run the trigger for that Id,
so on.
I want to check whether in PriceClaimTable the inserted CustomerId exist or not, If exists then update the details else just insert the
values in LoyaltyDetailsTable only.
Trigger query
CREATE TRIGGER DetailsAfterInsert ON [dbo].[LoyaltyDetailsTable]
FOR INSERT
as
UPDATE PriceClaimTable
SET CurrentPoints =
(
(SELECT SUM(LoayaltyPointsTable.Points) AS RecentPoints FROM LoayaltyPointsTable
join LoyaltyDetailsTable ON LoayaltyPointsTable.LoyaltyPointsId
= LoyaltyDetailsTable.LoyaltyPointsId
WHERE CustomerId=1 and LoyaltyDetailsId= (SELECT MAX(LoyaltyDetailsId)
AS LoyaltyDetailsTable FROM LoyaltyDetailsTable))
+
(SELECT CurrentPoints FROM PriceClaimTable WHERE ClaimCustomerId=1 and
PriceClaimId=(SELECT max(PriceClaimId) FROM PriceClaimTable
))
)
WHERE ClaimCustomerId=1 and PriceClaimId=(SELECT max(PriceClaimId) FROM PriceClaimTable)
This is my first attempt to write a trigger, and here is table structure.
Any help would be great.
What you're looking for here is the inserted table. Every time you issue an UPDATE statement, SQL Server generates two virtual tables called inserted and deleted that store information on the data modifications you're making. These tables are accessible from your trigger. For more information, see here: https://msdn.microsoft.com/en-us/library/ms191300.aspx
You can use inserted to get the IDs you're looking for. So, instead of:
WHERE ClaimCustomerId=1
you can use:
WHERE ClaimCustomerId=inserted.ClaimCustomerId

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.

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 make an update trigger query in pl 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.