--T_EHR_RUS_42Q2UOMG02
ALTER TABLE [EHR].[T_EHR_RUS_42Q2UOMG02] DROP CONSTRAINT [PK_T_EHR_RUS_42Q2UOMG02]
go
ALTER TABLE [EHR].[T_EHR_RUS_42Q2UOMG02] ADD [EhrRusID_int] int NULL
go
UPDATE [EHR].[T_EHR_RUS_42Q2UOMG02] SET [EhrRusID_int] = [IdEHR]
go
ALTER TABLE [EHR].[T_EHR_RUS_42Q2UOMG02] DROP COLUMN [EhrRusID]
go
EXEC sp_rename '[EHR].[T_EHR_RUS_42Q2UOMG02].[EhrRusID_int]', 'EhrRusID', 'COLUMN'
go
ALTER TABLE [EHR].[T_EHR_RUS_42Q2UOMG02] ALTER COLUMN [EhrRusID] int NOT NULL
go
ALTER TABLE [EHR].[T_EHR_RUS_42Q2UOMG02] ADD CONSTRAINT [PK_T_EHR_RUS_42Q2UOMG02]
PRIMARY KEY CLUSTERED ([EhrRusID])
go

INSERT INTO dbo.T_ENTITY_IDS(EntName,FieldName,id)
SELECT 'EHR.EHR_RUS_42Q2UOMG02', 'PrimaryKey', 4
WHERE NOT EXISTS (
    SELECT 1
    FROM dbo.T_ENTITY_IDS
    WHERE EntName = 'EHR.EHR_RUS_42Q2UOMG02'
      AND FieldName = 'PrimaryKey'
)
go

--T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol

drop table ehr.T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol
go
delete from ehr.T_EHR_REFERENCE where EhrEntity = (select EntityID from vclib.T_ENTITIES where EntName = 'EHR_RUS_Groupi_risk_razvitiia_prof_zabol')
go
delete from ehr.T_EHR_RUS_REF_BOOKS where Entities = (select EntityID from vclib.T_ENTITIES where EntName = 'EHR_RUS_Groupi_risk_razvitiia_prof_zabol')
go

CREATE TABLE [EHR].[T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol]
(
    [EhrRusID] int NOT NULL,
    [IdEHR] int NULL,
    [NameEHR] varchar(255) NULL,
    [DESCRIPTION] varchar(255) NULL,
    [EhrRusRefBooks] int NOT NULL
)
ON [PRIMARY]
GO
ALTER TABLE [EHR].[T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol] ADD CONSTRAINT [PK_T_EHR_RUS_GROUPI_RISK_RAZVITIIA_PROF_ZABOL]
PRIMARY KEY CLUSTERED ([EhrRusID]) ON [PRIMARY]
GO
ALTER TABLE [EHR].[T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol] ADD CONSTRAINT [FK_T_EHR_RUS_REF_BOOKS_T_EHR_RUS_GROUPI_RISK_RAZVITIIA_PROF_ZABOL_EhrRusRefBooks]
FOREIGN KEY([EhrRusRefBooks]) REFERENCES [EHR].[T_EHR_RUS_REF_BOOKS] ([EhrRusRefBooksID])
GO

--T_EHR_RUS_Vidi_sporta

DROP TABLE ehr.T_EHR_RUS_Vidi_sporta
go
delete from ehr.T_EHR_REFERENCE where EhrEntity = (select EntityID from vclib.T_ENTITIES where EntName = 'EHR_RUS_Vidi_sporta')
go
delete from ehr.T_EHR_RUS_REF_BOOKS where Entities = (select EntityID from vclib.T_ENTITIES where EntName = 'EHR_RUS_Vidi_sporta')
go
CREATE TABLE [EHR].[T_EHR_RUS_Vidi_sporta]
(
    [EhrRusID] int NOT NULL,
    [IdEHR] int NULL,
    [NameEHR] varchar(255) NULL,
    [CodeEHR] varchar(255) NULL,
    [PART] varchar(255) NULL,
    [NORMATIVE] int NULL,
    [ACTUAL] bit NOT NULL,
    [EhrRusRefBooks] int NOT NULL
)
ON [PRIMARY]
GO
ALTER TABLE [EHR].[T_EHR_RUS_Vidi_sporta] ADD CONSTRAINT [PK_T_EHR_RUS_VIDI_SPORTA]
PRIMARY KEY CLUSTERED ([EhrRusID]) ON [PRIMARY]
GO
ALTER TABLE [EHR].[T_EHR_RUS_Vidi_sporta] ADD CONSTRAINT [FK_T_EHR_RUS_REF_BOOKS_T_EHR_RUS_VIDI_SPORTA_EhrRusRefBooks]
FOREIGN KEY([EhrRusRefBooks]) REFERENCES [EHR].[T_EHR_RUS_REF_BOOKS] ([EhrRusRefBooksID])
GO
