Últimos mensajes - Powered by IBM
- Normalización
- Normalización
- Error -2147221503 [ODBC SQL Server ...
- Sql Server Backups
- sql server
- informe de consumo mensual
- rangos de fecha
- error con instancia en sql server 2005 express ...
- Conexion via Programacion a SQL SERVER 2008 R2
- Exportar datos desde un archivo de Texto a SQL ...
Palabras claves
uso del Case en parte Where de un select
12/01/2006 - 08:19 por Juan Carlos | Informe spam
Hola lista, quizas lo que vaya a preguntar no se pueda hacer.
Tengo el siguiente query original:
Alter Procedure SALDSS_SaldosPorRangosdeFechas --
SALDSS_SaldosPorRangosdeFechas '00', '001', '211', Null, '2005/08/01',
'2005/09/01', Null, 'T', 0, Null, Null, Null, 'Che', 'Color', 0
@SUCR_Codigo Char(2), @ALMA_Codigo Char(3), @FAMI_Codigo Char(4),
@SUBF_Codigo Char(5), @SALD_FechaI Datetime, @SALD_FechaF Datetime,
@MARC_Codigo Char(3), @PROD_Tipo Char(1), @UnidadMedida SmallInt,
@PROD_ClaseABC Char(1), @PROD_Desc_Contiene1 Varchar(80) = Null,
@PROD_Desc_Contiene2 Varchar(80) = Null, @PROD_Desc_NoContiene1 Varchar(80)
= Null, @PROD_Desc_NoContiene2 Varchar(80) = Null
As
Set NoCount On
Declare @SALD_FecSaldoI Char(10), @SALD_FecSaldoF Char(10), @SALD_FechaIM
Datetime
Select @SALD_FechaIM = @SALD_FechaI + 1
Select @SALD_FecSaldoI = Convert(Char(10), @SALD_FechaI, 102) ,
@SALD_FecSaldoF = Convert(Char(10), @SALD_FechaF, 102)
Create Table #TmpSaldosT (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
SALD_FecSaldo Char(10), PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4)
Constraint [Pk_TmpSaldosT_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, SALD_FecSaldo, PROD_CodProd) With
FillFactor = 90 On [Primary])
Create Table #TmpSaldosR (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4), SALD_Ingresos
Decimal(14,4), SALD_Salidas Decimal(14,4), SALD_SaldoPFF Decimal(14,4)
Constraint [Pk_TmpSaldosR_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, PROD_CodProd) With FillFactor = 90 On
[Primary])
Insert Into #TmpSaldosR
Select C.SUCR_Codigo, C.ALMA_Codigo, C.PROD_CodProd, 0, 0, 0, 0
From Productos P Inner Join Catalogos C On P.PROD_CodProd = C.PROD_CodProd
Where C.SUCR_Codigo = @SUCR_Codigo And C.ALMA_Codigo = @ALMA_Codigo And
C.CATL_Activo = 'S'
And P.FAMI_Codigo = IsNull(@FAMI_Codigo, P.FAMI_Codigo) And P.SUBF_Codigo =
IsNull(@SUBF_Codigo, P.SUBF_Codigo)
And P.MARC_Codigo = IsNull(@MARC_Codigo, P.MARC_Codigo) And P.PROD_Tipo =
IsNull(@PROD_Tipo, P.PROD_Tipo)
And P.PROD_ClaseABC = IsNull(@PROD_ClaseABC, P.PROD_ClaseABC)
And P.PROD_Desc Like '%' + dbo.Alltrim(IsNull(@PROD_Desc_Contiene1,
P.PROD_Desc)) + '%' And P.PROD_Desc Like '%' +
dbo.Alltrim(IsNull(@PROD_Desc_Contiene2, P.PROD_Desc)) + '%'
And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' + @PROD_Desc_NoContiene1 +
'%', '')) And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' +
@PROD_Desc_NoContiene2 + '%', ''))
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFI
From Saldos S Inner Join #TmpSaldosR P On S.SUCR_Codigo = P.SUCR_Codigo And
S.ALMA_Codigo = P.ALMA_Codigo And S.PROD_CodProd = P.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoI
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFI = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantIngr) As DVAL_CantIngr From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'I'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Ingresos = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantSalida) DVAL_CantSalida From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'S'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Salidas = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFF
From Saldos S Inner Join #TmpSaldosR P On P.SUCR_Codigo = S.SUCR_Codigo And
P.ALMA_Codigo = S.ALMA_Codigo And P.PROD_CodProd = S.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoF
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFF = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Drop Table #TmpSaldosT
Select P.PROD_CodigoAnt, S.PROD_CodProd, P.PROD_Desc, M.MARC_Descripcion,
T.TIPO_DescC,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFI When 1 Then S.SALD_SaldoPFI
/ P.PROD_Factor End As SALD_SaldoPFI,
Case @UnidadMedida When 0 Then S.SALD_Ingresos When 1 Then S.SALD_Ingresos
/ P.PROD_Factor End As SALD_Ingresos,
Case @UnidadMedida When 0 Then S.SALD_Salidas When 1 Then S.SALD_Salidas /
P.PROD_Factor End As SALD_Salidas,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFF When 1 Then S.SALD_SaldoPFF
/ P.PROD_Factor End As SALD_SaldoPFF,
dbo.Alltrim(U.UBIC_Codigo) + dbo.Alltrim(U.UBIC_Desc) As UBIC_Desc,
P.PROD_ClaseABC, F.FAMI_Codigo, F.FAMI_Desc1, SF.SUBF_Codigo, SF.SUBF_Desc1
From #TmpSaldosR S
Inner Join Productos P On S.PROD_CodProd = P.PROD_CodProd
Inner Join Tipos T On T.TIPO_CodTabla = P.TIPO_TabMed And T.TIPO_CodTipo =
Case @UnidadMedida When 0 Then P.TIPO_CodMedV When 1 Then P.TIPO_CodMedC End
Inner Join Marcas M On M.MARC_Codigo = P.MARC_Codigo
Inner Join Familias F On F.FAMI_Codigo = P.FAMI_Codigo
Inner Join Subfamilias SF On SF.SUBF_Codigo = P.SUBF_Codigo And
SF.FAMI_Codigo = P.FAMI_Codigo
Inner Join Catalogos C On S.SUCR_Codigo = C.SUCR_Codigo And S.ALMA_Codigo =
C.ALMA_Codigo And S.PROD_CodProd = C.PROD_CodProd
Left Join Ubicaciones U On U.ALMA_Codigo = C.ALMA_Codigo And U.UBIC_Codigo
= C.UBIC_Codigo
Order by S.PROD_CodProd
Drop Table #TmpSaldosR
Set NoCount Off
y lo que deseo es que con un parametro adicional me filter solo los datos
que tienen resultado es decir sean diferentes de 0 en el campo
S.SALD_SaldoPFF, entonces pense en hacer en esto y bueno no me funciona ya
que no lo compila el query analizer, le aumente la clausula WHERE al final
donde practicamente muestro los datos.
Alter Procedure SALDSS_SaldosPorRangosdeFechas --
SALDSS_SaldosPorRangosdeFechas '00', '001', '211', Null, '2005/08/01',
'2005/09/01', Null, 'T', 0, Null, Null, Null, 'Che', 'Color', 0
@SUCR_Codigo Char(2), @ALMA_Codigo Char(3), @FAMI_Codigo Char(4),
@SUBF_Codigo Char(5), @SALD_FechaI Datetime, @SALD_FechaF Datetime,
@MARC_Codigo Char(3), @PROD_Tipo Char(1), @UnidadMedida SmallInt,
@PROD_ClaseABC Char(1), @PROD_Desc_Contiene1 Varchar(80) = Null,
@PROD_Desc_Contiene2 Varchar(80) = Null, @PROD_Desc_NoContiene1 Varchar(80)
= Null, @PROD_Desc_NoContiene2 Varchar(80) = Null,
@SALD_ConSaldo Bit
As
Set NoCount On
Declare @SALD_FecSaldoI Char(10), @SALD_FecSaldoF Char(10), @SALD_FechaIM
Datetime
Select @SALD_FechaIM = @SALD_FechaI + 1
Select @SALD_FecSaldoI = Convert(Char(10), @SALD_FechaI, 102) ,
@SALD_FecSaldoF = Convert(Char(10), @SALD_FechaF, 102)
Create Table #TmpSaldosT (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
SALD_FecSaldo Char(10), PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4)
Constraint [Pk_TmpSaldosT_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, SALD_FecSaldo, PROD_CodProd) With
FillFactor = 90 On [Primary])
Create Table #TmpSaldosR (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4), SALD_Ingresos
Decimal(14,4), SALD_Salidas Decimal(14,4), SALD_SaldoPFF Decimal(14,4)
Constraint [Pk_TmpSaldosR_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, PROD_CodProd) With FillFactor = 90 On
[Primary])
Insert Into #TmpSaldosR
Select C.SUCR_Codigo, C.ALMA_Codigo, C.PROD_CodProd, 0, 0, 0, 0
From Productos P Inner Join Catalogos C On P.PROD_CodProd = C.PROD_CodProd
Where C.SUCR_Codigo = @SUCR_Codigo And C.ALMA_Codigo = @ALMA_Codigo And
C.CATL_Activo = 'S'
And P.FAMI_Codigo = IsNull(@FAMI_Codigo, P.FAMI_Codigo) And P.SUBF_Codigo =
IsNull(@SUBF_Codigo, P.SUBF_Codigo)
And P.MARC_Codigo = IsNull(@MARC_Codigo, P.MARC_Codigo) And P.PROD_Tipo =
IsNull(@PROD_Tipo, P.PROD_Tipo)
And P.PROD_ClaseABC = IsNull(@PROD_ClaseABC, P.PROD_ClaseABC)
And P.PROD_Desc Like '%' + dbo.Alltrim(IsNull(@PROD_Desc_Contiene1,
P.PROD_Desc)) + '%' And P.PROD_Desc Like '%' +
dbo.Alltrim(IsNull(@PROD_Desc_Contiene2, P.PROD_Desc)) + '%'
And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' + @PROD_Desc_NoContiene1 +
'%', '')) And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' +
@PROD_Desc_NoContiene2 + '%', ''))
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFI
From Saldos S Inner Join #TmpSaldosR P On S.SUCR_Codigo = P.SUCR_Codigo And
S.ALMA_Codigo = P.ALMA_Codigo And S.PROD_CodProd = P.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoI
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFI = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantIngr) As DVAL_CantIngr From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'I'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Ingresos = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantSalida) DVAL_CantSalida From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'S'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Salidas = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFF
From Saldos S Inner Join #TmpSaldosR P On P.SUCR_Codigo = S.SUCR_Codigo And
P.ALMA_Codigo = S.ALMA_Codigo And P.PROD_CodProd = S.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoF
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFF = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Drop Table #TmpSaldosT
Select P.PROD_CodigoAnt, S.PROD_CodProd, P.PROD_Desc, M.MARC_Descripcion,
T.TIPO_DescC,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFI When 1 Then S.SALD_SaldoPFI
/ P.PROD_Factor End As SALD_SaldoPFI,
Case @UnidadMedida When 0 Then S.SALD_Ingresos When 1 Then S.SALD_Ingresos
/ P.PROD_Factor End As SALD_Ingresos,
Case @UnidadMedida When 0 Then S.SALD_Salidas When 1 Then S.SALD_Salidas /
P.PROD_Factor End As SALD_Salidas,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFF When 1 Then S.SALD_SaldoPFF
/ P.PROD_Factor End As SALD_SaldoPFF,
dbo.Alltrim(U.UBIC_Codigo) + dbo.Alltrim(U.UBIC_Desc) As UBIC_Desc,
P.PROD_ClaseABC, F.FAMI_Codigo, F.FAMI_Desc1, SF.SUBF_Codigo, SF.SUBF_Desc1
From #TmpSaldosR S
Inner Join Productos P On S.PROD_CodProd = P.PROD_CodProd
Inner Join Tipos T On T.TIPO_CodTabla = P.TIPO_TabMed And T.TIPO_CodTipo =
Case @UnidadMedida When 0 Then P.TIPO_CodMedV When 1 Then P.TIPO_CodMedC End
Inner Join Marcas M On M.MARC_Codigo = P.MARC_Codigo
Inner Join Familias F On F.FAMI_Codigo = P.FAMI_Codigo
Inner Join Subfamilias SF On SF.SUBF_Codigo = P.SUBF_Codigo And
SF.FAMI_Codigo = P.FAMI_Codigo
Inner Join Catalogos C On S.SUCR_Codigo = C.SUCR_Codigo And S.ALMA_Codigo =
C.ALMA_Codigo And S.PROD_CodProd = C.PROD_CodProd
Left Join Ubicaciones U On U.ALMA_Codigo = C.ALMA_Codigo And U.UBIC_Codigo
= C.UBIC_Codigo
Where (Case When @SALD_ConSaldo = 0 Then S.SALD_SaldoPFF <> 0 End)
Order by S.PROD_CodProd
Drop Table #TmpSaldosR
Set NoCount Off
Pense de que esta manera me funcionaria o es que tengo que partirlo el SP
con un IF y definir que se ejecuta.
Salu2
Tengo el siguiente query original:
Alter Procedure SALDSS_SaldosPorRangosdeFechas --
SALDSS_SaldosPorRangosdeFechas '00', '001', '211', Null, '2005/08/01',
'2005/09/01', Null, 'T', 0, Null, Null, Null, 'Che', 'Color', 0
@SUCR_Codigo Char(2), @ALMA_Codigo Char(3), @FAMI_Codigo Char(4),
@SUBF_Codigo Char(5), @SALD_FechaI Datetime, @SALD_FechaF Datetime,
@MARC_Codigo Char(3), @PROD_Tipo Char(1), @UnidadMedida SmallInt,
@PROD_ClaseABC Char(1), @PROD_Desc_Contiene1 Varchar(80) = Null,
@PROD_Desc_Contiene2 Varchar(80) = Null, @PROD_Desc_NoContiene1 Varchar(80)
= Null, @PROD_Desc_NoContiene2 Varchar(80) = Null
As
Set NoCount On
Declare @SALD_FecSaldoI Char(10), @SALD_FecSaldoF Char(10), @SALD_FechaIM
Datetime
Select @SALD_FechaIM = @SALD_FechaI + 1
Select @SALD_FecSaldoI = Convert(Char(10), @SALD_FechaI, 102) ,
@SALD_FecSaldoF = Convert(Char(10), @SALD_FechaF, 102)
Create Table #TmpSaldosT (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
SALD_FecSaldo Char(10), PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4)
Constraint [Pk_TmpSaldosT_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, SALD_FecSaldo, PROD_CodProd) With
FillFactor = 90 On [Primary])
Create Table #TmpSaldosR (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4), SALD_Ingresos
Decimal(14,4), SALD_Salidas Decimal(14,4), SALD_SaldoPFF Decimal(14,4)
Constraint [Pk_TmpSaldosR_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, PROD_CodProd) With FillFactor = 90 On
[Primary])
Insert Into #TmpSaldosR
Select C.SUCR_Codigo, C.ALMA_Codigo, C.PROD_CodProd, 0, 0, 0, 0
From Productos P Inner Join Catalogos C On P.PROD_CodProd = C.PROD_CodProd
Where C.SUCR_Codigo = @SUCR_Codigo And C.ALMA_Codigo = @ALMA_Codigo And
C.CATL_Activo = 'S'
And P.FAMI_Codigo = IsNull(@FAMI_Codigo, P.FAMI_Codigo) And P.SUBF_Codigo =
IsNull(@SUBF_Codigo, P.SUBF_Codigo)
And P.MARC_Codigo = IsNull(@MARC_Codigo, P.MARC_Codigo) And P.PROD_Tipo =
IsNull(@PROD_Tipo, P.PROD_Tipo)
And P.PROD_ClaseABC = IsNull(@PROD_ClaseABC, P.PROD_ClaseABC)
And P.PROD_Desc Like '%' + dbo.Alltrim(IsNull(@PROD_Desc_Contiene1,
P.PROD_Desc)) + '%' And P.PROD_Desc Like '%' +
dbo.Alltrim(IsNull(@PROD_Desc_Contiene2, P.PROD_Desc)) + '%'
And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' + @PROD_Desc_NoContiene1 +
'%', '')) And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' +
@PROD_Desc_NoContiene2 + '%', ''))
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFI
From Saldos S Inner Join #TmpSaldosR P On S.SUCR_Codigo = P.SUCR_Codigo And
S.ALMA_Codigo = P.ALMA_Codigo And S.PROD_CodProd = P.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoI
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFI = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantIngr) As DVAL_CantIngr From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'I'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Ingresos = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantSalida) DVAL_CantSalida From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'S'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Salidas = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFF
From Saldos S Inner Join #TmpSaldosR P On P.SUCR_Codigo = S.SUCR_Codigo And
P.ALMA_Codigo = S.ALMA_Codigo And P.PROD_CodProd = S.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoF
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFF = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Drop Table #TmpSaldosT
Select P.PROD_CodigoAnt, S.PROD_CodProd, P.PROD_Desc, M.MARC_Descripcion,
T.TIPO_DescC,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFI When 1 Then S.SALD_SaldoPFI
/ P.PROD_Factor End As SALD_SaldoPFI,
Case @UnidadMedida When 0 Then S.SALD_Ingresos When 1 Then S.SALD_Ingresos
/ P.PROD_Factor End As SALD_Ingresos,
Case @UnidadMedida When 0 Then S.SALD_Salidas When 1 Then S.SALD_Salidas /
P.PROD_Factor End As SALD_Salidas,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFF When 1 Then S.SALD_SaldoPFF
/ P.PROD_Factor End As SALD_SaldoPFF,
dbo.Alltrim(U.UBIC_Codigo) + dbo.Alltrim(U.UBIC_Desc) As UBIC_Desc,
P.PROD_ClaseABC, F.FAMI_Codigo, F.FAMI_Desc1, SF.SUBF_Codigo, SF.SUBF_Desc1
From #TmpSaldosR S
Inner Join Productos P On S.PROD_CodProd = P.PROD_CodProd
Inner Join Tipos T On T.TIPO_CodTabla = P.TIPO_TabMed And T.TIPO_CodTipo =
Case @UnidadMedida When 0 Then P.TIPO_CodMedV When 1 Then P.TIPO_CodMedC End
Inner Join Marcas M On M.MARC_Codigo = P.MARC_Codigo
Inner Join Familias F On F.FAMI_Codigo = P.FAMI_Codigo
Inner Join Subfamilias SF On SF.SUBF_Codigo = P.SUBF_Codigo And
SF.FAMI_Codigo = P.FAMI_Codigo
Inner Join Catalogos C On S.SUCR_Codigo = C.SUCR_Codigo And S.ALMA_Codigo =
C.ALMA_Codigo And S.PROD_CodProd = C.PROD_CodProd
Left Join Ubicaciones U On U.ALMA_Codigo = C.ALMA_Codigo And U.UBIC_Codigo
= C.UBIC_Codigo
Order by S.PROD_CodProd
Drop Table #TmpSaldosR
Set NoCount Off
y lo que deseo es que con un parametro adicional me filter solo los datos
que tienen resultado es decir sean diferentes de 0 en el campo
S.SALD_SaldoPFF, entonces pense en hacer en esto y bueno no me funciona ya
que no lo compila el query analizer, le aumente la clausula WHERE al final
donde practicamente muestro los datos.
Alter Procedure SALDSS_SaldosPorRangosdeFechas --
SALDSS_SaldosPorRangosdeFechas '00', '001', '211', Null, '2005/08/01',
'2005/09/01', Null, 'T', 0, Null, Null, Null, 'Che', 'Color', 0
@SUCR_Codigo Char(2), @ALMA_Codigo Char(3), @FAMI_Codigo Char(4),
@SUBF_Codigo Char(5), @SALD_FechaI Datetime, @SALD_FechaF Datetime,
@MARC_Codigo Char(3), @PROD_Tipo Char(1), @UnidadMedida SmallInt,
@PROD_ClaseABC Char(1), @PROD_Desc_Contiene1 Varchar(80) = Null,
@PROD_Desc_Contiene2 Varchar(80) = Null, @PROD_Desc_NoContiene1 Varchar(80)
= Null, @PROD_Desc_NoContiene2 Varchar(80) = Null,
@SALD_ConSaldo Bit
As
Set NoCount On
Declare @SALD_FecSaldoI Char(10), @SALD_FecSaldoF Char(10), @SALD_FechaIM
Datetime
Select @SALD_FechaIM = @SALD_FechaI + 1
Select @SALD_FecSaldoI = Convert(Char(10), @SALD_FechaI, 102) ,
@SALD_FecSaldoF = Convert(Char(10), @SALD_FechaF, 102)
Create Table #TmpSaldosT (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
SALD_FecSaldo Char(10), PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4)
Constraint [Pk_TmpSaldosT_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, SALD_FecSaldo, PROD_CodProd) With
FillFactor = 90 On [Primary])
Create Table #TmpSaldosR (SUCR_Codigo Char(2), ALMA_Codigo Char(3),
PROD_CodProd Char(15), SALD_SaldoPFI Decimal(14,4), SALD_Ingresos
Decimal(14,4), SALD_Salidas Decimal(14,4), SALD_SaldoPFF Decimal(14,4)
Constraint [Pk_TmpSaldosR_SALDSS_SaldosProRangosdeFechas] Primary Key
Clustered (SUCR_Codigo, ALMA_Codigo, PROD_CodProd) With FillFactor = 90 On
[Primary])
Insert Into #TmpSaldosR
Select C.SUCR_Codigo, C.ALMA_Codigo, C.PROD_CodProd, 0, 0, 0, 0
From Productos P Inner Join Catalogos C On P.PROD_CodProd = C.PROD_CodProd
Where C.SUCR_Codigo = @SUCR_Codigo And C.ALMA_Codigo = @ALMA_Codigo And
C.CATL_Activo = 'S'
And P.FAMI_Codigo = IsNull(@FAMI_Codigo, P.FAMI_Codigo) And P.SUBF_Codigo =
IsNull(@SUBF_Codigo, P.SUBF_Codigo)
And P.MARC_Codigo = IsNull(@MARC_Codigo, P.MARC_Codigo) And P.PROD_Tipo =
IsNull(@PROD_Tipo, P.PROD_Tipo)
And P.PROD_ClaseABC = IsNull(@PROD_ClaseABC, P.PROD_ClaseABC)
And P.PROD_Desc Like '%' + dbo.Alltrim(IsNull(@PROD_Desc_Contiene1,
P.PROD_Desc)) + '%' And P.PROD_Desc Like '%' +
dbo.Alltrim(IsNull(@PROD_Desc_Contiene2, P.PROD_Desc)) + '%'
And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' + @PROD_Desc_NoContiene1 +
'%', '')) And P.PROD_Desc Not Like dbo.Alltrim(IsNull('%' +
@PROD_Desc_NoContiene2 + '%', ''))
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFI
From Saldos S Inner Join #TmpSaldosR P On S.SUCR_Codigo = P.SUCR_Codigo And
S.ALMA_Codigo = P.ALMA_Codigo And S.PROD_CodProd = P.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoI
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFI = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantIngr) As DVAL_CantIngr From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'I'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Ingresos = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select D.SUCR_Codigo, D.ALMA_Codigo, Convert(SmallDateTime, Getdate()),
D.PROD_CodProd, Sum(D.DVAL_CantSalida) DVAL_CantSalida From DetValesAlm D
Inner Join Vales V On D.SUCR_Codigo = V.SUCR_Codigo And D.ALMA_Codigo =
V.ALMA_Codigo And D.TIPO_TabVale = V.TIPO_TabVale And D.TIPO_CodVale =
V.TIPO_CodVale And D.VALE_NumVale = V.VALE_NumVale
Inner Join #TmpSaldosR P On P.SUCR_Codigo = D.SUCR_Codigo And P.ALMA_Codigo
= D.ALMA_Codigo And P.PROD_CodProd = D.PROD_CodProd
Where V.VALE_Estado = 'C' And D.DVAL_FecConfir Between @SALD_FechaIM And
@SALD_FechaF And V.VALE_Efecto = 'S'
Group By D.SUCR_Codigo, D.ALMA_Codigo, D.PROD_CodProd
Update #TmpSaldosR Set SALD_Salidas = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Delete From #TmpSaldosT
Insert Into #TmpSaldosT
Select S.SUCR_Codigo, S.ALMA_Codigo, Max(S.SALD_FecSaldo) As SALD_FecSaldo,
S.PROD_CodProd, Convert(decimal(14,4), 0) As SALD_SaldoPFF
From Saldos S Inner Join #TmpSaldosR P On P.SUCR_Codigo = S.SUCR_Codigo And
P.ALMA_Codigo = S.ALMA_Codigo And P.PROD_CodProd = S.PROD_CodProd
Where S.SALD_FecSaldo <= @SALD_FecSaldoF
Group by S.SUCR_Codigo, S.ALMA_Codigo, S.PROD_CodProd
Update #TmpSaldosT Set SALD_SaldoPFI = S.SALD_SaldoF
From #TmpSaldosT T Inner Join Saldos S On T.SUCR_Codigo = S.SUCR_Codigo And
T.ALMA_Codigo = S.ALMA_Codigo And T.SALD_FecSaldo = S.SALD_FecSaldo And
T.PROD_CodProd = S.PROD_CodProd
Update #TmpSaldosR Set SALD_SaldoPFF = S.SALD_SaldoPFI
From #TmpSaldosR T Inner Join #TmpSaldosT S On T.SUCR_Codigo = S.SUCR_Codigo
And T.ALMA_Codigo = S.ALMA_Codigo And T.PROD_CodProd = S.PROD_CodProd
Drop Table #TmpSaldosT
Select P.PROD_CodigoAnt, S.PROD_CodProd, P.PROD_Desc, M.MARC_Descripcion,
T.TIPO_DescC,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFI When 1 Then S.SALD_SaldoPFI
/ P.PROD_Factor End As SALD_SaldoPFI,
Case @UnidadMedida When 0 Then S.SALD_Ingresos When 1 Then S.SALD_Ingresos
/ P.PROD_Factor End As SALD_Ingresos,
Case @UnidadMedida When 0 Then S.SALD_Salidas When 1 Then S.SALD_Salidas /
P.PROD_Factor End As SALD_Salidas,
Case @UnidadMedida When 0 Then S.SALD_SaldoPFF When 1 Then S.SALD_SaldoPFF
/ P.PROD_Factor End As SALD_SaldoPFF,
dbo.Alltrim(U.UBIC_Codigo) + dbo.Alltrim(U.UBIC_Desc) As UBIC_Desc,
P.PROD_ClaseABC, F.FAMI_Codigo, F.FAMI_Desc1, SF.SUBF_Codigo, SF.SUBF_Desc1
From #TmpSaldosR S
Inner Join Productos P On S.PROD_CodProd = P.PROD_CodProd
Inner Join Tipos T On T.TIPO_CodTabla = P.TIPO_TabMed And T.TIPO_CodTipo =
Case @UnidadMedida When 0 Then P.TIPO_CodMedV When 1 Then P.TIPO_CodMedC End
Inner Join Marcas M On M.MARC_Codigo = P.MARC_Codigo
Inner Join Familias F On F.FAMI_Codigo = P.FAMI_Codigo
Inner Join Subfamilias SF On SF.SUBF_Codigo = P.SUBF_Codigo And
SF.FAMI_Codigo = P.FAMI_Codigo
Inner Join Catalogos C On S.SUCR_Codigo = C.SUCR_Codigo And S.ALMA_Codigo =
C.ALMA_Codigo And S.PROD_CodProd = C.PROD_CodProd
Left Join Ubicaciones U On U.ALMA_Codigo = C.ALMA_Codigo And U.UBIC_Codigo
= C.UBIC_Codigo
Where (Case When @SALD_ConSaldo = 0 Then S.SALD_SaldoPFF <> 0 End)
Order by S.PROD_CodProd
Drop Table #TmpSaldosR
Set NoCount Off
Pense de que esta manera me funcionaria o es que tengo que partirlo el SP
con un IF y definir que se ejecuta.
Salu2
Leer las respuestas