I have been reading up on triggers, and I don’t seem to be finding an example, that handles my situation. My situation is unfortunate. The previous DBA scattered redundant data throughout our database.
I’d like to update the company name in multiple other tables once the company name has been changed in my organiz table. I have this, but it doesn’t seem to work:
CREATE TRIGGER updOrgNameTrigger
ON organiz
AFTER UPDATE
AS
IF UPDATE(Org_Name_1)
BEGIN
DECLARE @org_name varchar(256)
SET @org_name = (select Org_Name_1 from organiz)
UPDATE other_table set Org_Name_1 = @org_name
UPDATE the_other_table set Org_name_1 = @org_name
END
Is what I am trying to do possible?
Your current trigger assumes that an update can only ever affect a single row; it also intends to update every single row in the other two tables with an arbitrary value from the source table (not necessarily the row that was updated!). You need to use the
insertedpseudo-table to identify the row(s) that fired the trigger and to pull the new value(s) for theOrg_Name_1column. How about this version: