IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_BC_LANC_TRANSF_CANCEL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_BC_LANC_TRANSF_CANCEL]
GO
--P_BC_LANC_TRANSF_CANCEL 1, 502975
CREATE PROCEDURE P_BC_LANC_TRANSF_CANCEL @CD_EMP INT, @CD_BC_LANC_ORIG INT 
AS 
SET NOCOUNT ON
DECLARE @CD_BC_LANC_ORIGEM INT
DECLARE @CD_BC_LANC_DEST INT

SELECT
	 @CD_BC_LANC_ORIGEM=CD_BC_LANC_ORIG, 
	 @CD_BC_LANC_DEST=CD_BC_LANC_DEST
FROM
	BC_LANC_TRANSF
WHERE
	CD_BC_LANC_DEST = @CD_BC_LANC_ORIG
	or CD_BC_LANC_ORIG=@CD_BC_LANC_ORIG

UPDATE
	CHQ_LANC_CPL
SET 
	CHQ_LANC_CPL.CD_CNT=CHQ_LANC_CPL_TRANSF.CD_CNT_ORIG
FROM
	CHQ_LANC_CPL_TRANSF INNER JOIN CHQ_LANC_CPL ON
	CHQ_LANC_CPL_TRANSF.CD_EMP = CHQ_LANC_CPL.CD_EMP AND
	CHQ_LANC_CPL_TRANSF.CD_CHQ = CHQ_LANC_CPL.CD_CHQ 
WHERE
	CHQ_LANC_CPL_TRANSF.CD_EMP=@CD_EMP
	AND CHQ_LANC_CPL_TRANSF.CD_BC_LANC_ORIG =@CD_BC_LANC_ORIG

DELETE
FROM
	CHQ_LANC_CPL_TRANSF
WHERE
	CD_EMP=@CD_EMP
	AND CD_BC_LANC_DEST=@CD_BC_LANC_DEST

DELETE
FROM
	BC_LANC_TRANSF
WHERE
	CD_BC_LANC_DEST = @CD_BC_LANC_DEST


DELETE
FROM
	BC_LANC
WHERE
	CD_EMP=@CD_EMP
	AND CD_BC_LANC=@CD_BC_LANC_DEST

DELETE
FROM
	BC_LANC
WHERE
	CD_EMP=@CD_EMP
	AND CD_BC_LANC=@CD_BC_LANC_ORIGEM