The trigger is updating multiple rows with the same value
Here a problem, I'm trying to update one or many rows in a table(B), but its updating the rows with the same value let's say
TABLE A
+------+-----+-----+
|ROWID |NAME |CODE |
+------+-----+-----+
| 123 |pepe | 1 |
| 456 |tito | 2 |
| 789 |gege | 3 |
-------------------
Current result:
+------+-----+-----+---------+
|ROWID |NAME |CODE | A_ROWID |
+------+-----+-----+---------+
| 321 |rolo | 1 | 123 |
| 654 |kit | 2 | 123 |
| 987 |gu | 3 | 123 |
-----------------------------
Expected result:
+------+-----+-----+---------+
|ROWID |NAME |CODE | A_ROWID |
+------+-----+-----+---------+
| 321 |rolo | 1 | 123 |
| 654 |kit | 2 | 456 |
| 987 |gu | 3 | 789 |
-----------------------------
This is the trigger, I will really appreciate any advice
ALTER TRIGGER [dbo].[TRIG_AfterInsert]
ON [dbo].[B]
AFTER INSERT
AS
BEGIN
DECLARE @Code nvarchar(100);
SELECT @Code = CODE FROM INSERTED;
UPDATE B
SET B.A_ROWID = A.ROWID
FROM A
WHERE A.CODE = @Code;
END