Insert Missing Benefits Pay
This script inserts missing benefit-related pay records into the PAYDESHIST and BENHIST tables for a specific employee. It is used to correct instances where a scheduled benefit was not properly paid out.
Data-Modifying Script
This script permanently modifies the PAYDESHIST and BENHIST tables. It is
highly recommended to first run the
missing_periods_missing_benefits_safety_check.sql script to preview the
changes before executing this one.
Description
The script finds scheduled benefits that lack a corresponding entry in both the PAYDESHIST and BENHIST tables. It then creates these missing records, ensuring the employee is correctly compensated for their benefit time.
Key functionalities include:
- Dual-Table Insertion: Creates records in both
PAYDESHISTandBENHISTto ensure data consistency. - Accurate Calculations: Converts hours from minutes and calculates the dollar amount based on the scheduled rate.
- Employee-Specific: Targets a single employee via their
FILEKEY. - Confirms Action: Reports the total number of records inserted into each table.
How to Use
- Run Safety Check First: Before using this script, run the
missing_periods_missing_benefits_safety_check.sqlscript to confirm which records will be created. - Set Employee
FILEKEY:- Open this script in your SQL editor.
- Find the line
DECLARE @FILEKEY INT = 6043;. - Replace
6043with theFILEKEYof the employee you need to correct.
- Execute the Script:
- Run the script to insert the missing benefit pay records.
- Verify the Result:
- The script will output a message confirming the number of records inserted into both tables.
Download Alternative Formats
📥 Browser Export Format:
- Download Export File - Download this export for external processing
SET NOCOUNT ON;
DECLARE @FILEKEY INT = 6043;
-- Replace with specific employee's FILEKEY
-- Create temporary table for employee info
CREATE TABLE #EmployeeInfo
(
FILEKEY INT,
ACTIVESTATUSEFFDATE DATE,
FirstValidPeriodStart DATE
);
-- Populate employee info
INSERT INTO #EmployeeInfo
SELECT
@FILEKEY AS FILEKEY,
e.ACTIVESTATUSEFFDATE,
DATEADD(WEEK, -1,
DATEADD(DAY, -(DATEPART(WEEKDAY, e.ACTIVESTATUSEFFDATE) - 1),
CAST(e.ACTIVESTATUSEFFDATE AS DATE))
) AS FirstValidPeriodStart
FROM EMPLOYEES e
WHERE e.FILEKEY = @FILEKEY
AND e.ACTIVESTATUS = 0;
-- Create temporary table for missing benefit pay
CREATE TABLE #MissingBenefitPay
(
FILEKEY INT,
SCHDATE DATE,
HOURS_MINUTES INT,
HOURS_ACTUAL REAL,
RATE REAL,
WG1 INT,
WG2 INT,
WG3 INT,
WG4 INT,
WG5 INT,
BENEFIT INT,
PAYDESNUM INT,
CALCULATED_DOLLARS REAL
);
-- Populate missing benefit pay
INSERT INTO #MissingBenefitPay
SELECT
s.FILEKEY,
s.SCHDATE,
s.HOURS AS HOURS_MINUTES,
s.HOURS / 60.0 AS HOURS_ACTUAL, -- Convert minutes to hours
s.RATE,
s.WG1,
s.WG2,
s.WG3,
s.WG4,
s.WG5,
s.BENEFIT,
b.PAYDESNUM,
(s.HOURS / 60.0) * s.RATE AS CALCULATED_DOLLARS
-- Hours * Rate
FROM #EmployeeInfo ei
INNER JOIN SCHEDULES s ON ei.FILEKEY = s.FILEKEY
AND s.SCHDATE >= ei.FirstValidPeriodStart
AND s.SCHDATE > '2025-04-19'
AND s.SCHDATE <= CAST(GETDATE() AS DATE)
AND s.SCHTYPE = 2 -- Only benefit schedules
INNER JOIN BENEFITS b ON s.BENEFIT = b.NUM
LEFT JOIN PAYDESHIST p ON s.FILEKEY = p.FILEKEY
AND s.SCHDATE = p.EVENTDATE
AND b.PAYDESNUM = p.PAYDESNUM
LEFT JOIN BENHIST bh ON s.FILEKEY = bh.FILEKEY
AND s.SCHDATE = bh.EVENTDATE
AND s.BENEFIT = bh.BENEFIT
WHERE p.FILEKEY IS NULL -- No matching pay record
AND bh.FILEKEY IS NULL;
-- No matching benefit history record
-- Insert into PAYDESHIST
INSERT INTO PAYDESHIST
(
[FILEKEY],
[EVENTDATE],
[WG1],
[WG2],
[WG3],
[PAYDESNUM],
[RATE],
[HOURS],
[DOLLARS],
[WG4],
[WG5]
)
SELECT
FILEKEY,
SCHDATE,
WG1,
WG2,
WG3,
PAYDESNUM,
RATE,
HOURS_ACTUAL, -- Use converted hours (240/60 = 4.0)
CALCULATED_DOLLARS,
WG4,
WG5
FROM #MissingBenefitPay;
DECLARE @PayDesHistCount INT = @@ROWCOUNT;
-- Insert into BENHIST
INSERT INTO BENHIST
(
[FILEKEY],
[EVENTDATE],
[BENEFIT],
[FIRSTUSE],
[PAYDESNUM],
[HOURS],
[DOLLARS],
[FLAGS]
)
SELECT
FILEKEY,
SCHDATE,
BENEFIT,
0 AS FIRSTUSE, -- Default to 0
PAYDESNUM,
HOURS_ACTUAL, -- Use converted hours (240/60 = 4.0)
CALCULATED_DOLLARS,
2 AS FLAGS
-- Default to 2 as specified
FROM #MissingBenefitPay;
DECLARE @BenHistCount INT = @@ROWCOUNT;
-- Show what was inserted
SELECT
'Inserted ' + CAST(@PayDesHistCount AS VARCHAR(10)) + ' records into PAYDESHIST and ' +
CAST(@BenHistCount AS VARCHAR(10)) + ' records into BENHIST for employee ' +
CAST(@FILEKEY AS VARCHAR(10)) AS Result;
-- Clean up temporary tables
DROP TABLE #EmployeeInfo;
DROP TABLE #MissingBenefitPay;
SET NOCOUNT OFF;Content Inventory
- Doc File:
content/docs/utilities/missing_periods_missing_benefits_insert.mdx - SQL Script:
SQL/utilities/missing_periods_missing_benefits_insert.sql - Browser Export:
public/aod/browser_exports/utilities/missing_periods_missing_benefits_insert.browser_export