CREATE PROCEDURE CP_Cbtes_Lote_Cambiar_OnLine (
	@Id_Trans					INT,	
	@Lote						VARCHAR(20)
	) 
AS
 
DECLARE @LoteAnt			VARCHAR(20)
DECLARE @Mensaje			VARCHAR(100)

BEGIN TRY
BEGIN TRAN


	SET @LoteAnt =	ISNULL((SELECT Lote FROM Cbtes WHERE Id_Trans = @Id_Trans),'')
	IF (SUBSTRING(@LoteAnt,1,3) NOT IN ('VW:','CW:'))
	BEGIN
		SELECT	 1					AS ErrorNumber 
				,0					AS ErrorSeverity  
				,0					AS ErrorState 
				,0					AS ErrorProcedure  
				,0					AS ErrorLine		
				,'El cbte no es Online'			AS ErrorMessage
		RETURN 1
	END 

    SET @LoteAnt = LTRIM(SUBSTRING(@LoteAnt,4,17) )

	UPDATE Cbtes SET Lote = 'VW:' + RTRIM(LTRIM(@Lote)) WHERE Lote = 'VW:' + RTRIM(LTRIM(@LoteAnt))

	UPDATE Cbtes SET Lote = 'CW:' + RTRIM(LTRIM(@Lote)) WHERE Lote = 'CW:' + RTRIM(LTRIM(@LoteAnt))

	COMMIT
	SELECT	 0					AS ErrorNumber 
			,0					AS ErrorSeverity  
			,0					AS ErrorState 
			,0					AS ErrorProcedure  
			,0					AS ErrorLine		
			,@Mensaje			AS ErrorMessage

END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo;  
	ROLLBACK
END CATCH;   

			
