
INSERT INTO AKUZ.T_MEDICAL_EPISODE (MedicalEpisodeID, TNMS, SetDTClinicalDiagnosis, OutcomeRecord, AmbulanceEvent, PaymentType, Mkb, Diagnosis, Descr, type_)
SELECT MedicalExaminationDataID, TNMS, SetDTClinicalDiagnosis, OutcomeRecord, AmbulanceEvent, PaymentType, Mkb, Diagnosis, Descr, 
(SELECT top 1 e.EntityID FROM VCLib.T_ENTITIES e WHERE e.EntName = 'MEDICAL_EXAMINATION_DATA' and e.[Schema] = 'AKUZ')
FROM AKUZ.T_MEDICAL_EXAMINATION_DATA;

EXEC sp_rename 'AKUZ.T_MEDICAL_EXAMINATION_DATA.MedicalExaminationDataID', 'MedicalEpisodeID', 'COLUMN';

ALTER TABLE [AKUZ].[T_MEDICAL_EXAMINATION_DATA] ADD CONSTRAINT [FK_T_MEDICAL_EPISODE_T_MEDICAL_EXAMINATION_DATA_MedicalEpisodeID]
FOREIGN KEY([MedicalEpisodeID]) REFERENCES [AKUZ].[T_MEDICAL_EPISODE] ([MedicalEpisodeID])
GO




ALTER TABLE AKUZ.T_DIVISION_SIGNIFICANT_EVENTS ALTER COLUMN MovementsDivisionCard uniqueidentifier NULL
GO

INSERT INTO AKUZ.T_DIVISION_SIGNIFICANT_EVENTS (DivisionSignificantEventsID, PostingDate, Post, MedicalEpisode, type_)
SELECT ChangeDiagnosisInPolyclinicEventID, PostingDate, Post, MedicalExaminationData, 
(SELECT top 1 e.EntityID FROM VCLib.T_ENTITIES e WHERE e.EntName = 'CHANGE_DIAGNOSIS_IN_POLYCLINIC_EVENT' and e.[Schema] = 'AKUZ')
FROM AKUZ.T_CHANGE_DIAGNOSIS_IN_POLYCLINIC_EVENT;

EXEC sp_rename 'AKUZ.T_CHANGE_DIAGNOSIS_IN_POLYCLINIC_EVENT.ChangeDiagnosisInPolyclinicEventID', 'DivisionSignificantEventsID', 'COLUMN';

ALTER TABLE [AKUZ].[T_CHANGE_DIAGNOSIS_IN_POLYCLINIC_EVENT] ADD CONSTRAINT [FK_T_DIVISION_SIGNIFICANT_EVENTS_T_CHANGE_DIAGNOSIS_IN_POLYCLINIC_EVENT_DivisionSignificantEventsID]
FOREIGN KEY([DivisionSignificantEventsID]) REFERENCES [AKUZ].[T_DIVISION_SIGNIFICANT_EVENTS] ([DivisionSignificantEventsID])
GO



UPDATE AKUZ.T_NURSE_MEDICAL_EXAMINATION
SET MedicalEpisode = MedicalExaminationData



UPDATE AKUZ.T_ASSOCIATED_AND_COMPLICATION_DISEASE
SET MedicalEpisode = MedicalExaminationData



ALTER TABLE AKUZ.T_POST_TREAT ALTER COLUMN MovementsDivisionCard uniqueidentifier NULL
GO

INSERT INTO AKUZ.T_POST_TREAT (PostTreatID, BeginCareDate, Post, MedicalEpisode)
SELECT NEWID(), PostingDate, Post, [MedicalEpisodeID]
FROM AKUZ.T_MEDICAL_EXAMINATION_DATA;