

/****** Object:  Trigger [T_PERIOD_SECTION_AfterInsert]    Script Date: 09.06.2016 12:09:54 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[T_PERIOD_SECTION_AfterInsert]'))
DROP TRIGGER [dbo].[T_PERIOD_SECTION_AfterInsert]
GO

IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[T_PERIOD_SECTION_AfterInsert]'))
EXEC dbo.sp_executesql @statement = N'--Добавление записей:
CREATE TRIGGER [dbo].[T_PERIOD_SECTION_AfterInsert] ON [dbo].[T_PERIOD_SECTION]
AFTER INSERT 
AS
DECLARE @fieldname  varchar(50)
declare @xml xml, @PrimaryKeyName varchar(255), @KeyType varchar(100), @BaseEntityID int
select top 1 @fieldname = t1.PeriodAlias
from inserted t1

declare @sql nvarchar(max)
set @sql = ''ALTER TABLE T_MONTH_VALUES ADD '' + @fieldname + '' DECIMAL(20,6) NULL'' 
exec sp_executesql @sql' 
GO
