ALTER TABLE AKUZ.T_CDA_LINKDOCS_INFORMATION ALTER COLUMN DocumentNote varchar(max);

ALTER TABLE AKUZ.T_CDA_PROC_RESINSTR_V4 ALTER COLUMN ResultText varchar(max);

ALTER TABLE AKUZ.T_CDA_RESEARCH ALTER COLUMN Result varchar(max);

ALTER TABLE AKUZ.T_CDA_SECTION_MEDICAL_REPORT_DOCINFO ALTER COLUMN VaccinationsInformation varchar(max);
ALTER TABLE AKUZ.T_CDA_MSE_REFERRAL_SECTION_REGIME ALTER COLUMN MedicalDevices varchar(max);

ALTER TABLE AKUZ.T_CDA_SECTION_RESCONS_CONS_DISP ALTER COLUMN Conclusion varchar(max) NOT NULL;

ALTER TABLE AKUZ.T_CDA_SECTION_SOCANAM ALTER COLUMN EpidimiogicalAnamness varchar(max);

if exists (select * from AKUZ.T_CDA_SECTION_SUR_OPERATION where len(OperationResult) > 3000) raiserror('Уменьшить поле OperationResult в таблице T_CDA_SECTION_SUR_OPERATION', 16, 10);
ALTER TABLE AKUZ.T_CDA_SECTION_SUR_OPERATION ALTER COLUMN OperationResult varchar(3000);

ALTER TABLE AKUZ.T_CDA_SECTION_SUR_PLANNED ALTER COLUMN PreoperativeEpicrisis varchar(max);


ALTER TABLE AKUZ.T_CHANGE_DIAGNOSIS_EVENT ALTER COLUMN Diagnosis varchar(max) NOT NULL;

if exists (select * from AKUZ.T_CDA_SECTION_SCOPORG_ROUTE_TO_CONS where len(Comment) > 7000) raiserror('Уменьшить поле Comment в таблице T_CDA_SECTION_SCOPORG_ROUTE_TO_CONS', 16, 10);
ALTER TABLE AKUZ.T_CDA_SECTION_SCOPORG_ROUTE_TO_CONS ALTER COLUMN Comment varchar(7000);

if exists (select * from sys.indexes i where i.[object_id] = OBJECT_ID('AKUZ.T_CARD_NOTE', 'U') and i.name = 'IX_T_CARD_NOTE_ReadyToBeSent')
	drop index IX_T_CARD_NOTE_ReadyToBeSent on AKUZ.T_CARD_NOTE;
go
if exists (select * from AKUZ.T_CARD_NOTE where len(PluggableAttributes) > 2000) raiserror('Уменьшить поле PluggableAttributes в таблице T_CARD_NOTE', 16, 10);
ALTER TABLE AKUZ.T_CARD_NOTE ALTER COLUMN PluggableAttributes varchar(2000);

CREATE NONCLUSTERED INDEX [IX_T_CARD_NOTE_ReadyToBeSent] ON [AKUZ].[T_CARD_NOTE] ([ReadyToBeSent], [EmdrDocType], [IsDelayed])
INCLUDE([CardNoteRecipient], [CdaDocument], [Descr], [Division], [DocumentNumber], [EmdrTemplate], [MedicalRecordID], [PluggableAttributes], [WhereToSend]) WITH (SORT_IN_TEMPDB = ON, FILLFACTOR = 95);
go
ALTER TABLE AKUZ.T_CDA_SECTION_SUR_OPERATION ALTER COLUMN Description varchar(max);

if exists (select * from AKUZ.T_CDA_SECTION_SUR_PLANNED where len(OperationJustification) > 1000) raiserror('Уменьшить поле OperationJustification в таблице T_CDA_SECTION_SUR_PLANNED', 16, 10);
ALTER TABLE AKUZ.T_CDA_SECTION_SUR_PLANNED ALTER COLUMN OperationJustification varchar(1000);

if exists (select * from AKUZ.T_DEATH_OCCURENCE where len(Descr) > 2500) raiserror('Уменьшить поле Descr в таблице T_DEATH_OCCURENCE', 16, 10);
ALTER TABLE AKUZ.T_DEATH_OCCURENCE ALTER COLUMN Descr varchar(2500);

ALTER TABLE AKUZ.T_DEPARTINFO_SURGERY_CODING ALTER COLUMN SurOperationDescription varchar(max);


if exists (select * from AKUZ.T_DESTINATION where len(Descr) > 2000) raiserror('Уменьшить поле Descr в таблице T_DESTINATION', 16, 10);
ALTER TABLE AKUZ.T_DESTINATION ALTER COLUMN Descr varchar(2000);


if exists (select * from AKUZ.T_DIAGNOSIS where len(Descr) > 3000) raiserror('Уменьшить поле Descr в таблице T_DIAGNOSIS', 16, 10);
ALTER TABLE AKUZ.T_DIAGNOSIS ALTER COLUMN Descr varchar(3000);

if exists (select * from AKUZ.T_CDA_HOSP_EPICRIS_LAB_RESEARCH where len(Conclusion) > 7000) raiserror('Уменьшить поле Conclusion в таблице T_CDA_HOSP_EPICRIS_LAB_RESEARCH', 16, 10);
ALTER TABLE AKUZ.T_CDA_HOSP_EPICRIS_LAB_RESEARCH ALTER COLUMN Conclusion varchar(7000);


if exists (select * from AKUZ.T_CDA_LINKDOCS_SANATORY_CARD where len(DocumentNote) > 3000) raiserror('Уменьшить поле DocumentNote в таблице T_CDA_LINKDOCS_SANATORY_CARD', 16, 10);
ALTER TABLE AKUZ.T_CDA_LINKDOCS_SANATORY_CARD ALTER COLUMN DocumentNote varchar(3000);

if exists (select * from AKUZ.T_CDA_MSE_REFERRAL_DGN_DIAGNOSIS where len(DoctorDesc) > 4000) raiserror('Уменьшить поле DoctorDesc в таблице T_CDA_MSE_REFERRAL_DGN_DIAGNOSIS', 16, 10);
ALTER TABLE AKUZ.T_CDA_MSE_REFERRAL_DGN_DIAGNOSIS ALTER COLUMN DoctorDesc varchar(4000) NOT NULL;


if exists (select * from AKUZ.T_CDA_ONKO_DGN where len(ClinicalDgnDesc) > 5000) raiserror('Уменьшить поле ClinicalDgnDesc в таблице T_CDA_ONKO_DGN', 16, 10);
ALTER TABLE AKUZ.T_CDA_ONKO_DGN ALTER COLUMN ClinicalDgnDesc varchar(5000) NOT NULL;


if exists (select * from AKUZ.T_CDA_ONKO_SECTION_STATEDIS where len(StateDescription) > 7000) raiserror('Уменьшить поле StateDescription в таблице T_CDA_ONKO_SECTION_STATEDIS', 16, 10);
ALTER TABLE AKUZ.T_CDA_ONKO_SECTION_STATEDIS ALTER COLUMN StateDescription varchar(7000);


if exists (select * from AKUZ.T_CDA_PATOBJ_STAGE_EPICRISIS where len(Conclusion) > 4500) raiserror('Уменьшить поле Conclusion в таблице T_CDA_PATOBJ_STAGE_EPICRISIS', 16, 10);
ALTER TABLE AKUZ.T_CDA_PATOBJ_STAGE_EPICRISIS ALTER COLUMN Conclusion varchar(4500) NOT NULL; 


if exists (select * from AKUZ.T_CDA_PROC_RESLAB_V4 where len(ResultText) > 3000) raiserror('Уменьшить поле ResultText в таблице T_CDA_PROC_RESLAB_V4', 16, 10);
ALTER TABLE AKUZ.T_CDA_PROC_RESLAB_V4 ALTER COLUMN ResultText varchar(3000);


ALTER TABLE AKUZ.T_CDA_SECTION_CONSULTATION_ADDINFO ALTER COLUMN AnamnesisVita varchar(max);
ALTER TABLE AKUZ.T_CDA_SECTION_CONSULTATION_ADDINFO ALTER COLUMN SelfTreatment varchar(max);


ALTER TABLE AKUZ.T_CDA_SECTION_DOCINFO_MEDRECORD_EXTRACT ALTER COLUMN MedicalLaborRecom varchar(max);

if exists (select * from AKUZ.T_CDA_SECTION_MEDICAL_REPORT_DOCINFO where len(PastInfectiousDisasesInfo) > 7000) raiserror('Уменьшить поле PastInfectiousDisasesInfo в таблице T_CDA_SECTION_MEDICAL_REPORT_DOCINFO', 16, 10);
ALTER TABLE AKUZ.T_CDA_SECTION_MEDICAL_REPORT_DOCINFO ALTER COLUMN PastInfectiousDisasesInfo varchar(7000);

ALTER TABLE AKUZ.T_BENEFITS ALTER COLUMN RecipeValidationLexem varchar(max);

if exists (select * from AKUZ.S_ANALYSIS_ValidateLexem where len(Value) > 2500) raiserror('Уменьшить поле Value в таблице S_ANALYSIS_ValidateLexem', 16, 10);
ALTER TABLE AKUZ.S_ANALYSIS_ValidateLexem ALTER COLUMN Value varchar(2500);

ALTER TABLE AKUZ.T_CDA_AMBSV_VISIT_V4 ALTER COLUMN Conclusion varchar(max);

if exists (select * from AKUZ.T_CDA_PROC_RESCONS_V4 where len(ConclusionText) > 6000) raiserror('Уменьшить поле ConclusionText в таблице T_CDA_PROC_RESCONS_V4', 16, 10);
ALTER TABLE AKUZ.T_CDA_PROC_RESCONS_V4 ALTER COLUMN ConclusionText varchar(6000);

if exists (select * from AKUZ.T_CDA_PROC_RESCONS_V4 where len(ResultText) > 6000) raiserror('Уменьшить поле ResultText в таблице T_CDA_PROC_RESCONS_V4', 16, 10);
ALTER TABLE AKUZ.T_CDA_PROC_RESCONS_V4 ALTER COLUMN ResultText varchar(6000);

ALTER TABLE AKUZ.T_CDA_SECTION_EXAMINATION_MEDICAL ALTER COLUMN ResearchDescription varchar(max) NOT NULL;

ALTER TABLE AKUZ.T_CDA_SECTION_PATIENT_COMPLAINTS ALTER COLUMN PatientComplaints varchar(max) NOT NULL;
ALTER TABLE AKUZ.T_CDA_SECTION_RESINFO_DOCVISIT ALTER COLUMN Conclusion varchar(max) NOT NULL;


if exists (select * from AKUZ.T_DEPARTINFO_PROC_RESINFO where len(Conclusion) > 7000) raiserror('Уменьшить поле Conclusion в таблице T_DEPARTINFO_PROC_RESINFO', 16, 10);
ALTER TABLE AKUZ.T_DEPARTINFO_PROC_RESINFO ALTER COLUMN Conclusion varchar(7000) NOT NULL;

if exists (select * from AKUZ.T_DIAGNOSTICS_RESULTS where len(RecommendationText) > 7000) raiserror('Уменьшить поле RecommendationText в таблице T_DIAGNOSTICS_RESULTS', 16, 10);
ALTER TABLE AKUZ.T_DIAGNOSTICS_RESULTS ALTER COLUMN RecommendationText varchar(7000);


ALTER TABLE [AKUZ].[T_IPRA_PARSE_LPU_RULE] ALTER COLUMN [ParseRule] varchar(max) NOT NULL

if exists (select * from [AKUZ].[T_CDA_OPERATION_AKENEO] where len([OperationDescription]) > 1000)
	raiserror('Уменьшить поле [OperationDescription] в таблице [T_CDA_OPERATION_AKENEO]', 16, 10);
ALTER TABLE [AKUZ].[T_CDA_OPERATION_AKENEO] ALTER COLUMN [OperationDescription] varchar(1000) NOT NULL

-- [T_REMOTE_DIRECTION]
if exists (select * from [AKUZ].[T_REMOTE_DIRECTION] where len([Anamnesis]) > 6000)
	raiserror('Уменьшить поле [Anamnesis] в таблице [T_REMOTE_DIRECTION]', 16, 10);
ALTER TABLE [AKUZ].[T_REMOTE_DIRECTION] ALTER COLUMN [Anamnesis] varchar(6000) NULL