do $$
declare 
	_type integer := (select e.EntityID from vclib.T_ENTITIES e where e.EntName = 'RECIPE_FORM' and e.schema = 'AKUZ' limit 1);
	_sql text;
begin
	drop index if exists akuz.uq_t_recipe_serialnumberdeleted;
	drop index if exists akuz.UQ_T_RECIPE_SerialNumberDeleted_recipeForm;

	_sql := 'CREATE UNIQUE INDEX IF NOT EXISTS UQ_T_RECIPE_SerialNumberDeleted_recipeForm ON AKUZ.T_RECIPE(Serial, Number, Year) where Deleted = ''0'' and MedicalRecordType = ' || _type::text;
	
	execute _sql;
	
	_sql := 'CREATE UNIQUE INDEX IF NOT EXISTS uq_t_recipe_serialnumberdeleted ON AKUZ.T_RECIPE(Serial, Number) where Deleted = ''0'' and  MedicalRecordType <>' || _type::text;
	
	execute _sql;
end;
$$ language plpgsql;

