IF EXISTS (SELECT 1 FROM sys.triggers WHERE object_id = OBJECT_ID('RelationInsertTrigger'))
    DROP TRIGGER RelationInsertTrigger
GO

CREATE TRIGGER RelationInsertTrigger ON Relation
INSTEAD OF INSERT
AS
BEGIN
    INSERT INTO DT465 
        (UUID, DI001202, DI001203, DI001200, DI001201, 
        DI007348, DI001206, DI001216, DI001497, 
        DI001498, DI009342, DI001489, DI001057,
        DI001204) 
    SELECT 
        inserted.UUID,
        (select pred.DI001097 from 
                DT463 pred
                CROSS JOIN DT463 succ
            WHERE 
                pred.UUID = inserted."activityPredecessorUUID" AND
                succ.UUID = inserted."activitySuccessorUUID"),
        (select pred.DI001098 from 
                DT463 pred
                CROSS JOIN DT463 succ
            WHERE 
                pred.UUID = inserted."activityPredecessorUUID" AND
                succ.UUID = inserted."activitySuccessorUUID"),
        (select succ.DI001097 from 
                DT463 pred
                CROSS JOIN DT463 succ
            WHERE 
                pred.UUID = inserted."activityPredecessorUUID" AND
                succ.UUID = inserted."activitySuccessorUUID"),
        (select succ.DI001098 from 
                DT463 pred
                CROSS JOIN DT463 succ
            WHERE 
                pred.UUID = inserted."activityPredecessorUUID" AND
                succ.UUID = inserted."activitySuccessorUUID"), 
        inserted.DI007348, inserted.DI001206, inserted.DI001216, inserted.DI001497, 
        inserted.DI001498, inserted.DI009342, inserted.DI001489, inserted.DI001057,
        NEXT VALUE FOR position_sequence
    FROM 
        inserted
END;
GO
