dbo.monitoreos_batch_10_prueba

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_10_prueba '2017-07-14'
CREATE OR ALTER PROCEDURE dbo.monitoreos_batch_10_prueba
@yesterday date = null

AS
BEGIN
    SET NOCOUNT ON;
 declare @r_id_cendeu int
 declare @yesterday1 date
 if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end 
 /*  sacar este set */
 set @yesterday1 = '2017-10-15'

 declare @r_cuit varchar(20),@r_seccion_mantenimiento int
 declare @cant1 int
 declare @cant2 int 
 declare @cant3 int
 declare @cant4 int
 
 set @cant1 = 0
 set @cant2 = 0
 set @cant3 = 0
 set @cant4 = 0
 
 /* en los 4 count tienen que ser = @yesterday no >= */
 set @cant2 = (select COUNT(*) from cendeu      where CONVERT(date, modified) >= @yesterday1)
 set @cant1 = (select COUNT(*) from cendeu      where CONVERT(date, created)  >= @yesterday1)
 set @cant3 = (select COUNT(*) from cendeu_temp where CONVERT(date, modified) >= @yesterday1)
 set @cant4 = (select COUNT(*) from cendeu_temp where CONVERT(date, created)  >= @yesterday1)
 
 if (@cant1 > 0 or  @cant2 > 0 or @cant3 > 0 or @cant4 > 0) 
 BEGIN
  DECLARE bn CURSOR

  FOR
   select distinct cuit,seccion_mantenimiento from monitoreos where visible = 1 and seccion_mantenimiento = 10 and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
  -- apertura del cursor
  OPEN bn 
  -- Lectura de la primera fila del cursor
  Fetch next from bn INTO  @r_cuit, @r_seccion_mantenimiento

  WHILE (@@FETCH_STATUS <> -1 )
 
  BEGIN 
   --Inicio otro cursor para poder pegar por ID.
         if (@cant1 > 0 or  @cant2 > 0)
   begin
    DECLARE cendeuc CURSOR
    FOR
     select distinct(id_cendeu)
       from cendeu 
       where iden = @r_cuit 
         /* tiene que ser = @yesterday no >= */
         and ((convert(date,created) >= @yesterday1 and created is not null) or (convert(date,modified) >= @yesterday1 and modified is not null)) 
      and vigente = 1
    OPEN cendeuc 
     FETCH next from cendeuc INTO  @r_id_cendeu
    WHILE (@@FETCH_STATUS <> -1 )
     BEGIN 
      select @r_id_cendeu, 'cendeu', @r_cuit
      FETCH next from cendeuc INTO  @r_id_cendeu

     END
    CLOSE cendeuc
    DEALLOCATE cendeuc 
   end 

   if (@cant3 > 0 or  @cant4 > 0) 
   begin
    DECLARE cendeuc2 CURSOR
    FOR
     select distinct(id_cendeu)
       from cendeu_temp
       where iden = @r_cuit 
         /* tiene que ser = @yesterday no >= */
         and ((convert(date,created) >= @yesterday1 and created is not null) or (convert(date,modified) >= @yesterday1 and modified is not null)) 
      and vigente = 1
   
    OPEN cendeuc2 
     FETCH next from cendeuc2 INTO  @r_id_cendeu
    WHILE (@@FETCH_STATUS <> -1 )
     BEGIN
       
     select @r_id_cendeu, 'cendeu_temp', @r_cuit
      FETCH next from cendeuc2 INTO  @r_id_cendeu
     END
    CLOSE cendeuc2
    DEALLOCATE cendeuc2 
   end 

   FETCH next from bn INTO  @r_cuit,@r_seccion_mantenimiento

  END

  -- Cierra el cursor
  CLOSE bn
  -- Libera los recursos del cursor
  DEALLOCATE bn 

 END
END 

GO

Used By

No items found