IF EXISTS (
SELECT * FROM sysobjects WHERE
name = 'trGroups' AND type = 'TR')
BEGIN
DROP TRIGGER trGroups
IF NOT EXISTS (
SELECT * FROM sysobjects WHERE
name = 'trGroups' AND type = 'TR')
PRINT 'DROP trGroups SUCCEEDED'
ELSE
PRINT 'DROP trGroups FAILED'
END
ELSE
PRINT 'Attempting to CREATE trGroups'
GO
CREATE TRIGGER dbo.trGroups
ON groups
AFTER INSERT, UPDATE, DELETE
AS
-- This trigger submits a cached rates delete job for modified groups.
-- It also updates the groups' last modified columns for modified groups.
-- It also updates the groups' state (if currently unspecified) based on
-- the (first) state extracted from groups' zip code for new/modified groups.
-- No error checking is currently being performed.
SET NOCOUNT ON -- stop display of rowcount messages
DECLARE
@groupID int,
@deletedCount int,
@returnCode int
SELECT
@deletedCount = COUNT(*)
FROM
deleted WITH (NOLOCK)
IF @deletedCount > 0
BEGIN
DECLARE groupIDCursor CURSOR
FAST_FORWARD
FOR
SELECT DISTINCT
t1.groupID
FROM
(
SELECT
groupID
FROM
inserted WITH (NOLOCK)
UNION
SELECT
groupID
FROM
deleted WITH (NOLOCK)
) as t1
OPEN groupIDCursor
FETCH NEXT
FROM
groupIDCursor
INTO
@groupID
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC @returnCode = dbo.spCachingModuleAddJobDeleteCachedRatesByGroupID
@groupID = @groupID
FETCH NEXT
FROM
groupIDCursor
INTO
@groupID
END
CLOSE groupIDCursor
DEALLOCATE groupIDCursor
END
GO
IF EXISTS (
SELECT * FROM sysobjects WHERE
name = 'trGroups' AND type = 'TR')
PRINT 'CREATE trGroups SUCCEEDED'
ELSE
PRINT 'CREATE trGroups FAILED'
GO