IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[FIES].[T_PLAN_SECTION_ForUpdate]'))
DROP TRIGGER [FIES].[T_PLAN_SECTION_ForUpdate]
GO

--Добавление записей:
CREATE TRIGGER [FIES].[T_PLAN_SECTION_ForUpdate] ON [FIES].[T_PLAN_SECTION]
FOR UPDATE 
AS
IF(UPDATE(SectionAlias))
BEGIN
    declare @sqltext nvarchar(max)

    set @sqltext = 
    (
        select N'EXECUTE sp_rename N''FIES.' + ts.TableName + N'.' + ps.FieldNameFrom + N''', ' + N'N''' + ps.FieldNameTo + ''', N''COLUMN'';'
        from (
                      select N'T_PLAN_ITEM' as TableName
                union select N'T_PLAN_ITEM_LIMITS'
                union select N'T_SECTION_DEPEND_SETTINGS'
                union select N'T_SMO_COEF_MONTH_VALUES'
                union select N'T_FACT_ITEM'
                union select N'T_MEETING_RESOLVE'
                union select N'T_SPLIT_SETTINGS'
                union select N'T_FACT_ITEM_SUM'
                union select N'T_EXTERNAL_PLAN_SUM'
                union select N'T_COEFFICENT_STATINFO'
                union select N'T_MO_PARAMETER_ITEM'
                union select N'T_TP_FIN_CLOSE_ITEM'
                union select N'T_PLAN_FACT_STATISTICS'
             ) ts,
             (
                select psd.SectionAlias FieldNameFrom, psi.SectionAlias FieldNameTo
                from inserted psi
                    join VCLib.T_ENTITIES ei on ei.EntityID = psi.Entities
                    left join VCLib.T_ENTITIES_ATTRIBUTES eai on eai.EntityID = ei.EntityID and eai.IsPrimaryKey = 1
                    join deleted psd on psi.PlanSectionID = psd.PlanSectionID
             ) ps
        for xml path('')
    )
    exec sp_executesql @sqltext
END
