dbo.Sp_control_cyq

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@desde date 3
@hasta date 3
@fuente varchar 50

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.Sp_control_cyq
@desde date = '2024-10-01',
@hasta date = null, 
@fuente varchar(50) = NULL 
AS
BEGIN

 SET NOCOUNT ON;
 if (@hasta is null) set @hasta = CONVERT(date,getdate())
 DECLARE @pcia varchar(150) 
 DECLARE @fecha date 
 DECLARE @dia_semana char(3) 
 DECLARE @cantidad int = 0
 DECLARE C_pcias CURSOR FOR
  select fue_nombre from fuentes where fue_codigo like 'B%' and fue_nombre like 'B.%' 
  and IIF(@fuente is not null, iif(fue_nombre = @fuente,1,0),1) = 1 
    order by fue_nombre
 OPEN C_pcias
 FETCH NEXT FROM C_pcias INTO @pcia
 WHILE @@fetch_status = 0
 BEGIN 
  set @fecha = @desde 
  WHILE @fecha <= @hasta 
  BEGIN
   set @dia_semana = case DATEPART(WEEKDAY,@fecha) 
        when 1 then 'DOM'
        when 2 then 'LUN'
        when 3 then 'MAR'
        when 4 then 'MIE'
        when 5 then 'JUE'
        when 6 then 'VIE'
        when 7 then 'SAB'
         end 
   if @dia_semana = 'DOM' 
      or @dia_semana = 'SAB' 
      or exists (select * from feriados where pais = 'AR' and activo = 1 and fecha_feriado = @fecha) 
   begin
    set @fecha = DATEADD(D,1,@fecha)
    continue
   end

   select @cantidad = count(*)
              from juicios  
             where fuente = @pcia
               and fecha_fuente = @fecha 
    
   if exists (select * from [control_cyq] where pcia = @pcia and fecha = @fecha)
    delete from [control_cyq] where pcia = @pcia and fecha = @fecha

   insert into [control_cyq] (pcia, fecha, dia_semana, cantidad) 
   values (@pcia, @fecha, @dia_semana, @cantidad) 
   
   set @fecha = DATEADD(D,1,@fecha)
  END 
   
  FETCH NEXT FROM C_pcias INTO @pcia

  
 END 
 CLOSE C_pcias
 DEALLOCATE C_pcias 
 /*
  select * from [control_cyq] 
  where fecha >= @desde  
  and iif(@fuente is not null, iif(pcia = @fuente, 1,0), 1) = 1 
  order by pcia, fecha 
  */

 

END

GO