create or replace procedure cambiaNombre (pViejo in varchar2,
                                          pNuevo in varchar2) IS
   CURSOR formulas Is
      select concepto, formula 
      from   concepto
      where  formula like '%'||pViejo||'%'
   for update of formula;

   vnuevo concepto.concepto%type;
begin
   insert into concepto 
   select cia, pNuevo, descripcion, grupo, cuenta, esDebitoCredito, 
          cuentaPuente, banco, efecto, formula, moneda, prioridad,
          tipo_movban, reporta, ordenImp
   FROM conceptO
   where concepto = pviejo;

   SELECT unique CONCEPTO INTO VNUEVO FROM CONCEPTO WHERE CONCEPTO= PNUEVO;
   dbms_output.put_line ('...... Se inserto concepto nuevo '||vnuevo||' ......');

   update CONCEPTONOCAJA 	set concepto	= pNuevo where concepto	= pViejo;
   update CONCEPTOVENTA 	set concepto	= pNuevo where concepto	= pViejo;
   update LIQCDETALLE  		set concepto	= pNuevo where concepto	= pViejo;
   update MOVIMIENTOCAJA  	set concepto	= pNuevo where concepto	= pViejo;
   update MOVIMIENTOCAJAHIST  	set concepto	= pNuevo where concepto	= pViejo;
   update MOVIMIENTOCAJATMP 	set concepto	= pNuevo where concepto	= pViejo;
   update MOVIMIENTOVENTA 	set concepto	= pNuevo where concepto	= pViejo;
   update MOVIMIENTOVENTAHIST 	set concepto	= pNuevo where concepto	= pViejo;
   update MOVIMIENTOVENTATMP 	set concepto	= pNuevo where concepto	= pViejo;
   update VENCONCEPTOCOBRO	set concepto	= pNuevo where concepto	= pViejo;
   update VENCONCEPTOCOBRO	set conceptoInt	= pNuevo where conceptoInt= pViejo;
   update VENDEPOSITO 		set concepto	= pNuevo where concepto	= pViejo;
   update VENDETCIERRE 		set concepto	= pNuevo where concepto	= pViejo;
   update VENDETDEPOSITO 	set concepto	= pNuevo where concepto	= pViejo;
   update VENDETDEPOSITO 	set conceptoPago= pNuevo where conceptoPago = pViejo;
   update VENPAGODOCUMENTOS 	set concepto	= pNuevo where concepto	= pViejo;
   update VENCONCEPTOVAL 	set concepto	= pNuevo where concepto	= pViejo;
   update VENCONCEPTOVAL 	set conceptoSalida  = pNuevo where conceptoSalida = pViejo;
   update VENPAGOVALORES 	set concepto	= pNuevo where concepto	= pViejo;
   update VENPELTARJETA 	set concepto	= pNuevo where concepto	= pViejo;
   update VENRETENCION 		set conceptoPago= pNuevo where conceptoPago = pViejo;
   update VENSALSERVICIO 	set concepto	= pNuevo where concepto	= pViejo;
   update VENTIPODOC 		set concepto    = pNuevo where concepto	= pViejo;
   update VENCLAVALE 		set conceptoEmi	= pNuevo where conceptoEmi= pViejo;
   update VENCLAVALE 		set conceptoLiq	= pNuevo where conceptoLiq= pViejo;
   update VENTIPODOC 		set conceptoGravado = pNuevo where conceptoGravado= pViejo;
   update VENTIPODOC 		set conceptoExento  = pNuevo where conceptoExento = pViejo;
   update VENTIPODOC 		set conceptoPago    = pNuevo where conceptoPago   = pViejo;
   update VENTIPODOC 		set conceptoCargos  = pNuevo where conceptoCargos = pViejo;
   update VENCONCEPTOMISC	set conceptoEntrada = pNuevo where conceptoEntrada= pViejo;
   update VENCONCEPTOMISC 	set conceptoSalida  = pNuevo where conceptoSalida = pViejo;
   update VENPARAMETROS         set entradaEfectivo = pnuevo where entradaEfectivo= pviejo;
   update VENPARAMETROS         set salidaEfectivo  = pnuevo where salidaEfectivo = pviejo;
   update CAJPARAMETRO          set conFaltante     = pnuevo where conFaltante    = pviejo;
   update CAJPARAMETRO          set conSobrante     = pnuevo where conSobrante    = pviejo;

   FOR f IN formulas
   LOOP
      update concepto
      set    formula = REPLACE(f.formula, pViejo, pNuevo)
      WHERE  current of formulas;
   END LOOP;

   delete from concepto
   where  concepto = pviejo;
exception
   when others then
      raise_application_error(-20001, 'ERROR concepto '||PNUEVO||': '||SQLERRM);
end;
/
