Скрипт:
DECLARE @TableName NVARCHAR(250)
SET @TableName = 'Activity' -- ЗДЕСЬ ВПИСЫВАЕТСЯ НАЗВАНИЕ ТАБЛИЦЫ
PRINT 'Start process ' + @TableName
EXEC ('
DELETE FROM Sys' + @TableName + 'Right;
DECLARE @TableSchemaUId UNIQUEIDENTIFIER;
SELECT top 1 @TableSchemaUId = UId FROM SysSchema WHERE [Name] = ''' + @TableName + '''
and [ExtendParent] = ''0''
/* Полный доступ автору записи */
INSERT INTO Sys' + @TableName + 'Right (CreatedOn, ModifiedOn, RecordId,
SysAdminUnitId, Operation, RightLevel, Position, SourceId)
SELECT GETUTCDATE(), GETUTCDATE(), o.Id, au.Id, op.Id, 2, 0, ''{4220CFBA-0514-44CE-ADD0-109B54B52084}'' -- Источник: Автор
FROM ' + @TableName + ' o
JOIN SysAdminUnit au ON (au.ContactId = o.CreatedById)
CROSS JOIN (SELECT 0 Id UNION ALL SELECT 1 UNION ALL SELECT 2) op -- Операции: Чтение, Изменение, Удаление
WHERE NOT EXISTS(SELECT * FROM Sys' + @TableName + 'Right WHERE RecordId = o.Id AND
SysAdminUnitId = au.Id AND Operation = op.Id);
/* Копирование прав по умолчанию */
INSERT INTO Sys' + @TableName + 'Right (CreatedOn, ModifiedOn, RecordId,
SysAdminUnitId, Operation, RightLevel, Position, SourceId)
SELECT DISTINCT GETUTCDATE(), GETUTCDATE(), o.Id, dr.GranteeSysAdminUnitId,
dr.Operation, dr.RightLevel, 0, ''{F41E0268-E324-4228-9E9E-5CB7CC906398}'' -- Источник: По умолчанию
FROM ' + @TableName + ' o
JOIN SysAdminUnit au ON (au.ContactId = o.CreatedById)
LEFT JOIN SysUserInRole uir ON (uir.SysUserId = au.Id)
JOIN SysEntitySchemaRecordDefRight dr ON (dr.AuthorSysAdminUnitId = uir.SysRoleId OR dr.AuthorSysAdminUnitId = au.Id OR
dr.AuthorSysAdminUnitId IN (SELECT Id FROM SysAdminUnit WHERE ParentRoleId IS NULL))
WHERE dr.SubjectSchemaUId = @TableSchemaUId AND
NOT EXISTS(SELECT * FROM Sys' + @TableName + 'Right WHERE RecordId = o.Id AND
SysAdminUnitId = dr.GranteeSysAdminUnitId AND Operation = dr.Operation);
')
PRINT @TableName + ' was processed'