
IF EXISTS (SELECT * FROM sys.indexes i WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_BENEFITS_V4]') AND name = N'UQ_T_CDA_SECTION_BENEFITS_V4_CdaAmbulatorySummaryDocumentV4' and is_unique_constraint <> 1
    AND NOT EXISTS (
        SELECT 1 
        FROM sys.index_columns ic
        JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
        WHERE ic.object_id = i.object_id 
        AND ic.index_id = i.index_id
        AND c.name = 'CdaAmbulatorySummaryDocumentV4_UQ'
    )
)
DROP INDEX [UQ_T_CDA_SECTION_BENEFITS_V4_CdaAmbulatorySummaryDocumentV4] ON [AKUZ].[T_CDA_SECTION_BENEFITS_V4]
GO

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_BENEFITS_V4]') AND name = N'CdaAmbulatorySummaryDocumentV4_UQ')
ALTER TABLE [AKUZ].[T_CDA_SECTION_BENEFITS_V4] ADD [CdaAmbulatorySummaryDocumentV4_UQ] AS (CASE WHEN [CdaAmbulatorySummaryDocumentV4] IS NOT NULL THEN NULL ELSE [CdaSectionBenefitsV4ID] END)
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_SECTION_BENEFITS_V4]') AND name = N'UQ_T_CDA_SECTION_BENEFITS_V4_CdaAmbulatorySummaryDocumentV4' and is_unique_constraint <> 1)
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_CDA_SECTION_BENEFITS_V4_CdaAmbulatorySummaryDocumentV4] ON [AKUZ].[T_CDA_SECTION_BENEFITS_V4] ([CdaAmbulatorySummaryDocumentV4] ASC, [CdaAmbulatorySummaryDocumentV4_UQ] ASC) ON [PRIMARY]
GO

