
DECLARE @EntId int
SET @EntId = (SELECT TOP 1 EntityId FROM VCLib.T_ENTITIES WHERE [Schema]='IES' and EntName='SCHET_SLUCH')

delete
from vclib.T_ENTITIES_ATTRIBUTES
where Name = 'PaymentsSluchs' and EntityID = @EntId

INSERT INTO VCLib.T_ENTITIES_ATTRIBUTES ([EntitiesAttributesId], [EntityID], [Index], [AttributeType], [Name], [Description], [SystemType], [MaxLength], [Precision], [Scale], [DefaultFormat], [IsCalculated], [IsFlag], [IsRequired], [Save], [LinkAttributeName], [PathsAttributes], [Filter], [IsCashed], [RequiredAttributes], [SortingAttribute], [EnumType], [IsIdentity], [IsCustom], [RemoveAction], [IsPrimaryKey], [ChoiceAttributeData], [ParametersData], [CustomsData], [MappingType], [MappingName], [IsStored], [MappingTable], [MappingRefTable], [RefTableSearchMode], [RefTableHashFunc], [AttrLexem], [IsAdditional])
SELECT newid(), @EntId, 5410070, 2, 'PaymentsSluchs', 'Случаи платёжных поручений', 'IES.PAYMENTS_SLUCH', NULL, NULL, NULL, NULL, 1, 0, 0, 1, NULL, '{SchetSluchAccomplished/PaymentsSluchs}', NULL, 0, '{SchetSluchAccomplished/PaymentsSluchs}', NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, 0, NULL, 1, NULL, NULL, NULL, NULL, NULL, 0
WHERE NOT EXISTS(SELECT * FROM VCLib.T_ENTITIES_ATTRIBUTES where EntityID = @EntId AND Name = 'PaymentsSluchs' AND IsAdditional = 0)

go

update t1
set SchetSluchAccomplished = ss.SchetSluchAccomplished
from ies.T_PAYMENTS_SLUCH t1
join ies.T_SCHET_SLUCH ss on (ss.SchetSluchID = t1.schetsluch)
where t1.SchetSluchAccomplished is null

go

alter table ies.T_PAYMENTS_SLUCH
alter column SchetSluchAccomplished uniqueidentifier not null

go

ALTER TABLE [IES].[T_PAYMENTS_SLUCH] DROP CONSTRAINT [FK_T_SCHET_SLUCH_T_PAYMENTS_SLUCH_SchetSluch]
GO

ALTER TABLE [IES].[T_PAYMENTS_SLUCH] DROP COLUMN [SchetSluch]
GO
