ALTER TABLE [AKUZ].[T_HOSPITAL_DESTINATION] ADD [ExecutionDivision] uniqueidentifier NULL
GO

UPDATE  hd
SET hd.[ExecutionDivision] = r.Division
from [AKUZ].[T_HOSPITAL_DESTINATION] hd
join AKUZ.T_MEDICAL_RECORD mr on mr.MedicalRecordID = hd.MedicalRecordID
join AKUZ.T_RESOURCE r on r.ResourceID = mr.Post
GO

ALTER TABLE [AKUZ].[T_HOSPITAL_DESTINATION] ALTER COLUMN [ExecutionDivision] uniqueidentifier NOT NULL
GO

ALTER TABLE [AKUZ].[T_HOSPITAL_DESTINATION] ADD CONSTRAINT [FK_T_DIVISION_T_HOSPITAL_DESTINATION_ExecutionDivision]
FOREIGN KEY([ExecutionDivision]) REFERENCES [AKUZ].[T_DIVISION] ([DivisionID])
GO
