Código SQL:
Ver original
ALTER PROCEDURE [dbo].[P5rADCArticuloCaract] (@Empresa utClave ,@Clase utClave ,@Grupo utClave ,@Familia utClave ,@Articulo utClave) AS DECLARE @Anio utEntero, @Periodo utEntero, @IdPeriodo utClave SET nocount ON SELECT @IdPeriodo = Valor FROM P5Sistema WHERE Parametro = 'PeriodoActual' AND Empresa = @Empresa AND Modulo = 'ADI' SELECT @Anio = Anio, @Periodo = Periodo FROM Periodo WHERE id = @IdPeriodo IF (isnull(@Clase,'')<>'' OR isnull(@Grupo,'')<>'' OR isnull(@Familia,'')<>'' OR isnull(@Articulo,'')<>'') BEGIN SELECT A.ArticuloFamilia, A.Clave AS Articulo, A.Texto1 AS NoParte, A.ArticuloTipo, A.texto2 AS NotasCompras, A.Descripcion, A.ArticuloClase, AC.CostoPromedio, isnull((SELECT Top 1 (1-(ED.pctDescuento/100))*ED.Precio FROM EntradaDetalle ED, EntradaEncabezado EE WHERE ED.Empresa = EE.Empresa AND ED.Folio = EE.Folio AND EE.Documento = 'ECPAM' AND EE.Estatus = 'A' AND ED.Articulo = A.Clave ORDER BY EE.FechaCaptura DESC),0) AS UltimoCosto, isnull((SELECT SUM(Existencia) FROM ArticuloExistencia WHERE Empresa = @Empresa AND Articulo = A.Clave AND IdPeriodo = @IdPeriodo AND Almacen LIKE '0%'),0) AS Existencia, isnull((SELECT SUM(OrdenadaVenta) FROM ArticuloSaldoAlmacen WHERE Articulo = A.Clave),0)AS OrdenadaVenta, A.MaximoAlmacenable AS Maximo, A.MinimoSeguridad AS Minimo, A.PuntoReorden, A.ArticuloGrupo, A.Numero1 AS Precio1, A.Numero2 AS Precio2, A.Numero3 AS Precio3, isnull((SELECT Top 1 FE.Fecha FROM FacturaEncabezado FE, FacturaDetalle FD WHERE FE.Empresa = FD.Empresa AND FE.Folio = FD.Folio AND FE.Empresa = @Empresa AND FD.Articulo = A.Clave AND FE.Estatus='A' ORDER BY FechaCaptura DESC),'19990101') AS FechaUltMov FROM ArticuloCosto AC, Articulo A WHERE A.Clave = AC.Articulo AND AC.Anio = @Anio AND AC.Periodo = @Periodo ORDER BY A.Clave END ELSE BEGIN SELECT A.ArticuloFamilia, A.Clave AS Articulo, A.Texto1 AS NoParte, A.ArticuloTipo, A.texto2 AS NotasCompras, A.Descripcion, A.ArticuloClase, AC.CostoPromedio, isnull((SELECT Top 1 (1-(ED.pctDescuento/100))*ED.Precio FROM EntradaDetalle ED, EntradaEncabezado EE WHERE ED.Empresa = EE.Empresa AND ED.Folio = EE.Folio AND EE.Documento = 'ECPAM' AND EE.Estatus = 'A' AND ED.Articulo = A.Clave ORDER BY EE.FechaCaptura DESC),0) AS UltimoCosto, isnull((SELECT SUM(Existencia) FROM ArticuloExistencia WHERE Empresa = @Empresa AND Articulo = A.Clave AND IdPeriodo = @IdPeriodo AND Almacen LIKE '0%'),0) AS Existencia, isnull((SELECT SUM(OrdenadaVenta) FROM ArticuloSaldoAlmacen WHERE Articulo = A.Clave),0)AS OrdenadaVenta, A.MaximoAlmacenable AS Maximo, A.MinimoSeguridad AS Minimo, A.PuntoReorden, A.ArticuloGrupo, A.Numero1 AS Precio1, A.Numero2 AS Precio2, A.Numero3 AS Precio3, isnull((SELECT Top 1 FE.Fecha FROM FacturaEncabezado FE, FacturaDetalle FD WHERE FE.Empresa = FD.Empresa AND FE.Folio = FD.Folio AND FE.Empresa = @Empresa AND FD.Articulo = A.Clave AND FE.Estatus='A' ORDER BY FechaCaptura DESC),'19990101') AS FechaUltMov INTO #ArticuloCaracteristica FROM ArticuloCosto AC, Articulo A WHERE A.Clave = AC.Articulo AND AC.Anio = @Anio AND AC.Periodo = @Periodo ORDER BY A.Clave IF isnull(@Clase,'') <> '' BEGIN DELETE #ArticuloCaracteristica WHERE ArticuloClase <> @Clase END IF isnull(@Grupo,'')<> '' BEGIN DELETE #ArticuloCaracteristica WHERE ArticuloGrupo <> @Grupo END IF isnull(@Familia,'')<> '' BEGIN DELETE #ArticuloCaracteristica WHERE ArticuloFamilia <> @Familia END IF isnull(@Articulo,'')<> '' BEGIN DELETE #ArticuloCaracteristica WHERE Articulo NOT LIKE ltrim(rtrim(@Articulo))+'%' END SELECT * FROM #ArticuloCaracteristica END