Procedure delete join between tables

0

Good afternoon, I have the following Tb.Customer environment where a client has N processes in Tb.Process, N moves in Tb.Andamento, and N attachments in Tb.Annexo.

Ineedtomountansqlstatementthatdeletesaclientbythecode" ClienteID ", delete all linked processes, paths, and attachments. Since the Tb.Andamento and Tb.Anexo are linked to Tb.Processo by " NumeroProcesso "

CREATE PROCEDURE delete_Cliente

    @ClienteID int

AS
BEGIN
        SELECT * FROM Andamento WHERE NumeroProcesso IN         
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)
        SELECT * FROM Anexo WHERE NumeroProcesso IN         
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)
        SELECT * FROM ArquivoAnexoAcordo WHERE NumeroProcesso IN        
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)
        SELECT * FROM HistoricoNegociacao WHERE NumeroProcesso IN       
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)

        SELECT * FROM Cliente WHERE ClienteID = @ClienteID
        SELECT * FROM Processo WHERE ClienteID = @ClienteID


        DELETE Andamento WHERE NumeroProcesso IN                
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)
        DELETE Anexo WHERE NumeroProcesso IN                
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)
        DELETE ArquivoAnexoAcordo WHERE NumeroProcesso IN               
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)
        DELETE HistoricoNegociacao WHERE NumeroProcesso IN              
        (SELECT NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID)

        DELETE Processo WHERE ClienteID = @ClienteID
        DELETE Cliente WHERE ClienteID = @ClienteID

END
GO
    
asked by anonymous 05.12.2017 / 17:11

2 answers

0

The only problem that I saw really serious in your routine is that it does not have a transaction, that is, if an error occurs in one of the steps, the previous one will not be undone.

I included a transaction and did an improvement on the queries for a parameter and not a subquery. In fact, I think it will work

    CREATE PROCEDURE delete_Cliente

    @ClienteID int

   AS
   BEGIN

    Declare @NumProcesso varchar(50)

    SELECT @NumProcesso = NumeroProcesso FROM Processo WHERE ClienteID = @ClienteID

    BEGIN TRANSACTION

            DELETE ArquivoAnexoAcordo WHERE NumeroProcesso = @NumProcesso               
            DELETE HistoricoNegociacao WHERE NumeroProcesso = @NumProcesso
            DELETE Andamento WHERE NumeroProcesso = @NumProcesso
            DELETE Anexo WHERE NumeroProcesso = @NumProcesso

            DELETE Processo WHERE ClienteID = @ClienteID
            DELETE Cliente WHERE ClienteID = @ClienteID

    Commit transaction

    IF @@Error > 0
    BEGIN
        ROLLBACK TRANSACTION
        PRINT 'ERRO AO EXCLUIR CLIENTE'

    END

   END
    
05.12.2017 / 17:33
0

If a customer can have more than one process, then you need to be aware of that detail. The suggestion that follows first annotates all client processes for only after going deleting the rows of the tables.

-- código #1
CREATE PROCEDURE delete_Cliente
     @ClienteID int as
begin

-- anota todos os processos do cliente
declare @Processos_Cliente table (NumeroProcesso int primary key);

INSERT into @Processos_Cliente
  SELECT NumeroProcesso
    from Processo
    where ClienteID = @ClienteID;

BEGIN TRANSACTION;

-- apaga andamento
DELETE Andamento 
  where NumeroProcesso in (SELECT NumeroProcesso from @Processos_Cliente);

-- apaga anexo
DELETE Anexo
  where NumeroProcesso in (SELECT NumeroProcesso from @Processos_Cliente);

-- apaga linha(s) do processo
DELETE Processo
  where NumeroProcesso in (SELECT NumeroProcesso from @Processos_Cliente);

-- apaga linha do cliente
DELETE Cliente
  where ClienteID = @ClienteID;

COMMIT;

end;
go

Code # 1 contains the Client, Process, Progress and Attachment tables, which are described in the diagram.

    
05.12.2017 / 20:29