dbo.obtener_card_bancos_test

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@p_Cuit varchar 20

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE OR ALTER PROCEDURE dbo.obtener_card_bancos_test
@p_Cuit varchar(20)
AS
BEGIN
    SET NOCOUNT ON;
  declare @p_5anios date = getdate() - (5*365)
 declare @p_2anios date = getdate() - (2*365)
 declare @p_3meses date = getdate() - (90)
 declare @periodo_maximo date,@periodo_maximo_cuit date,@fecha_formateada varchar(10),@periodo_comparar varchar(6);

 --select @periodo_maximo = (select max(fecha_informacion) from fuentes_procesadas where visible = '1')
 
 select @periodo_maximo = convert(date,max(cendeu.fec_inf+'01')) from cendeu where iden = @p_cuit AND vigente = 1
 if (@periodo_maximo is not null ) 
 begin
  set @periodo_comparar =  convert(varchar(6),DATEPART(YY,@periodo_maximo))+  right('0' + rtrim(convert(varchar(6),(datepart(MM,@periodo_maximo)))),2)
  select distinct count(*) as cantidad_bancos,@periodo_maximo as periodo,sum((prestamos+particip+garotor+o_cptos+deuda_total)) as deuda_total from cendeu where iden = @p_Cuit and fec_inf = @periodo_comparar and vigente = 1 
    and not exists (select * from cendeu b where b.iden = cendeu.iden and cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int) and b.fec_inf = cendeu.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu.id_cendeu)
 and not (
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu c where c.iden = cendeu.iden and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
   union all
  select distinct count(*) as cantidad_bancos,@periodo_maximo as periodo,sum((prestamos+particip+garotor+o_cptos+deuda_total)) as deuda_total from cendeu_temp where iden = @p_Cuit and fec_inf = @periodo_comparar and vigente = 1 
    and not exists (select * from cendeu_temp b where b.iden = cendeu_temp.iden and cast(b.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) and b.fec_inf = cendeu_temp.fec_inf and b.vigente = 1 and b.id_fuente = 2 and b.id_cendeu != cendeu_temp.id_cendeu)
 and not (
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu_temp c where c.iden = cendeu_temp.iden and cast(c.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu_temp.iden 
        and cast(c.codigo_ent as int) = cast(cendeu_temp.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
 end 
END

GO

Used By

No items found