IF EXISTS (SELECT * FROM sysobjects WHERE type = 'TR' AND name = 'tg_groups_delete')
BEGIN
DROP TRIGGER tg_groups_delete
END;
CREATE TRIGGER tg_groups_delete
ON tc_groups FOR DELETE
AS BEGIN
UPDATE tc_groups SET groupid = NULL WHERE groupid IN (SELECT deleted.id FROM deleted)
END
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'TR' AND name = 'tg_users_delete')
BEGIN
DROP TRIGGER tg_users_delete
END;
CREATE TRIGGER tg_users_delete
ON tc_users FOR DELETE
AS BEGIN
DELETE FROM tc_user_user WHERE manageduserid IN (SELECT deleted.id FROM deleted)
END