dbo.monitoreos_batch_comex_importaciones

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@yesterday date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [monitoreos_batch_comex] '2024-09-17'

select * from comercio_exterior where cuit = '30714532215'
*/

-- select * from productos_secciones where producto = 12
-- seccion 11 = Comex
-- 23/01/2025: Importaciones se toma de la tabla comercio_exterior no comex_importaciones



CREATE OR ALTER PROCEDURE dbo.monitoreos_batch_comex_importaciones
@yesterday date = null

AS
BEGIN
    SET NOCOUNT ON;

 declare @importaciones varchar(50), @importaciones_moneda varchar(50),   
   @total bigint,
   @consumo bigint,
   @temporal bigint,
   @deposito bigint, 
   @zona_franca bigint,

   @tipo_operacion varchar(50),
   @periodo varchar(7), 
   @html varchar(max),
   @fecha varchar(7),
   @created date 
        
 if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end 
 
 
 declare @r_cuit varchar(20),@r_seccion_mantenimiento int = 11
 DECLARE bn CURSOR FOR
  select --'30550815997'
  distinct cuit 
   from monitoreos 
   where visible = 1 
   and isnull(seccion_mantenimiento,11) = @r_seccion_mantenimiento
   and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
   and (fecha_desde is null or fecha_desde <= @yesterday)  
 OPEN bn 
 Fetch next from bn INTO  @r_cuit 

 WHILE (@@FETCH_STATUS <> -1 )

  BEGIN

   DECLARE mod_comex CURSOR FOR
    select distinct convert(varchar(7),periodo) as periodo,
      (isnull(consumo,0)+isnull(deposito,0)+isnull(zona_franca,0)+isnull(temporal,0)) as total, 
      consumo,
      temporal,
      deposito, 
      zona_franca
       from comercio_exterior 
      where cuit = @r_cuit  
        and (convert(date,created) = @yesterday and created is not null)
   order by periodo desc 

   declare @periodo_ant varchar(7) = ''
   OPEN mod_comex 
   
   FETCH next from mod_comex INTO  @tipo_operacion, @fecha, @created, @total, 
           @consumo, @temporal, @deposito, @zona_franca 

   WHILE (@@FETCH_STATUS <> -1 )
    BEGIN
     
     set @periodo = SUBSTRING(@fecha,6,2) + '/' + SUBSTRING(@fecha,1,4)

     set @html = '<td>'+@r_cuit+'</td><td>'+@periodo_ant+'</td>'+
        '<td align=right>'+format(@consumo,'#,#')+'</td>'+
        '<td align=right>'+format(@temporal,'#,#')+'</td>'+
        '<td align=right>'+format(@deposito,'#,#')+'</td>'+
        '<td align=right>'+format(@zona_franca,'#,#')+'</td>'+
        '<td align=right>'+format(@total,'#,#')+'</td></tr>'

     insert into monitoreos_resultados (id_tabla,tabla,cuit,seccion_mantenimiento,fecha,novedad,tipo_novedad,html)
                values(null, 'comercio_exterior', @r_cuit, 11, @created, 1,'comercio_exterior', @html)
     update monitoreos 
       set fecha_ultimo_procesamiento = @yesterday,
        fecha_modificacion = @yesterday
     where cuit = @r_cuit 
      and isnull(seccion_mantenimiento,@r_seccion_mantenimiento) = @r_seccion_mantenimiento
      and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
      and (fecha_desde is null or fecha_desde <= @yesterday) 
      
     
     FETCH next from mod_comex INTO  @tipo_operacion, @fecha, @created, @total, 
             @consumo, @temporal, @deposito, @zona_franca 
          
    END
   
    CLOSE mod_comex
    DEALLOCATE mod_comex 
   
    FETCH next from bn INTO  @r_cuit 

  END
  CLOSE bn
  DEALLOCATE bn 
END 

GO

Used By

No items found