Project

General

Profile

Bug #2183 » TiggerDelimit.txt

M Azid Wahyudi, 06/06/2023 05:43 PM

 
1
USE [MinovaHR_Indomobil_Dev]
2
GO
3
/****** Object:  Trigger [dbo].[CombinedTrigger]    Script Date: 06/06/2023 17:39:14 ******/
4
SET ANSI_NULLS ON
5
GO
6
SET QUOTED_IDENTIFIER ON
7
GO
8
CREATE TRIGGER [dbo].[CombinedTriggerDelimit]
9
ON [dbo].[hr_md_orm_object]
10
AFTER INSERT
11
AS
12
BEGIN
13
    SET NOCOUNT ON;
14

    
15
    -- Simpan record baru yang di-insert pertama kali
16
    INSERT INTO hr_md_orm_object
17
    SELECT *
18
    FROM inserted
19
    WHERE object NOT IN (
20
        SELECT object
21
        FROM hr_md_orm_object
22
        WHERE class = 'DE'
23
    );
24

    
25
    -- Hapus catatan duplikat berdasarkan objectid dan start_date maksimum
26
    DELETE FROM hr_md_orm_object
27
    WHERE object IN (
28
        SELECT object
29
        FROM (
30
            SELECT object, ROW_NUMBER() OVER (PARTITION BY object ORDER BY start_date DESC) AS RowNumber
31
            FROM hr_md_orm_object
32
            WHERE class = 'DE' AND object IN (
33
                SELECT object
34
                FROM hr_md_orm_object
35
                WHERE class = 'DE'
36
                GROUP BY object
37
                HAVING COUNT(*) > 1
38
            )
39
        ) AS duplicates
40
        WHERE duplicates.RowNumber = 1
41
    ) AND class = 'DE'
42
    AND end_date = (
43
        SELECT MAX(end_date)
44
        FROM hr_md_orm_object
45
        WHERE object IN (
46
            SELECT object
47
            FROM (
48
                SELECT object, ROW_NUMBER() OVER (PARTITION BY object ORDER BY start_date DESC) AS RowNumber
49
                FROM hr_md_orm_object
50
                WHERE class = 'DE' AND object IN (
51
                    SELECT object
52
                    FROM hr_md_orm_object
53
                    WHERE class = 'DE'
54
                    GROUP BY object
55
                    HAVING COUNT(*) > 1
56
                )
57
            ) AS duplicates
58
            WHERE duplicates.RowNumber > 1
59
        )
60
    );
61

    
62
    -- Memperbarui field enddate pada tabel hr_md_orm_relationship
63
    UPDATE r
64
    SET r.end_date = i.end_date
65
    FROM hr_md_orm_relationship AS r
66
    INNER JOIN hr_md_orm_object AS i ON r.[object] = i.[object]
67
    WHERE r.class = 'DE'
68
      AND i.class = 'DE'; -- Ubah sesuai dengan hubungan antara tabel yang sesuai
69
END;
(5-5/5)