Advertisement
DataCCIW

cust_CCIW_copy_attributes_to_children

Mar 2nd, 2022
2,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 8.27 KB | None | 0 0
  1. CREATE PROCEDURE cust_CCIW_copy_attributes_to_children
  2.   @AttributeIDList VARCHAR(1000)
  3. AS
  4.  
  5. IF OBJECT_ID('tempdb..#temptable') IS NOT NULL
  6. BEGIN
  7.   DROP TABLE #temptable;
  8. END;
  9.  
  10. IF OBJECT_ID('tempdb..#update_table') IS NOT NULL
  11. BEGIN
  12.   DROP TABLE #update_table;
  13. END;
  14.  
  15.  
  16.  
  17. DECLARE
  18.   @AdultRole INT = 29;
  19. DECLARE
  20.   @ChildRole INT = 31;
  21.  
  22. DECLARE
  23.   @UpdateDateTime DATETIME = GETDATE();
  24.  
  25. DECLARE
  26.   @OrganizationID INT = 1;
  27.  
  28. DECLARE
  29.   @HistoryTypeLUID INT = 9457;
  30.  
  31.  
  32. SELECT CPA.person_id,
  33.        person2 = CPA2.person_id,
  34.        child_id = CFM2.person_id,
  35.        CFM.family_id,
  36.        CPA.attribute_id,
  37.        Attb_Mod_Date = CPA.date_modified,
  38.        CFM2.person_id AS Child_per_id,
  39.        Attb_val_1 = CPA.attribute_value,
  40.        Update_Person_ID = CASE WHEN CPA2.date_modified IS NULL
  41.                                     OR CPA.date_modified > CPA2.date_modified THEN CPA2.person_id
  42.                           END,
  43.        Insert_Person_ID = CASE WHEN CPA2.date_modified IS NULL THEN CFM2.person_id
  44.                                ELSE NULL
  45.                           END,
  46.        new_int_value =  CPA.int_value,
  47.        new_decimal_value = CPA.decimal_value,
  48.        new_varchar_value = CPA.varchar_value,
  49.        new_datetime_value = CPA.datetime_value,
  50.        new_attribute_value = CPA.attribute_value,
  51.        CPA.attribute_type,
  52.        CPA.attribute_name,
  53.        CPA.date_modified,
  54.        [date_modified_2] = CPA2.date_modified
  55. INTO #temptable
  56. FROM   cust_CCIW_v_person_attribute CPA
  57.        JOIN core_family_member CFM ON CFM.person_id = CPA.person_id
  58.        JOIN core_family_member CFM2 ON CFM.family_id = CFM2.family_id
  59.        LEFT JOIN cust_CCIW_v_person_attribute CPA2 ON CPA2.person_id = CFM2.person_id
  60.                                                       AND CPA2.attribute_id = CPA.attribute_id
  61.        INNER JOIN cust_CCIW_v_head H ON H.person_id = CPA.person_id
  62.        INNER JOIN cust_CCIW_v_person_attribute M on M.person_id = H.person_id and M.attribute_id = 244 --Head of House Missionary Status
  63. WHERE  
  64. M.int_value in (11570, 11571, 11586) --Career, Midterm, Missions Associate (Prayer Only)
  65. AND CPA.attribute_id IN
  66. (
  67.     SELECT *
  68.     FROM   dbo.fnSplit(@AttributeIDList)
  69. )
  70.        AND CFM2.role_luid = @ChildRole
  71.        AND CFM.role_luid = @AdultRole
  72.        AND CFM.person_id != CFM2.person_id
  73.        AND -- Do update where the values are null, != comparison will fail for null values
  74.        ( CPA.attribute_value != CPA2.attribute_value
  75.          OR CPA2.attribute_value IS NULL
  76.                                         )
  77.        AND -- Don't update attributes where both values are null
  78.        ( CPA.attribute_value IS NOT NULL
  79.          OR CPA2.attribute_value IS NOT NULL
  80.                                             )
  81.        AND CFM.family_id NOT IN -- Don't include families that have 3+ Adults, this can lead to a scenario where the script tries to insert duplicate primary keys
  82. (
  83.     SELECT f.family_id
  84.     FROM   core_family_member fm
  85.            JOIN core_family f ON f.family_id = fm.family_id
  86.     GROUP BY f.family_id,
  87.              role_luid,
  88.              f.family_name
  89.     HAVING role_luid = @AdultRole
  90.            AND COUNT(*) > 2
  91. );
  92.  
  93.  
  94. SELECT *
  95. FROM   #temptable t
  96. ORDER BY person_id;
  97.  
  98. SELECT DISTINCT
  99.        Update_Person_ID,
  100.        Insert_Person_ID,
  101.        attribute_id,
  102.        new_int_value,
  103.        new_decimal_value,
  104.        new_varchar_value,
  105.        new_datetime_value,
  106.        new_attribute_value,
  107.        attribute_type,
  108.        attribute_name
  109. INTO #update_table
  110. FROM   #temptable;
  111.  
  112. BEGIN TRAN Child_Attb_Update;
  113. -- Do Updates
  114.  
  115. IF
  116. (
  117.     SELECT COUNT(*)
  118.     FROM   #update_table
  119.     WHERE  Update_Person_ID IS NOT NULL
  120. ) > 0
  121. BEGIN
  122.   UPDATE CPA
  123.     SET  int_value = CASE WHEN T.attribute_type IN(0, 3, 4, 9) THEN T.new_int_value
  124.                           ELSE int_value
  125.                      END, varchar_value = CASE WHEN T.attribute_type IN(1, 7, 8) THEN T.new_varchar_value
  126.                                                ELSE varchar_value
  127.                                           END, datetime_value = CASE WHEN T.attribute_type IN(2) THEN T.new_datetime_value
  128.                                                                      ELSE datetime_value
  129.                                                                 END, decimal_value = CASE WHEN T.attribute_type IN(5, 6) THEN T.new_decimal_value
  130.                                                                                           ELSE decimal_value
  131.                                                                                      END, date_modified = @UpdateDateTime
  132.   FROM   core_person_attribute CPA
  133.          JOIN #update_table T ON CPA.person_id = T.Update_Person_ID
  134.                                  AND CPA.attribute_id = T.attribute_id;
  135. END;
  136. --
  137.  
  138. SELECT
  139.        Change_Type = CASE WHEN Update_Person_ID IS NULL THEN 'Add New Attribute Value' ELSE 'Change Existing Attribute Value' END,
  140.        P.person_id,
  141.        [Person to Update] =P.first_name + ' ' + P.last_name,
  142.        U.attribute_name,
  143.        U.new_attribute_value,
  144.        U.attribute_id
  145. FROM   #update_table U
  146.        JOIN core_person P ON P.person_id = COALESCE(Update_Person_ID, Insert_Person_ID)
  147. ORDER BY Update_Person_ID DESC;
  148.  
  149.     -- Do Inserts
  150.  
  151. IF
  152. (
  153.     SELECT COUNT(*)
  154.     FROM   #update_table
  155.     WHERE  Insert_Person_ID IS NOT NULL
  156. ) > 0
  157.  
  158. BEGIN
  159.   INSERT INTO  core_person_attribute(person_id,
  160.                attribute_id,
  161.                int_value,
  162.                varchar_value,
  163.                datetime_value,
  164.                decimal_value,
  165.                date_created,
  166.                date_modified,
  167.                created_by,
  168.                modified_by,
  169.                organization_id)
  170.          SELECT U.Insert_Person_ID,
  171.                 U.attribute_id,
  172.                 CASE WHEN U.attribute_type IN(0, 3, 4, 9) THEN U.new_int_value
  173.                      ELSE NULL
  174.                 END,
  175.                 CASE WHEN U.attribute_type IN(1, 7, 8) THEN U.new_varchar_value
  176.                      ELSE NULL
  177.                 END,
  178.                 CASE WHEN U.attribute_type IN(2) THEN U.new_datetime_value
  179.                      ELSE NULL
  180.                 END,
  181.                 CASE WHEN U.attribute_type IN(5, 6) THEN U.new_decimal_value
  182.                      ELSE NULL
  183.                 END,
  184.                 @UpdateDateTime,
  185.                 @UpdateDateTime,
  186.                 'Attribute Copy Script',
  187.                 'Attribute Copy Script',
  188.                 @OrganizationID
  189.          FROM   #update_table U
  190.          WHERE  U.Insert_Person_ID IS NOT NULL;
  191. END;
  192.  
  193.  
  194. --Add history
  195.  
  196. IF
  197. (
  198.     SELECT COUNT(*)
  199.     FROM   #update_table
  200. ) > 0
  201. BEGIN
  202.   INSERT INTO                    core_person_history(date_created,
  203.                                                      date_modified,
  204.                                                      created_by,
  205.                                                      modified_by,
  206.                                                      person_id,
  207.                                                      history_type_luid,
  208.                                                      history_qualifier_id,
  209.                                                      system_history,
  210.                                                      history,
  211.                                                      organization_id,
  212.                                                      display_flag,
  213.                                                      display_expiration,
  214.                                                      private_flag)
  215.          SELECT @UpdateDateTime,
  216.                 @UpdateDateTime,
  217.                 'Attribute Copy Script',
  218.                 'Attribute Copy Script',
  219.                 COALESCE(U.Update_Person_ID, U.Insert_Person_ID),
  220.                 @HistoryTypeLUID,
  221.                 U.attribute_id,
  222.                 'True',
  223.                 CASE WHEN U.Update_Person_ID IS NOT NULL THEN 'Updated ' + attribute_name + ' via Attribute Copy Script to ' + new_attribute_value
  224.                      ELSE 'Attribute ' + attribute_name + ' created via Attribute Copy Script'
  225.                 END,
  226.                 @OrganizationID,
  227.                 'false',
  228.                 0,
  229.                 'False'
  230.          FROM   #update_table U;
  231. END;
  232. --
  233.  
  234. COMMIT TRAN Child_Attb_Update;
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement