How can I update and replicate one column from one table to another column from another table? - sql

I need to replicate one column (TYPE) from one table (CUTOMER) to another column (UNDEF000) from table (ORDERS), by this way everytime when someone update column(TYPE) to be automaticaly replicated on (UNDEF000), Table CUSTOMER and ORDERS are linked by column (PRE_ORDERCODE).
First I try to fill UNDEF000 from TYPE:
UPDATE ORDERS
JOIN CUSTOMER
SET ORDERS.UNDEF000=CUSTOMER.TYPE
WHERE ORDERS.PRE_ORDERCODE= CUSTOMER.PRE_ORDERCODE;
not function :(
UPDATE ORDERS
SET ORDERS.UNDEF000= CUSTOMER.TYPE
FROM CUSTOMER CUSTOMER
INNER JOIN ORDERS ORDERS
ON CUSTOMER.PRE_ORDERCODE= ORDERS.PRE_ORDERCODE
Can you please help me with this two problems?

I think your update should be like:
UPDATE ORDERS O SET O.UNDEF000= (
SELECT CUSTOMER.TYPE FROM CUSTOMER
WHERE CUSTOMER.PRE_ORDERCODE = O.PRE_ORDERCODE);
Trigger code:
create or replace trigger after_update_customer after update on
customer for each row
declare
begin
update orders set UNDEF000 = :new.type
where pre_ordercode = :new.pre_ordercode;
end;

Related

SQL insert trigger for insert with update

I have two tables:
[1] Donations - with amount and pet_id field
[2] Pets - with id and total donations field
I'm trying to create a trigger which will update total donations field whenever
a new row is being inserted to Donations table. I tried this one:
create trigger update_donations
on sponserships
for insert
as
update dbo.Pets
set tot_donations = (
select new_val = inserted.amount + pets.tot_donations
from inserted
where inserted.[Pet-ID] = pets.[Animal-ID]
)
But of course it changes all records, whereas i want to change only records that are changed in the donations table.
It is usually not a good practice to store this type of derived information - you could have a view that computes it on the fly rather than a trigger that keeps it up to date. Also please note that if you go that way, you also need a delete and an update trigger...
That said, you can use the following query in your insert trigger to update the relevant record in the pets table:
update p
set p.total_donations = p.total_donations + i.amount
from dbo.Pets p
inner join inserted i on i.[Pet-ID] = p.[Animal-ID]

Add data to trigger from the last insert

I am faced with the following situation:
I created a trigger which reacts on insert to the third table. When I insert any data (for example 1 1 2), last number should be subtracted from the Amount of stock column from cell, which has necessary ID Product (as it's shown on picture). But how can I understand which row was the last added? I thought firstly to do it by select, but it seems impossible. And now I think that it's possible to do it with the help of cursor, but it doesn't seem as the best variant. Is there a better variant how can I do it?
Here's my code of trigger, but it only subtracts 1 from the 1st product each time, unfortunately:
CREATE TRIGGER AmountInsert ON Amount
AFTER INSERT
AS
BEGIN
UPDATE Product
SET Amount_On_Stock =
(SELECT Amount_On_Stock FROM Product
WHERE ID_Product = 1) - 1
WHERE ID_Product = 1
END
The first thing you need to understand is that in a trigger in SQL Server you are provided with an inserted pseudo-table and a deleted pseudo-table. You use these tables to determine what changes have occurred.
I think the following trigger accomplishes what you are looking for - the comments explain the logic.
CREATE TRIGGER dbo.AmountInsert ON dbo.Amount
AFTER INSERT
AS
BEGIN
set nocount on;
update P set
-- Adjust the stock level by the amount of the latest insert
Amount_On_Stock = coalesce(Amount_On_Stock) - I.Amount
from dbo.Product P
inner join (
-- We need to group by ID_Product in case the same product appears in the insert multiple times
select ID_Product, sum(Amount) Amount
from Inserted
group by ID_Product
-- No need to update is net change is zero
having sum(Amount) <> 0
) I
on I.ID_Product = P.ID_Product;
END

SQL After trigger update value that triggers update

I have 2 tables, shipment and invoice. I have an after trigger that updates the shipment table with the invoice number from the invoice table.The invoice table has their related shipment numbers, can be more than one for each invoice.
Create Trigger ShipmentInvoice
on Therefore.dbo.Invoice
For Insert
AS
Update cat set
invoice = Fac.invoice
FROM therefore.dbo.thecat10 cat
INNER JOIN therefore.dbo.vFacturaAlbaran alb
on alb.shipment COLLATE Modern_Spanish_CI_AS = alb.shipment
This Works but it updates the whole table every time. I need a way to update the shipment table only with the values that were recently added in the invoice table when the trigger is activated.
Edit
The trigger is over the invoice table. I am updating the shipment table with the invoice numbers from the invoice table.
This Works but it updates the whole table every time. I need a way to update the shipment table only with the values that were recently added in the invoice table when the trigger is activated.
That because you are joining both tables, you need to use INSERTED table to get only the inserted rows as :
Create Trigger ShipmentInvoice
on Therefore.dbo.Invoice
For Insert
AS
Update cat set
invoice = I.invoice
FROM therefore.dbo.thecat10 cat
INNER JOIN INSERTED I alb ON Cat.ID = I.ID
...
it seems odd that you are updating the table to you are already inserting into; why are you not resolving the correct value before hand? This is pseudo-sql, but will get you on the right path
CREATE TRIGGER ShipmentInvoice
ON Therefore.dbo.Invoice
FOR INSERT
AS
UPDATE cat
SET invoice = Fac.invoice --There is no table/alias Fac, where is this coming from? inserted?
FROM therefore.dbo.thecat10 cat
JOIN inserted i ON cat.{Primary/Foreign ID COLUMN} = i.{Primary/Foreign ID COLUMN}
INNER JOIN therefore.dbo.vFacturaAlbaran alb ON alb.shipment COLLATE Modern_Spanish_CI_AS = alb.shipment;
Note the items in the braces ({}) and also my comment about your object Fac.

Trigger with table join

I want to create a trigger to update stock inventory. My quantity on stock and quantity ordered by the client are on two different tables. How do i join both?
CREATE OR REPLACE
TRIGGER UPDATE_QUANTITY_TRIGGER
AFTER insert ON SALE_ORDER
FOR EACH ROW
BEGIN
update product values
(:old.product_id,
:old.product_name,
:old.description,
quantity_on_stock-s.quantity,
:old.minimal_quantity,
:old.unit_price,
:old.product_type_id);
from product, sale_order s
where product.product_id=s.product_id;
END;
OR
CREATE OR REPLACE
TRIGGER UPDATE_QUANTITY_TRIGGER
AFTER insert ON SALE_ORDER
FOR EACH ROW
BEGIN
update product set
p.quantity_on_stock= p.quantity_on_stock-s.quantity;
from product p , sale_order s
where p.product_id=s.product_id;
END;
The sale_order table has columns sale_no, quantity, and product_id.
You can't join in an update statement. Your second attempt is closer but is still joining, and has no correlation. You also have a stray semicolon. I think you're looking for something like:
CREATE OR REPLACE
TRIGGER UPDATE_QUANTITY_TRIGGER
AFTER insert ON SALE_ORDER
FOR EACH ROW
BEGIN
update product p set
p.quantity_on_stock = p.quantity_on_stock - :new.quantity
where p.product_id = :new.product_id;
END;
This uses the values from the :new pseudorecord to both identify the product record to be updated, and to get the quantity to decrement the stock level by. There is no need to join to the triggering table again - which isn't allowed anyway.
You may run into unexpected behaviour or results doing this kind of update in a multiuser environment though.

Advanced sql query

I have two tables in a sql database, I am trying to update a column from my committed table (committedtbl) with values from my vendor table (vendortbl) based on a common column from both tables.
There is a column with the vendor identification number (vendorno) in both tables, I tried adding the vendor description (vendorname) column from the vendortbl to the committedtbl but there are no values in it.
I need to insert values into the vendorname based on the corresponding numbers from vendorno... How do I accomplish this?
The vendorname column already exists in my committedtbl.
I have tried this, but got an error:
update v_vendorname
set v_vendorname = v_vendorno
from vendortbl vt
where v_vendorno = vt.v_venkey
update committedtbl
set c.vendorname = v.vendorname
from committedtbl c
inner join vendortbl v on v.vendorno = c.vendorno