T-SQL将记录插入我们的ERP,检查重复项

时间:2017-06-14 14:05:52

标签: tsql

将记录插入我们的ERP,我需要检查重复项。 PC是我们的企业资源规划(ERP)。 GM是我们的客户关系管理(CRM)PCB是我们用来同步ERP和CRM之间更新的桥梁。哪个更好?方法1或方法2

PC是我们的ERP, GM是我们的CRM, PCB是我们的桥梁

SQL Server 2008r

Method 1
       IF New PCB.People_ID <> PC.PeopleID (1) --People_ID not Duplicate
          IF -- Next Check Name
        A.  or PCB FIRST_NAME <> PC FIRST_NAME (2)  
        B.  or PCB MIDDLE_NAME <> PC MIDDLE_NAME (2) 
        C.  or PCB LAST_NAME <> PC LAST_NAME (2)
          --Name Not Duplicate
             IF PCB @TAX_ID <> PC @P_TAX_ID (3)  --Next  Check  TAX_ID
                         --TAX_ID not  Duplicate
               Create New Record  sp_insert_people (15)
           Send PC.PeopleID to GM GM.U_KEY4
                    THEN 
                          Send back the 99-SSN to GM Status (6)
                    ENDIF
          THEN –Name is Duplicate
                 ABC Matching Names  send 99-NAM to GM Status (5)
             ENDIF
        THEN --People_ID is Duplicate (7)
          IF – Next Check Name 
        A.  or PCB FIRST_NAME <> PC FIRST_NAME (2) 
        B.  or PCB MIDDLE_NAME <> PC MIDDLE_NAME (2) 
        C.  or PCB LAST_NAME <> PC LAST_NAME (2)
          --Name not Duplicate
          send 99-NAM to GM Status (14)
         THEN
             IF PCB @TAX_ID <> PC @P_TAX_ID (3)  --Next  Check  TAX_ID
                          --TAX_ID not  Duplicate
                             send back the 99-SSN to Status GM.KEY1 (13)
                    THEN  --TAX_ID Was Not Duplicate
                           --Check Date of Birth
                IF CG BIRTH_DATE <> PC P_BIRTH_DATE (10)
                   send back 99-DOB to GM Status  Status GM.KEY1 (16)
                            THEN
                                 Update Record  sp_insert_people (11)
                            ENDIF –Date of Birth
                     ENDIF –TAX_ID
        ENDIF   
            Record was not created or updated for some reason other than listed above send back 99-OTHER  (17) To GM Status
        ENDIF

Method 2

    --  Start Check Comparison People Information
        DECLARE @p_list_people_changes TABLE(PEOPLE_ID VARCHAR(200));

        INSERT INTO @p_list_people_changes SELECT PEOPLE_ID FROM PEOPLECHANGES; --Some condition will add to reduce list

        IF OBJECT_ID('tempdb..#TMP_LIST_PEOPLE_ACTION') IS NOT NULL
        DROP TABLE #TMP_LIST_PEOPLE_ACTION;

        CREATE TABLE #TMP_LIST_PEOPLE_ACTION 
        (
            PEOPLE_ID VARCHAR(200),
            USTUSTATDT DATETIME,
            RESPOND_CODE VARCHAR(20),
            ACT CHAR(1) --I: INSERT, U: UPDATE, E: ERROR
        );

        DECLARE @p_PEOPLE_ID VARCHAR(200);  
        SELECT TOP 1 @p_PEOPLE_ID = PEOPLE_ID FROM @p_list_people_changes;

        IF OBJECT_ID('tempdb..#tmp_bridge_people_change') IS NOT NULL
        DROP TABLE #tmp_bridge_people_change;
        SELECT t.* INTO #tmp_bridge_people_change FROM PEOPLECHANGES t 
        INNER JOIN 
        (SELECT PEOPLE_ID, MAX(USTUSTATDT) USTUSTATDT  FROM PEOPLECHANGES
        GROUP BY PEOPLE_ID) t1 ON t.PEOPLE_ID = t1.PEOPLE_ID AND t.USTUSTATDT = t1.USTUSTATDT
        ;

        IF OBJECT_ID('tempdb..#tmp_powercampus_people') IS NOT NULL
        DROP TABLE #tmp_powercampus_people;
        SELECT * INTO #tmp_powercampus_people FROM PC.dbo.PEOPLE;


        --Get list of new People
        INSERT INTO #TMP_LIST_PEOPLE_ACTION (PEOPLE_ID, USTUSTATDT, RESPOND_CODE, ACT)
        SELECT br.PEOPLE_ID, br.USTUSTATDT, NULL, 'I' FROM #tmp_bridge_people_change br
        LEFT JOIN #tmp_powercampus_people pw ON br.PEOPLE_ID = pw.PEOPLE_ID
        WHERE pw.PEOPLE_ID IS NULL;

        --Get list of update/error People
        INSERT INTO #TMP_LIST_PEOPLE_ACTION (PEOPLE_ID, USTUSTATDT, RESPOND_CODE, ACT)
        SELECT br.PEOPLE_ID, br.USTUSTATDT,
        CASE 
                WHEN (br.FIRST_NAME = pw.FIRST_NAME AND br.MIDDLE_NAME = pw.MIDDLE_NAME AND br.LAST_NAME = pw.LAST_NAME)
                     AND (br.TAX_ID = pw.TAX_ID) 
                     AND (br.BIRTH_DATE = pw.BIRTH_DATE)
                THEN 
                    NULL
                ELSE 
                    CASE WHEN (br.FIRST_NAME <> pw.FIRST_NAME OR br.MIDDLE_NAME <> pw.MIDDLE_NAME OR br.LAST_NAME <> pw.LAST_NAME)
                        THEN '99-NAM'
                        WHEN (br.FIRST_NAME = pw.FIRST_NAME AND br.MIDDLE_NAME = pw.MIDDLE_NAME AND br.LAST_NAME = pw.LAST_NAME)
                                 AND (br.TAX_ID <> pw.TAX_ID)
                        THEN '99-SSN'
                        WHEN (br.FIRST_NAME = pw.FIRST_NAME AND br.MIDDLE_NAME = pw.MIDDLE_NAME AND br.LAST_NAME = pw.LAST_NAME)
                                 AND (br.TAX_ID = pw.TAX_ID) 
                                 AND (br.BIRTH_DATE <> pw.BIRTH_DATE)
                        THEN '99-DOB'
                        END
            END RESPOND_CODE, 
            CASE 
                WHEN (br.FIRST_NAME = pw.FIRST_NAME AND br.MIDDLE_NAME = pw.MIDDLE_NAME AND br.LAST_NAME = pw.LAST_NAME)
                     AND (br.TAX_ID = pw.TAX_ID) 
                     AND (br.BIRTH_DATE = pw.BIRTH_DATE)
                THEN 
                    'U'
                ELSE 
                    CASE WHEN NOT(br.FIRST_NAME <> pw.FIRST_NAME OR br.MIDDLE_NAME <> pw.MIDDLE_NAME OR br.LAST_NAME <> pw.LAST_NAME)
                        THEN 'E'
                        WHEN NOT((br.FIRST_NAME <> pw.FIRST_NAME OR br.MIDDLE_NAME <> pw.MIDDLE_NAME OR br.LAST_NAME <> pw.LAST_NAME)
                                 AND (br.TAX_ID <> pw.TAX_ID))
                        THEN 'E'
                        WHEN NOT((br.FIRST_NAME <> pw.FIRST_NAME OR br.MIDDLE_NAME <> pw.MIDDLE_NAME OR br.LAST_NAME <> pw.LAST_NAME)
                                 AND (br.TAX_ID <> pw.TAX_ID) 
                                 AND (br.BIRTH_DATE <> pw.BIRTH_DATE))
                        THEN 'E'
                        END
            END ACT
        FROM #tmp_bridge_people_change br
        INNER JOIN #tmp_powercampus_people pw ON br.PEOPLE_ID = pw.PEOPLE_ID
    --  End Check Comparison People Information

    --  Start Action after Comparison
    --  IF FALSE RAISE MESSEAGE TO UPDATE GM STATUS
        BEGIN TRY
            UPDATE c
            SET c.KEY1 = tmp.RESPOND_CODE
            FROM #TMP_LIST_PEOPLE_ACTION tmp
            JOIN GoldMine_test.dbo.CONTACT1 c   ON  tmp.PEOPLE_ID = c.KEY4
            WHERE tmp.ACT = 'E';
            COMMIT TRANSACTION;
        END TRY
        BEGIN CATCH
            ROLLBACK TRANSACTION;
            UPDATE c
            SET c.KEY1 = '99-OVR'
            FROM #TMP_LIST_PEOPLE_ACTION tmp
            JOIN GoldMine_test.dbo.CONTACT1 c   ON  tmp.PEOPLE_ID = c.KEY4
            WHERE tmp.ACT = 'E';
        END CATCH

    --  IF TRUE NEW RECORD THEN INSERT INTO PowerCampus
        BEGIN TRY
            INSERT INTO PC.dbo.People --People
              (PEOPLE_CODE,
               PEOPLE_ID,
               PEOPLE_CODE_ID,
               FIRST_NAME,
               MIDDLE_NAME,
               LAST_NAME,
               CREATE_DATE,
               CREATE_TIME...) 
              SELECT PEOPLE_CODE,
                     PEOPLE_ID,
                     PEOPLE_CODE_ID,
                     FIRST_NAME,
                     MIDDLE_NAME,
                     LAST_NAME,
                     CREATE_DATE,
                     CREATE_TIME,
                     'PERM'...
                FROM Bridge_test.dbo.PEOPLECHANGES t
               WHERE (Processed is Null) 
               AND EXISTS (SELECT 1 FROM #TMP_LIST_PEOPLE_ACTION tmp WHERE tmp.ACT = 'I' AND t.PEOPLE_ID = tmp.PEOPLE_ID AND t.USTUSTATDT = tmp.USTUSTATDT);

            --Send PEOPLE_ID to GM - I don't know what extractly information which you want to send
            INSERT INTO GoldMine_test.dbo.CONTACT1 (KEY4) 
            SELECT PEOPLE_ID FROM #TMP_LIST_PEOPLE_ACTION tmp
            WHERE tmp.ACT = 'I';

            COMMIT TRANSACTION;
        END TRY
        BEGIN CATCH
            ROLLBACK TRANSACTION;
            INSERT INTO GoldMine_test.dbo.CONTACT1 (KEY4, U_KEY4) 
            SELECT PEOPLE_ID, '99-OVR' U_KEY4 FROM #TMP_LIST_PEOPLE_ACTION tmp
            WHERE tmp.ACT = 'I';
        END CATCH

    --  IF TRUE EXISTS RECORD THEN UPDATE PowerCampus
        BEGIN TRY

            UPDATE c
                SET    c.PEOPLE_CODE = pc.PEOPLE_CODE,
                       --c.PEOPLE_ID = pc.,
                       c.PEOPLE_CODE_ID = pc.PEOPLE_CODE_ID,
                       c.FIRST_NAME = pc.FIRST_NAME,
                       c.MIDDLE_NAME = pc.MIDDLE_NAME,
                       c.LAST_NAME = pc.LAST_NAME,
                       c.CREATE_DATE = pc.CREATE_DATE,
                       c.CREATE_TIME = pc.CREATE_TIME...
            FROM PC.dbo.PEOPLE c
            INNER JOIN #TMP_LIST_PEOPLE_ACTION tmp ON c.PEOPLE_ID = tmp.PEOPLE_ID AND tmp.ACT = 'U'
            INNER JOIN Bridge_Test.dbo.PEOPLECHANGES pc ON tmp.PEOPLE_ID = pc.PEOPLE_ID AND tmp.USTUSTATDT = pc.USTUSTATDT
            COMMIT TRANSACTION;
        END TRY
        BEGIN CATCH
            ROLLBACK TRANSACTION;
            UPDATE c
            SET c.U_KEY4 = '99-OVR'
            FROM #TMP_LIST_PEOPLE_ACTION tmp
            JOIN GoldMine_test.dbo.CONTACT1 c   ON  tmp.PEOPLE_ID = c.KEY4
            WHERE tmp.ACT = 'U';
        END CATCH
    --  End Action after Comparison


    END
    GO

1 个答案:

答案 0 :(得分:0)

PowerCampus有许多用于重复检查的内置存储过程,您在尝试自己编写之前应该考虑这些过程:

[dbo].[spExistDuplicatePeople]
[dbo].[spFindDuplicatePeople]
[dbo].[spSelDuplicatePeopleByBatch]
[WebServices].[spSelTranscriptStudent]

此外,您应该使用内置过程在PEOPLE表中插入记录,例如[WebServices].[spInsPerson]