0
Good afternoon, I have the following Tb environment.Client where a client has N processes in Tb.Process, N movements in Tb.Progress and N attachments in Tb.Annex.
I need to mount an sql statement that when deleting a client by code "ClienteID
", delete all linked processes, schedules and attachments.
Being that the Tb.Andamento and Tb.Anexo are linked to Tb.Processo pelo "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
perfect! thanks for the instructions
– Evandro
Note that a client may have more than one process. That must be why in the original @Evandro code there was a sub-query for each DELETE.
– José Diz
Indeed, if there is more than one process, a subselect or Join should be made
– Alexandre Cavaloti