LogoSupport Hub

Gary and Leo Trigger - Schedules Table

Gary and Leo Trigger - Schedules Table

Overview

This database trigger automatically processes benefit schedule entries when new records are inserted into the SCHEDULES table. It calculates benefit hours based on historical pay designation data, updates schedule hours with calculated values, and creates corresponding supervisor edits for benefit tracking. The trigger is specifically designed for benefit types 1 and 3 (likely vacation and sick time) and ensures proper benefit allocation based on employee work history.

Parameters

The trigger operates automatically on INSERT operations with these criteria:

  • Schedule Type: SCHTYPE = 2 (benefit schedules)
  • Benefit Types: BENEFIT IN (1, 3) (specific benefit categories)
  • Schedule Style: SCHSTYLE ≠ 10 (excludes already processed schedules)
  • Database Context: Configured for "garyandleo" database

Data Components

The trigger performs several complex operations:

  1. Pay Period Analysis - Builds temporary table of current and previous pay periods
  2. Historical Hours Calculation - Sums hours from pay designation history
  3. Benefit Hours Computation - Calculates available benefit hours with caps
  4. Schedule Updates - Updates schedule records with calculated hours
  5. Supervisor Edit Creation - Creates benefit edits for payroll processing

Output Format

This trigger produces no direct output but modifies multiple tables:

TableActionDescription
SCHEDULESUPDATESets calculated hours and marks as processed
SUPEDITSINSERT/UPDATECreates or updates benefit supervisor edits

Schedule Updates

  • hours: Calculated benefit hours (capped at 480 minutes)
  • schstyle: Set to 10 to mark as processed

Supervisor Edit Details

  • EDITTYPE: 301 (benefit edit type)
  • USERNAME: 'BENEFITS'
  • PAYDESNUM: 17 (benefit type 1) or 11 (benefit type 3)
  • hours: Calculated benefit hours minus 60 minutes

Technical Implementation

The trigger uses:

  • Temporary table creation for pay period tracking
  • Cursor-based processing for individual employee records
  • Complex date calculations for pay period boundaries
  • Historical data aggregation from paydeshist table
  • Conditional logic for benefit type handling
  • Duplicate prevention with EXISTS checks

Calculation Logic

  1. Pay Period Discovery: Identifies current and up to 3 previous pay periods
  2. Hours Aggregation: Sums specific pay designation hours from history
  3. Benefit Calculation: Converts hours to minutes with formula (hours/20)*60
  4. Hour Capping: Maximum 480 minutes (8 hours) per benefit schedule
  5. Edit Hours: Benefit hours minus 60 minutes for supervisor edits

Benefit Type Mapping

Benefit TypePAYDESNUMDescription
117Primary benefit (likely vacation)
311Secondary benefit (likely sick)

Pay Designation Types Included

  • Pay designations: 1, 2, 3, 9, 10, 11, 20, 21

Notes

  • Automatic Processing: Executes on every INSERT to SCHEDULES table
  • Benefit-Specific: Only processes benefit types 1 and 3
  • Pay Period Aware: Uses current and historical pay period data
  • Hour Capping: Limits benefit hours to 8 hours (480 minutes) maximum
  • Duplicate Prevention: Checks for existing supervisor edits before creating new ones
  • Database Specific: Configured for "garyandleo" database environment
  • Processing Marker: Uses schstyle = 10 to prevent reprocessing
  • Historical Dependency: Requires accurate paydeshist and archives data
  • Ensure archives table contains accurate pay period information
  • Verify pay designation mappings match current business rules
  • Monitor for performance impact on large INSERT operations
  • Test benefit calculations thoroughly before deployment
  • Consider the impact on payroll processing workflows
  • Backup database before installing or modifying this trigger
  • Review benefit hour calculations periodically for accuracy

T-SQL

GaryandLeoTrigger_SchedulesTable.sql
USE [databasename - uri: garyandleo]
GO

/****** Object:  Trigger [dbo].[SCHS]    Script Date: 2/3/2023 11:40:59 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[SCHS]
    ON [dbo].[SCHEDULES]
    FOR INSERT
AS

DECLARE @fkey VARCHAR(30);

SELECT @fkey = filekey
FROM inserted
WHERE schtype = 2
	AND benefit IN (1, 3);

IF @fkey IS NOT NULL 
BEGIN
	CREATE TABLE #pp
	(
		filekey INT,
		start DATETIME,
		enddate DATETIME,
		pp INT
	);

	DECLARE @today DATETIME;
	SELECT @today = GETDATE();

	INSERT INTO #pp
	SELECT DISTINCT
		filekey,
		periodbegin,
		periodend,
		1
	FROM archives
	WHERE @today BETWEEN periodbegin AND periodend;

	INSERT INTO #pp
	SELECT DISTINCT
		archives.filekey,
		periodbegin,
		periodend,
		2
	FROM archives
		INNER JOIN #pp ON archives.filekey = #pp.filekey
			AND archives.periodend = #pp.start - 1;

	INSERT INTO #pp
	SELECT DISTINCT
		archives.filekey,
		periodbegin,
		periodend,
		3
	FROM archives
		INNER JOIN #pp ON archives.filekey = #pp.filekey
			AND archives.periodend = #pp.start - 1
	WHERE pp = 2;

	INSERT INTO #pp
	SELECT DISTINCT
		archives.filekey,
		periodbegin,
		periodend,
		4
	FROM archives
		INNER JOIN #pp ON archives.filekey = #pp.filekey
			AND archives.periodend = #pp.start - 1
	WHERE pp = 3;

	DECLARE employees_cursor CURSOR FOR
        SELECT
		filekey,
		schdate,
		benefit
	FROM schedules
	WHERE schtype = 2
		AND benefit IN (1, 3)
		AND schstyle <> 10
	ORDER BY filekey;

	SET NOCOUNT ON;

	OPEN employees_cursor;

	DECLARE 
        @filekey INT,
        @schdate DATETIME,
        @benefit INT,
        @hrs REAL,
        @shrs REAL,
        @chrs REAL,
        @pend DATETIME,
        @ppstart DATETIME,
        @spstart DATETIME,
        @pendpp INT;

	FETCH NEXT FROM employees_cursor INTO @filekey, @schdate, @benefit;

	WHILE (@@FETCH_STATUS = 0)
    BEGIN
		-- This is executed as long as the previous fetch succeeds

		-- Get Dates
		SELECT @spstart = (
            SELECT start
			FROM #pp
			WHERE filekey = @filekey
				AND @schdate BETWEEN start AND enddate
        );

		SELECT @pend = (
            SELECT enddate
			FROM #pp
			WHERE filekey = @filekey
				AND enddate = @spstart - 1
        );

		SELECT @pendpp = (
            SELECT pp
			FROM #pp
			WHERE filekey = @filekey
				AND enddate = @pend
        );

		SELECT @ppstart = (
            SELECT start
			FROM #pp
			WHERE filekey = @filekey
				AND pp = @pendpp + 1
        );

		-- Get Hours
		SELECT @hrs = ISNULL((
            SELECT SUM(hours)
			FROM paydeshist
			WHERE filekey = @filekey
				AND paydesnum IN (1, 2, 3, 9, 10, 11, 20, 21)
				AND eventdate >= @ppstart
				AND eventdate <= @pend
        ), 0);

		IF ROUND(((@hrs / 20) * 60), 0) > 480
            SELECT @shrs = 480;
        ELSE
            SELECT @shrs = ROUND(((@hrs / 20) * 60), 0);

		SELECT @chrs = @shrs - 60;

		IF @hrs > 0
        BEGIN
			IF @shrs > 0
            BEGIN
				UPDATE schedules 
                SET hours = @shrs, 
                    schstyle = 10 
                WHERE filekey = @filekey
					AND schtype = 2
					AND benefit = @benefit
					AND schdate = @schdate;

				IF @benefit = 1
                BEGIN
					IF NOT EXISTS (
                        SELECT *
					FROM supedits
					WHERE filekey = @filekey
						AND username = 'BENEFITS'
						AND effdate = @schdate
						AND prevpaydes = 1
                    )
                        INSERT INTO supedits
					VALUES
						(
							@filekey,
							301,
							@schdate,
							'1900-01-01 00:01:00.000',
							'BENEFITS',
							0, 0, 0, 0, 0, 0, 0,
							17,
							1,
							@chrs,
							0.0, 0.0,
							GETDATE(),
							0, 0, 0,
							'',
							GETDATE(),
							GETDATE(),
							GETDATE()
                        );
                    ELSE
                        UPDATE supedits 
                        SET hours = @chrs 
                        WHERE filekey = @filekey
						AND username = 'BENEFITS'
						AND effdate = @schdate
						AND prevpaydes = 1;
				END;

				IF @benefit = 3
                BEGIN
					IF NOT EXISTS (
                        SELECT *
					FROM supedits
					WHERE filekey = @filekey
						AND username = 'BENEFITS'
						AND effdate = @schdate
						AND prevpaydes = 3
                    )
                        INSERT INTO supedits
					VALUES
						(
							@filekey,
							301,
							@schdate,
							'1900-01-01 00:01:00.000',
							'BENEFITS',
							0, 0, 0, 0, 0, 0, 0,
							11,
							3,
							@chrs,
							0.0, 0.0,
							GETDATE(),
							0, 0, 0,
							'',
							GETDATE(),
							GETDATE(),
							GETDATE()
                        );
                    ELSE
                        UPDATE supedits 
                        SET hours = @chrs 
                        WHERE filekey = @filekey
						AND username = 'BENEFITS'
						AND effdate = @schdate
						AND prevpaydes = 3;
				END;
			END;
		END;

		FETCH NEXT FROM employees_cursor INTO @filekey, @schdate, @benefit;
	END;

	CLOSE employees_cursor;
	DEALLOCATE employees_cursor;
END;

Content Inventory

  • Doc File: content/docs/utilities/GaryandLeoTrigger_SchedulesTable.mdx
  • SQL Script: SQL/utilities/GaryandLeoTrigger_SchedulesTable.sql