CREATE TABLE [AKUZ].[T_IPRA2_RHB_RES]
(
    [Ipra2RhbResID] uniqueidentifier NOT NULL,
    [NAME] varchar(1024) NULL,
    [SHNAME] varchar(64) NOT NULL
)
ON [PRIMARY]
GO

ALTER TABLE [AKUZ].[T_IPRA2_RHB_RES] ADD CONSTRAINT [PK_T_IPRA2_RHB_RES]
PRIMARY KEY CLUSTERED ([Ipra2RhbResID]) ON [PRIMARY]
GO

insert into AKUZ.T_IPRA2_RHB_RES (Ipra2RhbResID, [NAME], SHNAME)
select NEWID() as Ipra2RhbResID
    , case when irr.[SHNAME] = 'не выполнено' then irr.[NAME] else null end as [NAME]
    , irr.[SHNAME] as [SHNAME]
from AKUZ.T_IPRA_RHB_RES irr

ALTER TABLE [AKUZ].[T_IPRA2_EXECUTION_EVENT] ADD [Result] uniqueidentifier NOT NULL
GO

ALTER TABLE [AKUZ].[T_IPRA2_EXECUTION_EVENT] ADD CONSTRAINT [FK_T_IPRA2_RHB_RES_T_IPRA2_EXECUTION_EVENT_Result]
FOREIGN KEY([Result]) REFERENCES [AKUZ].[T_IPRA2_RHB_RES] ([Ipra2RhbResID])
GO