DROP PROCEDURE Copiar_Temporal_A_OP_CTRLTURNDETProc
GO
CREATE PROCEDURE Copiar_Temporal_A_OP_CTRLTURNDETProc
(	@BATNBRTURN	UD_CORRELATIVOS,
	@SITEID		UD_IDSITE,
	@CORRNBR	INT
)
AS
BEGIN
	BEGIN TRAN

	DELETE 
	FROM OP_CTRLTURNDET 	
	WHERE BATNBRTURN=@BATNBRTURN AND SITEID=@SITEID 

	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Eliminando en OP_CTRLTURNDET'
		RETURN(1)
	END


	INSERT INTO OP_CTRLTURNDET(BATNBRTURN,SITEID,DATEPROCES,NBRTUR,SIDE,HOSENUMBER,INVTIDBARR,STARTREADING,ENDREADING,QTYSALES,QTYSERAF,QTYOTHER,TOTQTY)
	SELECT @BATNBRTURN,SITEID,DATEPROCES,NBRTUR,SIDE,HOSENUMBER,INVTIDBARR,STARTREADING,ENDREADING,QTYSALES,QTYSERAF,QTYOTHER,TOTQTY 
	FROM TMP_OP_CTRLTURNDET 
	WHERE CORRNBR=@CORRNBR AND SITEID=@SITEID 

	IF(@@ERROR!=0 OR @@ROWCOUNT=0)
	BEGIN
		ROLLBACK
		RAISERROR 20000 'Error Insertando en TMP_OP_CTRLTURNDET'
		RETURN(1)
	END

	COMMIT TRAN 
	RETURN(0)
END 

