Skip to content

Instantly share code, notes, and snippets.

@thiago-vieira
Created April 11, 2021 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiago-vieira/7647878e140e660f4116afb942dadef1 to your computer and use it in GitHub Desktop.
Save thiago-vieira/7647878e140e660f4116afb942dadef1 to your computer and use it in GitHub Desktop.
Exemplo de tabela para fazer log de inclusão e exclusão de registros
-- schema and data test
create table log_table
( id integer,
tipo varchar2(30),
usuario integer,
setor integer,
data_inclusao date,
data_exclusao date,
id_anterior integer,
status_atual integer
);
insert into log_table values (42, 'Atendente', 1, 51, to_date('2017-11-01','yyyy-mm-dd'), null,null, 1);
insert into log_table values (43, 'Atendente', 2, 52, to_date('2017-11-01','yyyy-mm-dd'), null,null, 1);
insert into log_table values (44, 'Atendente', 3, 53, to_date('2017-11-01','yyyy-mm-dd'), null,null, 1);
-- na tabela de permissoes, usar insert if not exists e, somente se retornar 1 linha, gravar o log
-- excluir
update log_table set data_exclusao = sysdate
where tipo = 'Atendente' and usuario = 2 and setor = 52 and data_exclusao is null
/
-- excluir
update log_table set data_exclusao = sysdate
where tipo = 'Atendente' and usuario = 3 and setor = 53 and data_exclusao is null
/
-- incluir
DECLARE
ultimo_registro log_table%ROWTYPE;
BEGIN
select * into ultimo_registro from log_table
where id in (select max(id) from log_table
where tipo = 'Atendente' and usuario = 3 and setor = 53);
if ultimo_registro.data_exclusao is not null then
insert into log_table values ((select max(id)+1 from log_table),'Atendente',
3, 53, to_date('2017-11-02','yyyy-mm-dd'), null, ultimo_registro.id, 1);
update log_table set status_atual = 0 where id = ultimo_registro.id;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
insert into log_table values ((select max(id)+1 from log_table),'Atendente',
3, 53, to_date('2017-11-02','yyyy-mm-dd'), null, null, 1);
END;
/
select * from log_table order by id
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment