Trigger for update base on parent table condition - sql-server-2012

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

Related

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

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

SQL Trigger to update multiple columns in a View

I have this trigger that updates the value format of a column, But I would like to make it update on more then one column in a view instead of a table.
The trigger;
CREATE TRIGGER [dbo].[TriigerName]
ON [dbo].[Table]
AFTER INSERT
AS
BEGIN
UPDATE
t
SET
t.ColName = dbo.FunctionName(i.ColName)
FROM
dbo.table t
INNER JOIN
inserted i
ON
i.PrimaryId = t.PrimaryId
END
I have tried adding a second t.colName = dbo.FunctionName(i.colName) under SET but that did not work. The query ran, but did not update the value in the second column.
How can I modify this trigger to make it run on a view?
Thanks
EDIT 1:
I get this error ; View or function 't' is not updatable because the modification affects multiple base tables.
and I also changed AFTER INSERT to INSTEAD OF INSERT.
I think that your error message is normal. I guess your view is based on multiple tables
Any INSERT, UPDATE, or DELETE operation on a join view can modify only one underlying base table at a time.
Check this link:
http://docs.oracle.com/cd/B10501_01/server.920/a96521/views.htm#391
As user3238101 said, you cannot update 2 different tables with one statement, so you need to make 2 statements.
CREATE TRIGGER [dbo].[TriigerName]
ON [dbo].[Table]
AFTER INSERT
AS
BEGIN
UPDATE
t
SET
t.ColName = dbo.FunctionName(i.ColName)
FROM
dbo.table t
INNER JOIN
inserted i
ON
i.PrimaryId = t.PrimaryId
UPDATE
t
SET
t.ColName2 = dbo.FunctionName2(i.ColName2)
FROM
dbo.table t
INNER JOIN
inserted i
ON
i.PrimaryId = t.PrimaryId
END

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.

After insert trigger for updating a column

I am writing an after insert trigger trying to find a solution to this problem here:
https://stackoverflow.com/questions/19355644/dead-ends-all-around-trying-to-update-geography-column
What I am unsure of is how to write the trigger to take into consideration multiple records as explained here as a potential you need to code for.
So far I had this but it applies only to a single record so if the table had 100 records inserted in a batch 99 would not be updated. This is my understanding so far and may not be correct.
create trigger tri_inserts on [dbo].[Address]
after insert
as
set nocount on
update Address
SET AddyGeoCode = GEOGRAPHY::Point(inserted.AddyLat, inserted.Addylong, 4326)
GO
Would I say join to the inserted table to discover / update all the new records?
In case it is needed my Address table schema is AddyLat & AddyLong decimal(7,4) and AddyGeoCode Geography.
TIA
Yes, you need to join on inserted table.
UPDATE a
SET a.AddyGeoCode = GEOGRAPHY::Point(a.AddyLat, a.Addylong, 4326) --you can use AddyLat&Long from either a or i
FROM Address a
INNER JOIN inserted i ON a.id = i.id --whatever are your PK columns

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.