Sistema de gestão ELIGO

Como excluir todos os registros de movimentos por SQL?

ELIGO.FAQ-77959
Comandos para excluir todos os registros de:

Atenção
É aconselhado fazer um backup dos dados antes da execução dos comandos!

Não existe possibilidade de retorno ou recuperação das informações após a execução dos comandos!



Exclusão de movimentos:

Utilizar a RTDBUT.UTL_DB_SQL (Executor de SQL e DML) para execução do comandos a seguir:
delete from custos_produtos;
delete from movimentos_estoques;
delete from operacoes_fiscais_itens_documento;
delete from itens_doc_entradas_saidas_sub;
delete from historicos_pedidos;
delete from importacoes_nfe_entrada;
delete from itens_doc_entradas_saidas_imp;
delete from itens_doc_entradas_saidas_imp_ad;
delete from formacoes_precos;
delete from itens_doc_entradas_saidas;
delete from reservas_produtos_pedidos;
delete from itens_romaneios;
delete from atendimento_item_pedido;
delete from itens_pedidos;
delete from pedidos_carregamentos;
delete from produtos_contados;
delete from resultados_contagens;
update doc_entradas_saidas set ultima_conferencia_id = null where ultima_conferencia_id is not null;
delete from documentos_contagens;
delete from contagens_produtos;
delete from produtos_pedidos;
delete from pedidos_titulos;
delete from pedidos;
delete from operacoes_fiscais_documentos;
delete from movimentacao_contabil;
update movimentos_financeiros set lancamento_id = null;
update lancamentos set movimento_financeiro_id = null;
update retornos_bancarios set movimento_financeiro_titulo_id = null;
update movimentos_financeiros set retorno_bancario_id = null;
delete from retornos_bancarios;
delete from movimentos_financeiros;
delete from lancamentos;
delete from titulos;
delete from doc_entradas_saidas_pagamentos;
delete from doc_vinculado_doc_entradas_saidas;
delete from doc_entradas_saidas_pessoas_relacionadas;
delete from doc_entradas_saidas_apuracoes;
delete from eventos_documentos;
delete from doc_nfe_inutilizados;
delete from doc_entradas_saidas_eventos;
delete from doc_entradas_saidas;
delete from estoques_produtos;

Exclusão de produtos cadastrados:

delete from codigos_barras where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030));
delete from alteracoes_itens_colecoes where item_colecao_id in (select id from itens_colecoes where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030)));
delete from itens_colecoes where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030));
delete from historicos_produtos where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030));
delete from fichas_tecnicas_produtos where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030));
delete from chaves_produtos where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030));
delete from componentes_produtos where produto_id in (select id from produtos where tipo_produto_id not in (16301,93030));
delete from produtos where tipo_produto_id not in (16301,93030);
Observações:





Exclusão de todos os dados de um estabelecimento específico:


Remover FK dos itens de pedidos do estabelecimento desejado dos itens de documentos de outros estabelecimentos:
update itens_doc_entradas_saidas set item_pedido_id = null
where item_pedido_id in (
select i.id from itens_pedidos i
join pedidos p on p.id = i.pedido_id
where p.estabelecimento_id = 21931);

Remover FK dos itens de pedidos do estabelecimento desejado dos sub-itens de documentos de outros estabelecimentos:
update itens_doc_entradas_saidas_sub set item_pedido_id = null
where id in (
select s.id from itens_doc_entradas_saidas_sub s
join itens_pedidos i on i.id = s.item_pedido_id
join pedidos p on p.id = i.pedido_id
where p.estabelecimento_id = 21931);

delete from itens_doc_entradas_saidas_sub where id in (
select s.id from itens_doc_entradas_saidas_sub s
join itens_doc_entradas_saidas i on i.id = s.item_documento_id
join doc_entradas_saidas d on d.id = i.documento_id
where d.estabelecimento_id = 18772);

delete from importacoes_nfe_entrada where id in (
select e.id from importacoes_nfe_entrada e
join doc_entradas_saidas d on d.id = e.documento_id
where d.estabelecimento_id = 18772);

delete from etiquetas where id in (
select e.id from etiquetas e
join itens_doc_entradas_saidas i on i.id = e.item_documento_id
join doc_entradas_saidas d on d.id = i.documento_id
where estabelecimento_id = 18772);

delete from documentos_cupons_fiscais where id in (
select c.id from documentos_cupons_fiscais c
join doc_entradas_saidas d on d.id = c.documento_id
where estabelecimento_id = 18772);

delete from doc_nfe_inutilizados where estabelecimento_id = 18772;

delete from doc_entradas_saidas_apuracoes where id in (
select a.id from doc_entradas_saidas_apuracoes a
join doc_entradas_saidas d on d.id = a.documento_id
where d.estabelecimento_id = 18772);

delete from atendimento_item_pedido where id in (
select a.id from atendimento_item_pedido a
join itens_pedidos i on i.id = a.atende_item_id
join pedidos p on p.id = i.pedido_id
where p.estabelecimento_id = 18772);

Itens de pedidos:
delete from itens_pedidos where id in (
select i.id from itens_pedidos i 
join pedidos p on p.id = i.pedido_id
where p.estabelecimento_id = 21931);

delete from pedidos_financeiros where id in (
select f.id from pedidos_financeiros f
join pedidos p on p.id = f.pedido_id 
where p.estabelecimento_id = 21931);

delete from pedidos_titulos where id in (
select f.id from pedidos_titulos f
join pedidos p on p.id = f.pedido_id  
where p.estabelecimento_id = 21931);

delete from pedidos_titulos where id in (
select f.id from pedidos_titulos f
join titulos t on t.id = f.titulo_id
where t.estabelecimento_id = 18772);


Remover FK dos pedidos do estabelecimento desejado dos itens de documentos de outros estabelecimentos:
update itens_doc_entradas_saidas set pedido_id = null where id in (
select i.id from itens_doc_entradas_saidas i
join pedidos p on p.id = i.pedido_id
where p.estabelecimento_id = 21931);

delete from historicos_pedidos where id in (
select h.id from historicos_pedidos h
join pedidos p on p.id = h.pedido_id
where p.estabelecimento_id = 18772);

delete from pedidos where estabelecimento_id = 21931;

delete from custos_produtos where estabelecimento_id = 18772;

delete from movimentos_estoques where id in (
select m.id from movimentos_estoques m
join itens_doc_entradas_saidas i on i.id = m.item_documento_id
join doc_entradas_saidas d on d.id = i.documento_id
where d.estabelecimento_id = 18772);

delete from operacoes_fiscais_documentos where id in (
select o.id from operacoes_fiscais_documentos o
join doc_entradas_saidas d on d.id = o.documento_id
where d.estabelecimento_id = 21931);

delete from pedidos_titulos where id in (
select f.id from pedidos_titulos f
join doc_entradas_saidas d on d.id = f.documento_id
where d.estabelecimento_id = 21931);

delete from doc_entradas_saidas_eventos where id in (
select e.id from doc_entradas_saidas_eventos e
join doc_entradas_saidas d on d.id = e.documento_id
where d.estabelecimento_id = 21931);

delete from operacoes_fiscais_itens_documento where id in (
select o.id from operacoes_fiscais_itens_documento o
join itens_doc_entradas_saidas i on i.id = o.item_documento_id
join doc_entradas_saidas d on d.id = i.documento_id
where d.estabelecimento_id = 18772);

delete from doc_entradas_saidas where estabelecimento_id = 21931;

update lancamentos set movimento_financeiro_id = null where id in (
select l.id from lancamentos l
join movimentos_financeiros m on m.id = l.movimento_financeiro_id
join titulos t on t.id = m.titulo_id
where t.estabelecimento_id = 21931);

delete from movimentos_financeiros where id in (
select m.id from movimentos_financeiros m
join titulos t on t.id = m.titulo_id
where t.estabelecimento_id = 21931);

delete from titulos where estabelecimento_id = 21931;

delete from lancamentos where estabelecimento_id = 21931;

delete from pedidos_financeiros 
where conta_corrente_id in  (
select id from contas_correntes where estabelecimento_id = 18772);

delete from contas_correntes_estabelecimentos where estabelecimento_id = 18772;

update titulos set lop_id = (select id from lops where lop = 'CARTEIRA')  
where lop_id in (
select id from lops where estabelecimento_id = 18772);
delete from lops where estabelecimento_id = 18772;

delete from contas_correntes where estabelecimento_id = 18772;

delete from colaboradores where estabelecimento_id = 21931;

delete from contas_contabeis where estabelecimento_id = 21931;

delete from estatisticas_documentos_produtos where estabelecimento_id = 21931;

delete from estoques_produtos where estabelecimento_id = 21931;

delete from lotes_nfse where estabelecimento_id = 21931;

delete from lotes_nfe where estabelecimento_id = 21931;

delete from numeros_doc_es where estabelecimento_id = 21931;

delete from saldos_diarios_estoques where estabelecimento_id = 21931;

delete from saldos_mensais_est_prod where estabelecimento_id = 21931;

delete from tipos_pedidos_estabelecimento where estabelecimento_id = 21931;

delete from usuarios_estabelecimentos where estabelecimento_id = 21931;