viernes, 24 de mayo de 2013

Cursores en Oracle



Un cursor según la RAE es:

Cursor: m. inform. Indicador que se desplaza sobre la pantalla para indicar una posición.

Existen 2 tipos de cursores, los implícitos y los explícitos, cada uno con uno con diferente función, ya que uno solo trae un registro y el otros puede traer 2 o mas registros respectivamente.

Los  atributos de un cursor son:
·         %rowcount: cantidad de registros.
·         %found: encontró datos.
·         %notfound: no encontró datos.                                      Estos 3 son booleanos
·         %isopen: indica que el cursor esta abierto.

Hay 3 maneras de realizar un cursor
1.      Ciclo FOR: Interacción de 2 o mas objetos, persona-computador.
2.     OPEN, FETCH, CLOSE: Abre, obtiene y cierra el cursor.
3.     REF Cursor: Tipo de dato del valor del cursor.

Ejemplos:

Ciclo FOR
declare
  cursor nombre_cursor is
    select campo1, campo2
    from tabla;
begin
    for v_lit in nombre_cursor loop
    dbms_output.put_line ('campo1:'||v_lit.campo1||'campo2:'||v_lit.campo2);
  end loop;
end;

--La variable solo sirve dentro del ciclo FOR.

OPEN, FETCH, CLOSE
declare
  cursor nombre_cursor is
  select campo1, campo2
  from tabla;
 v_lit cur%rowtype;
 begin
  open nombre_cursor;
  loop
  fetch nombre_cursor into v_lit;
  exit when nombre_cursor%notfound;
dbms_output.put_line ('campo1:'||v_lit.campo1||'campo2:'||v_lit. campo2);
  end loop;
  close nombre_cursor;
end;


martes, 21 de mayo de 2013

Triggers con inserting, updating y deleting Oracle



CREATE OR REPLACE TRIGGER TR_AUDITA
BEFORE INSERT OR DELETE OR UPDATE
ON TABLA_REGISTRO
FOR EACH ROW
DECLARE
BEGIN
IF INSERTING  THEN 
:NEW.USUARIOMOD:=USER;
:NEW.FECHAMOD:=SYSDATE;
INSERT INTO TABLA_AUDITA VALUES(USER,SYSDATE,'INGRESO EN TABLA_REGISTRO','NOMBRE '||:NEW.NOMBRE);
END IF;
IF UPDATING THEN 
:NEW.USUARIOMOD:=USER;
:NEW.FECHAMOD:=SYSDATE;
   INSERT INTO TABLA_AUDITA VALUES(USER,SYSDATE,'ACTUALIZO EN TABLA_REGISTRO' ,'NOMBRE '||:OLD.NOMBRE||'NOMBRE '||:NEW.NOMBRE);
END IF;
IF   DELETING THEN 
   INSERT INTO TABLA_AUDITA VALUES(USER,SYSDATE,'BORRO EN TABLA_REGISTRO','NOMBRE '||:OLD.NOMBRE);
END IF;

END;
/

Hay que recordar que si vamos a insertar (inserting) no ponemos :old. ya que es un nuevo registro y por ende no existe.
Para updating ponemos :new y :old, ya que si actualizamos va a ser de un registro existente y de ello se va a derivar un registro nuevo.
En el deleting solo ponemos :old debido a que vamos a eliminar un registro viejo, no va a ser un registro nuevo ovbiamente.


viernes, 10 de mayo de 2013

Llamar a un reporte desde Oracle forms


PROCEDURE nombre_procedure IS

plid paramlist;
the_param varchar2(15) := 'tmpdata';

BEGIN
plid := get_parameter_list(the_param);
/* check if 'tmpdata' exists */
IF NOT id_null(plid) THEN
destroy_parameter_list(plid);
END IF;
/* if it does destroy it */
plid := create_parameter_list(the_param);
/* create it afresh */
add_parameter(plid, 'P_PROVINCIA', TEXT_PARAMETER,To_char(:nombre_bloque.campo_bloque));
/* associate the param in the form with the param in the report */
add_Parameter(plid, 'PARAMFORM', TEXT_PARAMETER, 'YES');
/* to suppress the parameter form displaying */
run_product(REPORTS, /* product name */
'nombre_reporte.rep', /* Oracle Reports module */
SYNCHRONOUS, /* communication mode */
RUNTIME, /* execution mode */
FILESYSTEM, /* location of the Reports module */
plid, /* handle to the parameter list */
null
);
END;

En el lienzo creamos un botón, a este botón le asignamos un trigger inteligente when-button-pressed y digitamos el sigiente código:

nombre_procedure;

Listo.