I've been trying to figure out the trigger code which is generated by the tool for MS SQL. The first thing that comes to mind: when would you ever have a null value for the anchor id? Second, table @PE is declared to be not null for all fields. However, the WHERE clause has "inserted.PE_ID is null" which doesn't jive
Third, I don't understand how you are matching up the row numbers on the join.
Here is a sample:
--------------------------------- [Insert Trigger] -----------------------------------
-- PE_Person insert trigger on the latest perspective
--------------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM sys.triggers WHERE name = 'itPE_Person')
DROP TRIGGER [dbo].[itPE_Person]
GO
CREATE TRIGGER [dbo].[itPE_Person] ON lPE_Person
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @now DATETIME2(7) = SYSDATETIME();
DECLARE @v INT, @maxV INT;
DECLARE @PE TABLE (
Row int identity(1,1) not null primary key,
PE_ID int not null
);
INSERT INTO [dbo].[PE_Person](
Metadata_PE
)
OUTPUT
inserted.PE_ID
INTO
@PE
SELECT
Metadata_PE
FROM
inserted
WHERE
inserted.PE_ID is null;
DECLARE @inserted TABLE (
PE_ID int not null,
Metadata_PE metatype not null,
PE_GEN_RecordedAt datetime null,
PE_GEN_ErasedAt datetime null,
Metadata_PE_GEN metatype null,
PE_GEN_Person_Gender string null
);
INSERT INTO @inserted
SELECT
ISNULL(i.PE_ID, a.PE_ID),
i.Metadata_PE,
ISNULL(i.PE_GEN_RecordedAt, @now),
ISNULL(i.PE_GEN_ErasedAt, '9999-12-31'),
ISNULL(i.Metadata_PE_GEN, i.Metadata_PE),
i.PE_GEN_Person_Gender
FROM (
SELECT
PE_ID,
Metadata_PE,
PE_GEN_RecordedAt,
PE_GEN_ErasedAt,
Metadata_PE_GEN,
PE_GEN_Person_Gender,
ROW_NUMBER() OVER (PARTITION BY PE_ID ORDER BY PE_ID) AS Row
FROM
inserted
) i
LEFT JOIN
@PE a
ON
a.Row = i.Row;
INSERT INTO [dbo].[PE_GEN_Person_Gender](
PE_GEN_PE_ID,
PE_GEN_Person_Gender,
Metadata_PE_GEN,
PE_GEN_RecordedAt,
PE_GEN_ErasedAt
)
SELECT
i.PE_ID,
i.PE_GEN_Person_Gender,
i.Metadata_PE_GEN,
i.PE_GEN_RecordedAt,
i.PE_GEN_ErasedAt
FROM
@inserted i
WHERE
i.PE_GEN_Person_Gender is not null;
END