ALTER TABLE [AKUZ].[T_MOVEMENT_RESULT_VMP_SERVICE_LINK] DROP CONSTRAINT [FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_VMP_SERVICE_LINK_V022PatientModel]
GO

ALTER TABLE [AKUZ].[T_MOVEMENT_RESULT] DROP CONSTRAINT [FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_V022PatientModel]
GO

ALTER TABLE [AKUZ].[T_V022_PATIENT_MODEL] DROP CONSTRAINT [PK_T_V022_PATIENT_MODEL]
GO

ALTER TABLE [AKUZ].[T_V022_PATIENT_MODEL] ADD [V022PatientModelID] uniqueidentifier NULL
GO

UPDATE [AKUZ].[T_V022_PATIENT_MODEL] SET [V022PatientModelID] = newid()
GO

ALTER TABLE [AKUZ].[T_V022_PATIENT_MODEL] ALTER COLUMN [V022PatientModelID] uniqueidentifier NOT NULL
GO

ALTER TABLE [AKUZ].[T_V022_PATIENT_MODEL] ADD CONSTRAINT [PK_T_V022_PATIENT_MODEL]
PRIMARY KEY CLUSTERED ([V022PatientModelID]) ON [PRIMARY]
GO

exec sp_rename 'AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK.V022PatientModel', 'V022PatientModelOld', 'COLUMN'

ALTER TABLE [AKUZ].[T_MOVEMENT_RESULT_VMP_SERVICE_LINK] ADD [V022PatientModel] uniqueidentifier NULL
GO

ALTER TABLE [AKUZ].[T_MOVEMENT_RESULT_VMP_SERVICE_LINK] ADD CONSTRAINT [FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_VMP_SERVICE_LINK_V022PatientModel]
FOREIGN KEY([V022PatientModel]) REFERENCES [AKUZ].[T_V022_PATIENT_MODEL] ([V022PatientModelID])
GO

update mrv
set mrv.V022PatientModel = vpm.V022PatientModelID
from [AKUZ].[T_MOVEMENT_RESULT_VMP_SERVICE_LINK] mrv
inner join AKUZ.T_V022_PATIENT_MODEL vpm
    on vpm.IDMPAC = mrv.V022PatientModelOld

alter table [AKUZ].[T_MOVEMENT_RESULT_VMP_SERVICE_LINK] drop column V022PatientModelOld

exec sp_rename 'AKUZ.T_MOVEMENT_RESULT.V022PatientModel', 'V022PatientModelOld', 'COLUMN'

ALTER TABLE [AKUZ].[T_MOVEMENT_RESULT] ADD [V022PatientModel] uniqueidentifier NULL
GO

ALTER TABLE [AKUZ].[T_MOVEMENT_RESULT] ADD CONSTRAINT [FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_V022PatientModel]
FOREIGN KEY([V022PatientModel]) REFERENCES [AKUZ].[T_V022_PATIENT_MODEL] ([V022PatientModelID])
GO

update mr
set mr.V022PatientModel = vpm.V022PatientModelID
from [AKUZ].[T_MOVEMENT_RESULT] mr
inner join AKUZ.T_V022_PATIENT_MODEL vpm
    on vpm.IDMPAC = mr.V022PatientModelOld

alter table [AKUZ].[T_MOVEMENT_RESULT] drop column V022PatientModelOld

exec dbo.sp_AlterDropColumn '[AKUZ].[T_V022_PATIENT_MODEL]' , 'MPACNAME', 1, 'varchar(2000) NOT NULL'
GO

exec dbo.sp_AlterDropColumn '[AKUZ].[T_V022_PATIENT_MODEL]' , 'DATEBEG', 1, 'datetime NOT NULL'
GO

exec dbo.sp_AlterDropColumn '[AKUZ].[T_V022_PATIENT_MODEL]' , 'IDMPAC', 1, 'int NOT NULL'
GO

exec dbo.sp_AlterDropColumn '[AKUZ].[T_N002_OnkStad]' , 'KOD_St', 1, 'varchar(32) NULL'
GO

