DROP PROCEDURE ReemplazoTurnoProc
GO
CREATE PROCEDURE ReemplazoTurnoProc
(
	@SITEID		UD_IDSITE,
	@DATEPROCE 	UD_FECHA,
	@NUMBERTURN 	TINYINT,
	@SALESPERID	UD_SALESPERSONID,
	@SALESPERIDOLD	UD_SALESPERSONID
)
AS
BEGIN
	BEGIN TRAN
		INSERT INTO OP_TURNVSSIDE(SITEID,DATEPROCE,NUMBERTURN,SALESPERID,SIDE,STATUS,SALESPERIDOLD)
		SELECT SITEID,DATEPROCE,NUMBERTURN,@SALESPERIDOLD,SIDE,1,SALESPERID
		FROM OP_TURNVSSIDE
		WHERE SITEID=@SITEID AND DATEPROCE=@DATEPROCE AND NUMBERTURN=@NUMBERTURN AND SALESPERID=@SALESPERID
		
		IF (@@error!=0 OR @@rowcount=0)
    			BEGIN
        			RAISERROR  20000 'Error Actualizando en OP_TURNVSSIDE'
        			ROLLBACK TRAN
        			RETURN(1)
    
    			END

		--ELIMINANDO
		DELETE 
		FROM OP_TURNVSSIDE
		WHERE SITEID=@SITEID AND DATEPROCE=@DATEPROCE AND NUMBERTURN=@NUMBERTURN AND SALESPERID=@SALESPERID
		
		IF (@@error!=0 OR @@rowcount=0)
    			BEGIN
        			RAISERROR  20000 'Error Eliminando en OP_TURNVSSIDE'
        			ROLLBACK TRAN
        			RETURN(1)
    
    			END

	COMMIT TRAN
	RETURN(0)
END 

select * from OP_TURNVSSIDE



