ALTER FUNCTION [dbo].[ProCostoTotal](@codbus    VARCHAR(200), 
                                     @codreceta INT, 
                                     @fecha     DATE)
RETURNS DECIMAL(18, 4)
AS
     BEGIN
         DECLARE @resultado AS DECIMAL(18,4);

         SET @resultado = CASE
                              WHEN MONTH(@fecha) = 1 THEN
							 (SELECT isnull(costoUni,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 2 THEN
							 (SELECT isnull(costoUni2,0)FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 3 THEN
							 (SELECT isnull(costoUni3,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 4 THEN
							 (SELECT isnull(costoUni4,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 5 THEN
							 (SELECT isnull(costoUni5,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 6 THEN
							 (SELECT isnull(costoUni6,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 7 THEN
							 (SELECT isnull(costoUni7,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 8 THEN
							 (SELECT isnull(costoUni8,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) =9 THEN
							 (SELECT isnull(costoUni9,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 10 THEN
							 (SELECT isnull(costoUni10,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 11 THEN
							 (SELECT isnull(costoUni11,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

							  WHEN MONTH(@fecha) = 12 THEN
							 (SELECT isnull(costoUni12,0) FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))
				         END;

			--CASE 

			-- WHEN MONTH(@fecha) = 1 THEN
			-- SET @resultado = (SELECT costoUnidad FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

			-- WHEN MONTH(@fecha) = 2 THEN
			--  SET @resultado = (SELECT costoUnidad2 FROM proReceta WHERE codArticulo = @codbus AND codReceta = @codreceta and periodo = year(@fecha))

		 --END

         RETURN @resultado;
     END