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_envio_mails_con_novedades
CREATE OR ALTER PROCEDURE dbo.monitoreos_envio_mails_con_novedades
@yesterday date = null
AS
BEGIN
SET NOCOUNT ON;
if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end
declare @respuesta table (cliente int, nombre_cliente varchar(200), email varchar(200))
declare @r_cliente int
declare @r_user_id int
declare @r_nombre_cliente varchar(200)
declare @r_mail varchar(200)
DECLARE bn CURSOR FOR
/*
--declare @yesterday date = getdate() - 1
select distinct a.cliente --, a.user_id
from monitoreos a
inner join monitoreos_resultados b
on b.cuit = a.cuit
and convert(date, b.fecha) = @yesterday
where a.visible = 1
order by a.cliente
*/
select distinct a.cliente
from monitoreos a
inner join monitoreos_resultados b on (b.cuit = a.cuit and convert(date, b.fecha) = @yesterday)
/* 29/07/2024: controla que el cliente este activo */
inner join clientes_productos cp on cp.cliente = a.cliente
inner join clientes c on c.cliente = a.cliente and c.estado_cliente = 'A'
/* 29/07/2024: Si el cliente está unificado, veo que el cliente padre esté activo y tenga el producto MAN activo */
left join clientes_union u on u.cliente = a.cliente
left join clientes cu on cu.cliente = u.cliente_union
left join clientes_productos cpu on cpu.cliente = u.cliente_union and cpu.id_producto = 16
/**/
where a.visible = 1
and cp.id_producto = 16 --> Mantenimiento de cartera
and cp.activo = 1
/* 29/07/2024: Si el cliente está unificado, veo que el cliente padre esté activo y tenga el producto MAN activo */
and iif(u.cliente_union is not null, iif((cu.estado_cliente = 'A' and cpu.activo = 1),1,0) , 1) = 1
/**/
--and a.cliente = 20872528
order by a.cliente
-- apertura del cursor
OPEN bn
-- Lectura de la primera fila del cursor
Fetch next from bn INTO @r_cliente --, @r_user_id
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @r_mail = null
/*
if (@r_user_id is not null and @r_user_id > 0)
begin
set @r_mail = (select email from users where user_id = @r_user_id)
end
*/
if (@r_mail is null)
begin
DECLARE cmail CURSOR FOR
select distinct email from users
where user_id in (select user_id from usuarios_productos where cliente = @r_cliente)
and email is not null
OPEN cmail
Fetch next from bn INTO @r_mail
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @r_nombre_cliente = (select nombre_cliente from clientes where cliente = @r_cliente)
insert into @respuesta values (@r_cliente, @r_nombre_cliente, @r_mail)
FETCH next from cmail INTO @r_mail
END
close cmail
deallocate cmail
end
Fetch next from bn INTO @r_cliente --, @r_user_id
END
CLOSE bn
DEALLOCATE bn
select distinct cliente, nombre_cliente, email from @respuesta
END
GO
GO
--exec monitoreos_envio_mails_con_novedades
CREATE OR ALTER PROCEDURE dbo.monitoreos_envio_mails_con_novedades
@yesterday date = null
AS
BEGIN
SET NOCOUNT ON;
if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end
declare @respuesta table (cliente int, nombre_cliente varchar(200), email varchar(200))
declare @r_cliente int
declare @r_user_id int
declare @r_nombre_cliente varchar(200)
declare @r_mail varchar(200)
DECLARE bn CURSOR FOR
/*
--declare @yesterday date = getdate() - 1
select distinct a.cliente --, a.user_id
from monitoreos a
inner join monitoreos_resultados b
on b.cuit = a.cuit
and convert(date, b.fecha) = @yesterday
where a.visible = 1
order by a.cliente
*/
select distinct a.cliente
from monitoreos a
inner join monitoreos_resultados b on (b.cuit = a.cuit and convert(date, b.fecha) = @yesterday)
/* 29/07/2024: controla que el cliente este activo */
inner join clientes_productos cp on cp.cliente = a.cliente
inner join clientes c on c.cliente = a.cliente and c.estado_cliente = 'A'
/* 29/07/2024: Si el cliente está unificado, veo que el cliente padre esté activo y tenga el producto MAN activo */
left join clientes_union u on u.cliente = a.cliente
left join clientes cu on cu.cliente = u.cliente_union
left join clientes_productos cpu on cpu.cliente = u.cliente_union and cpu.id_producto = 16
/**/
where a.visible = 1
and cp.id_producto = 16 --> Mantenimiento de cartera
and cp.activo = 1
/* 29/07/2024: Si el cliente está unificado, veo que el cliente padre esté activo y tenga el producto MAN activo */
and iif(u.cliente_union is not null, iif((cu.estado_cliente = 'A' and cpu.activo = 1),1,0) , 1) = 1
/**/
--and a.cliente = 20872528
order by a.cliente
-- apertura del cursor
OPEN bn
-- Lectura de la primera fila del cursor
Fetch next from bn INTO @r_cliente --, @r_user_id
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @r_mail = null
/*
if (@r_user_id is not null and @r_user_id > 0)
begin
set @r_mail = (select email from users where user_id = @r_user_id)
end
*/
if (@r_mail is null)
begin
DECLARE cmail CURSOR FOR
select distinct email from users
where user_id in (select user_id from usuarios_productos where cliente = @r_cliente)
and email is not null
OPEN cmail
Fetch next from bn INTO @r_mail
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @r_nombre_cliente = (select nombre_cliente from clientes where cliente = @r_cliente)
insert into @respuesta values (@r_cliente, @r_nombre_cliente, @r_mail)
FETCH next from cmail INTO @r_mail
END
close cmail
deallocate cmail
end
Fetch next from bn INTO @r_cliente --, @r_user_id
END
CLOSE bn
DEALLOCATE bn
select distinct cliente, nombre_cliente, email from @respuesta
END
GO
Depends On
7Used By
No items found