
/*------ ATD 361575 ------*/
DECLARE v_count NUMBER:=0;
BEGIN
SELECT 1 INTO v_count FROM USER_TAB_COLUMNS WHERE UPPER(table_name) = 'ITEMCLICOMPLEMENTO' AND column_name = 'REFERENCIAITEM';
EXCEPTION
WHEN NO_DATA_FOUND THEN
  BEGIN
  EXECUTE IMMEDIATE 'ALTER TABLE ITEMCLICOMPLEMENTO ADD REFERENCIAITEM VARCHAR2(30)';
  EXECUTE IMMEDIATE 'UPDATE ITEMCLICOMPLEMENTO 
                        SET ITEMCLICOMPLEMENTO.REFERENCIAITEM = ( SELECT P.REFERENCIA
                                                                  FROM PEDICLICAD P
                                                                  WHERE ITEMCLICOMPLEMENTO.NUMPED = P.NUMPED
                                                                    AND P.REFERENCIA IS NOT NULL )';
  END;
END;