ALTER TABLE [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3] DROP CONSTRAINT FK_T_CN_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3]') AND name = N'UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote' and is_unique_constraint <> 1)
DROP INDEX [UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote] ON [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3]
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3]') AND name = N'UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote' and is_unique_constraint = 1)
ALTER TABLE [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3] DROP CONSTRAINT [UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote]
IF  EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3]') AND name = N'CardNote_UQ')
ALTER TABLE [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3] DROP COLUMN [CardNote_UQ]
GO

ALTER TABLE [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3] ADD [CardNote_UQ] AS (CASE WHEN [CardNote] IS NOT NULL THEN NULL ELSE [CdaDocumentProtocolextractRev3ID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote] ON [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3] ([CardNote] ASC, [CardNote_UQ] ASC) ON [PRIMARY]
GO

ALTER TABLE [AKUZ].[T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3] ADD CONSTRAINT [FK_T_CARD_NOTE_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote]
FOREIGN KEY([CardNote]) REFERENCES [AKUZ].[T_CARD_NOTE] ([MedicalRecordID])
GO

