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