S
If when inserting in Table A you want to insert in Table B, and when updating in Table A, you want to update in Table B, then you would recommend to make several changes regarding trigger published in your question:Since the trigger is only triggered after insert or updateto determine if it is a update Just see if there's any data in the pseudo board deleted. If there isn't, we can be sure it's a insert.You have to take into account that it can be a massive operation, as in SQL Server, the triggers do not launch for each row that is inserted, modified or deleted. They are launched once, even if the marras instruction is modifying or inserting multiple records, as well as the strategy of insert/update You must take this into account.To update I use the instruction update/from that is exclusive SQL ServerBut it makes me very comfortable in these cases. If you're not familiar with her, I recommend your study.To insert, I make a insert/select, so I insert all the records that return the select.With this in mind, the trigger would look like this:CREATE TRIGGER test
ON [dbo].[CLASIFICACIONES]
AFTER UPDATE, INSERT
AS
BEGIN
SET NOCOUNT ON;
if exists (select 1 from deleted)
begin
--es un update, actualizamos:
update b
set c3 = i.c3
, c4 = i.c4
, c5 = i.c5
, c6 = i.c6
from inserted i
inner join KDIHCS b on b.c1 = i.c1 and b.c2 = i.c2;
end
else
begin
--es un registro nuevo, también lo insertamos
insert into KDIHCS (c1, c2, c3, c4, c5, c6)
select c1, c2, c3, c4, c5, c6
from inserted;
end;
set nocount off;
END
go
Maybe the names of the fields don't match, I don't have the structure of your tables by hand. The idea is that you analyze the solution proposed and adapt it to your particular case.