ALTER TABLE AKUZ.T_SCHET_SLUCH_LINK_F014 DROP CONSTRAINT FK_T_F014_DENY_REASON_T_SCHET_SLUCH_LINK_F014_F014DENYREASON;
GO
ALTER TABLE AKUZ.T_HSCHET_SLUCH_LINK_F014 DROP CONSTRAINT FK_T_F014_DENY_REASON_T_HSCHET_SLUCH_LINK_F014_F014DENYREASON;
GO
ALTER TABLE AKUZ.T_F014_DENY_REASON DROP CONSTRAINT PK_T_F014_DENY_REASON;
GO


ALTER TABLE [AKUZ].[T_F014_DENY_REASON] ADD [F014DenyReasonID] uniqueidentifier NULL
GO
UPDATE [AKUZ].[T_F014_DENY_REASON] SET [F014DenyReasonID] = NEWID()
GO
ALTER TABLE [AKUZ].[T_F014_DENY_REASON] ALTER COLUMN [F014DenyReasonID] uniqueidentifier NOT NULL
GO
ALTER TABLE [AKUZ].[T_F014_DENY_REASON] ADD CONSTRAINT [PK_T_F014_DENY_REASON] PRIMARY KEY CLUSTERED ([F014DenyReasonID]) ON [PRIMARY]
GO


exec sp_rename 'AKUZ.T_SCHET_SLUCH_LINK_F014.F014DenyReason', 'F014DenyReason_old', 'COLUMN'
GO
exec sp_rename 'AKUZ.T_HSCHET_SLUCH_LINK_F014.F014DenyReason', 'F014DenyReason_old', 'COLUMN'
GO


ALTER TABLE [AKUZ].[T_SCHET_SLUCH_LINK_F014] ADD [F014DenyReason] uniqueidentifier NULL
GO

UPDATE sslf
SET sslf.[F014DenyReason] = fdr.F014DenyReasonID
from [AKUZ].[T_SCHET_SLUCH_LINK_F014] sslf
inner join AKUZ.T_F014_DENY_REASON fdr
    on fdr.Kod = sslf.F014DenyReason_old
GO

ALTER TABLE [AKUZ].[T_SCHET_SLUCH_LINK_F014] ALTER COLUMN [F014DenyReason] uniqueidentifier NOT NULL
GO

ALTER TABLE [AKUZ].[T_SCHET_SLUCH_LINK_F014] ADD CONSTRAINT [FK_T_F014_DENY_REASON_T_SCHET_SLUCH_LINK_F014_F014DenyReason]
FOREIGN KEY([F014DenyReason]) REFERENCES [AKUZ].[T_F014_DENY_REASON] ([F014DenyReasonID])
GO


ALTER TABLE [AKUZ].[T_HSCHET_SLUCH_LINK_F014] ADD [F014DenyReason] uniqueidentifier NULL
GO

UPDATE sslf
SET sslf.[F014DenyReason] = fdr.F014DenyReasonID
from [AKUZ].[T_HSCHET_SLUCH_LINK_F014] sslf
inner join AKUZ.T_F014_DENY_REASON fdr
    on fdr.Kod = sslf.F014DenyReason_old
GO

ALTER TABLE [AKUZ].[T_HSCHET_SLUCH_LINK_F014] ALTER COLUMN [F014DenyReason] uniqueidentifier NOT NULL
GO

ALTER TABLE [AKUZ].[T_HSCHET_SLUCH_LINK_F014] ADD CONSTRAINT [FK_T_F014_DENY_REASON_T_HSCHET_SLUCH_LINK_F014_F014DenyReason]
FOREIGN KEY([F014DenyReason]) REFERENCES [AKUZ].[T_F014_DENY_REASON] ([F014DenyReasonID])
GO


exec dbo.sp_AlterDropColumn '[AKUZ].[T_SCHET_SLUCH_LINK_F014]' , 'F014DenyReason_old', 0
GO
exec dbo.sp_AlterDropColumn '[AKUZ].[T_HSCHET_SLUCH_LINK_F014]' , 'F014DenyReason_old', 0
GO