IF EXISTS (
    SELECT 1 
    FROM sys.indexes 
    WHERE name = 'IX_T_PATIENT_BENEFITS_Benefits'
)
	DROP INDEX IX_T_PATIENT_BENEFITS_Benefits ON AKUZ.T_PATIENT_BENEFITS;
GO

CREATE TABLE temp_pb_mkb
(
    PatientBenefitsID UNIQUEIDENTIFIER,
    Mkb               VARCHAR(20) NOT NULL
)
GO


INSERT INTO temp_pb_mkb (PatientBenefitsID, Mkb)
SELECT pb.PatientBenefitsID,
       mkb.MCBCode
FROM AKUZ.T_PATIENT_BENEFITS pb
JOIN akuz.T_MKB mkb ON mkb.MkbID = pb.Mkb
GO


ALTER TABLE AKUZ.T_PATIENT_BENEFITS
    DROP CONSTRAINT FK_T_MKB_T_PATIENT_BENEFITS_Mkb
GO


ALTER TABLE AKUZ.T_PATIENT_BENEFITS
    DROP COLUMN Mkb
GO


ALTER TABLE AKUZ.T_PATIENT_BENEFITS ADD Mkb varchar(20)
GO


UPDATE pb
SET Mkb = tmp.Mkb
FROM AKUZ.T_PATIENT_BENEFITS pb
JOIN temp_pb_mkb tmp ON tmp.PatientBenefitsID = pb.PatientBenefitsID
GO


DROP TABLE temp_pb_mkb
GO