Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
14| Name | Data Type | Length | Description |
|---|---|---|---|
| @p_accion | varchar | 30 | |
| @p_lista | varchar | -1 | |
| @estado_informe | varchar | 1 | |
| @prioridad | tinyint | 1 | |
| @fecha_inicio | datetime | 8 | |
| @fecha_comprometida | datetime | 8 | |
| @idioma | smallint | 2 | |
| @ingresado_por | int | 4 | |
| @idTeam | int | 4 | |
| @user_id | int | 4 | |
| @proximo_llamado | datetime | 8 | |
| @asignado | int | 4 | |
| @nota | varchar | -1 | |
| @cliente | int | 4 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: MIS
-- Create date: 12/06/2024
-- Description: Hace un update masivo de la tabla informes, a determinados campos de una lista de informes (p_lista)
-- =============================================
-- select * from informes where informe in (1,2)
--
--exec [informes_updates] 'SELECT', '100842700,100849691'
/*
select informe, informe_padre, fecha_inicio, fecha_comprometida
from informes
where (informe_padre in (100812889,100786818,100787806,100790360,100792111,100792170,100796231,100792045,100800357,100811549,100811494,100813806,100820527,100820551,100820543,100820535,100836632,100836664,100834904,100834896,100836870,100836814,100836886,100839416,100839253,100839277,100840011,100840135,100840255,100840407,100840087,100840439,100840383,100840399,100840159,100840063,100840295,100840327,100840455,100840151,100840415,100840183,100840463,100840071,100840343,100840231,100840119,100840303,100840199,100840239,100840111,100840143,100840447,100840103,100840055,100840079,100840047,100840215,100840367,100840311,100840335,100840175,100840263,100840279,100840127,100840271,100840287,100840039,100840391,100840375,100840423,100842094,100842102,100849404,100849396,100850283,100850331,100850315,100850299,100850275,100850291,100850339,100850323,100850307,100851000,100850982,100850974,100850956,100840431,100852309,100852333,100852301,100852317,100852325,100840095,100851008,100835966,100836053,100836390,100836736,100836414,100835974,100836134,100835901,100836406,100836398,100836240,100836361,100836672,100836198,100836822,100836332,100836298,100836680,100835925,100836075,100836544,100836592,100836472,100836031,100836846,100836504,100836480,100836488,100836282,100836656,100835917,100836752,100836878,100836744,100836712,100836624,100836023,100836097,100836688,100836640,100836528,100836608,100836600,100836536,100836206,100836105,100836576,100836720,100836306,100835958,100836382,100836696,100836552,100836448,100836314,100835950,100836838,100836798,100836430,100836806,100836340,100836166,100836113,100836464,100836422,100836584,100836704,100836150,100836648,100836182,100836174,100836830,100836728,100836496,100835860,100836854,100836790,100835868,100835999,100836616,100836862,100836256,100852829,100852845,100852878,100852821,100852837,100852853)
or informe in (100812889,100786818,100787806,100790360,100792111,100792170,100796231,100792045,100800357,100811549,100811494,100813806,100820527,100820551,100820543,100820535,100836632,100836664,100834904,100834896,100836870,100836814,100836886,100839416,100839253,100839277,100840011,100840135,100840255,100840407,100840087,100840439,100840383,100840399,100840159,100840063,100840295,100840327,100840455,100840151,100840415,100840183,100840463,100840071,100840343,100840231,100840119,100840303,100840199,100840239,100840111,100840143,100840447,100840103,100840055,100840079,100840047,100840215,100840367,100840311,100840335,100840175,100840263,100840279,100840127,100840271,100840287,100840039,100840391,100840375,100840423,100842094,100842102,100849404,100849396,100850283,100850331,100850315,100850299,100850275,100850291,100850339,100850323,100850307,100851000,100850982,100850974,100850956,100840431,100852309,100852333,100852301,100852317,100852325,100840095,100851008,100835966,100836053,100836390,100836736,100836414,100835974,100836134,100835901,100836406,100836398,100836240,100836361,100836672,100836198,100836822,100836332,100836298,100836680,100835925,100836075,100836544,100836592,100836472,100836031,100836846,100836504,100836480,100836488,100836282,100836656,100835917,100836752,100836878,100836744,100836712,100836624,100836023,100836097,100836688,100836640,100836528,100836608,100836600,100836536,100836206,100836105,100836576,100836720,100836306,100835958,100836382,100836696,100836552,100836448,100836314,100835950,100836838,100836798,100836430,100836806,100836340,100836166,100836113,100836464,100836422,100836584,100836704,100836150,100836648,100836182,100836174,100836830,100836728,100836496,100835860,100836854,100836790,100835868,100835999,100836616,100836862,100836256,100852829,100852845,100852878,100852821,100852837,100852853)
)
order by informe_padre, informe
*/
CREATE OR ALTER PROCEDURE dbo.informes_updates
@p_accion varchar(30) = NULL,
@p_lista varchar(max) = NULL,
@estado_informe varchar(1) = NULL,
@prioridad tinyint = NULL,
@fecha_inicio datetime = NULL,
@fecha_comprometida datetime = NULL,
@idioma smallint = NULL,
@ingresado_por int = NULL,
@idTeam int = NULL,
@user_id int = NULL,
@proximo_llamado datetime = NULL,
@asignado int = NULL,
@nota varchar(max) = NULL,
@cliente int = NULL
AS
BEGIN
SET NOCOUNT ON;
set @p_lista = REPLACE (@p_lista,';',',') -- por si lo mandan separados por ;
while (@p_lista like '%,,%') -- por si hay varias comas seguidas
begin
set @p_lista = REPLACE (@p_lista,',,',',')
end
while (@p_lista like '%,') -- si termina en coma, elimina ultimo caracter
begin
set @p_lista = SUBSTRING (@p_lista, 1, Len(@p_lista) - 1)
end
declare @informes table (informe int)
insert into @informes select item from dbo.DelimitedSplit8K(@p_lista, ',')
if (@p_accion = 'SELECT')
begin
select informes.informe, informes.informe_padre, informes.fecha_inicio, informes.fecha_comprometida, LN_demo.idTeam
from informes
left join ln_demo on ln_demo.informe = informes.informe_padre
where informes.informe_padre in (select informe from @informes)
or informes.informe in (select informe from @informes)
union
select informes.informe, informes.informe_padre, informes.fecha_inicio, informes.fecha_comprometida, LN_demo.idTeam
from informes
left join ln_demo on ln_demo.informe = informes.informe
where informes.informe in (select informe_padre
from informes
where informe in (select informe from @informes))
order by informe_padre, informe
end
if (@p_accion = 'UPDATE')
begin
declare @total_informes int = 0
declare @total_ln_DEMO int = 0
-- Si la lista está vacía o no se encuentran esos nros de informe: No hace nada
if (select COUNT(*) from @informes) = 0
begin
select @total_informes as total_informes, @total_ln_DEMO as total_contrapartes
return
end
select @total_informes = COUNT(*) from informes where informe in (select informe from @informes)
select @total_ln_DEMO = COUNT(*) from ln_DEMO where informe in (select informe from @informes)
if (@total_informes = 0 and @total_ln_DEMO = 0)
begin
select @total_informes as total_informes, @total_ln_DEMO as total_contrapartes
return
end
-- Si hay informes
declare @usuario varchar(200) = null
declare @usuario2 varchar(200) = null
declare @usuario3 varchar(200) = null
declare @Team varchar(200) = null
if (@user_id is not null)
begin
select @usuario = users.first_name + ' ' + users.last_name from users where user_id = @user_id
if (@usuario is null) select @usuario = username from users where user_id = @user_id
end
if (@ingresado_por is not null)
begin
select @usuario2 = users.first_name + ' ' + users.last_name from users where user_id = @ingresado_por
if (@usuario2 is null) select @usuario2 = username from users where user_id = @ingresado_por
end
if (@asignado is not null)
begin
select @usuario3 = users.first_name + ' ' + users.last_name from users where user_id = @asignado
if (@usuario3 is null) select @usuario3 = username from users where user_id = @asignado
end
if (@idTeam is not null)
begin
select @Team = nombre from LN_Teams where id_team = @idTeam
end
declare @log varchar(max) = '[' + isnull(@usuario,'') + ' - ' + format(getdate(),'dd/MM/yyyy hh:mm') + ']: Cambio masivo'
if (@estado_informe is not null)
begin
set @log = @log + ' - Estado informe: ' +
case
when @estado_informe = 'I' then 'Ingresado'
when @estado_informe = 'A' then 'No calificado'
when @estado_informe = 'X' then 'Carga Comercial'
when @estado_informe = 'T' then 'Respondido'
when @estado_informe = 'P' then 'En gestión'
when @estado_informe = 'W' then 'En espera'
when @estado_informe = 'M' then 'A Completar'
when @estado_informe = 'E' then 'En proceso'
when @estado_informe = 'F' then 'Finalizado'
when @estado_informe = 'K' then 'Aprobado'
when @estado_informe = 'R' then 'Observado'
when @estado_informe = 'C' then 'Rechazado'
when @estado_informe = 'N' then 'Anulado'
else @estado_informe
end
end
if (@prioridad is not null) set @log = @log + ' - Dedicación: ' + convert(varchar,@prioridad)
if (@fecha_inicio is not null) set @log = @log + ' - Fecha inicio: ' + format(@fecha_inicio,'dd/MM/yyyy hh:mm')
if (@fecha_comprometida is not null) set @log = @log + ' - Fecha comprometida: ' + format(@fecha_comprometida,'dd/MM/yyyy hh:mm')
if (@idioma is not null) set @log = @log + ' - Idioma: ' + @idioma
if (@ingresado_por is not null) set @log = @log + ' - Solicitado por: ' + isnull(@usuario2,convert(varchar,@ingresado_por))
if (@asignado is not null) set @log = @log + ' - Asignado a: ' + isnull(@usuario3,convert(varchar,@asignado))
if (@idTeam is not null) set @log = @log + ' - Team: ' + isnull(@Team,convert(varchar,@idTeam))
if (@proximo_llamado is not null) set @log = @log + ' - Próximo llamado: ' + format(@proximo_llamado,'dd/MM/yyyy hh:mm')
if (@nota is not null) set @log = @log + ' - Nota: ' + @nota
--select @log
if (@total_informes > 0 and
(@estado_informe is not null or
@prioridad is not null or
@fecha_inicio is not null or
@fecha_comprometida is not null or
@idioma is not null or
@asignado is not null or
@idTeam is not null) -- team solo por el log
)
begin
update informes
set estado_informe = isnull(@estado_informe,estado_informe),
prioridad = isnull(@prioridad,prioridad),
fecha_inicio = isnull(@fecha_inicio,fecha_inicio),
fecha_iniciado = isnull(@fecha_inicio,fecha_iniciado),
fecha_comprometida = isnull(@fecha_comprometida,fecha_comprometida),
idioma = isnull(@idioma,idioma),
ingresado_por = isnull(@ingresado_por,ingresado_por),
asignado = isnull(@asignado,asignado),
proximo_llamado = isnull(@proximo_llamado,proximo_llamado),
historial_informe = isnull(convert(varchar(max),historial_informe),'') + isnull(@log,'')
where informe in (select informe from @informes)
end
if (@idTeam is not null and @total_ln_DEMO > 0)
begin
update ln_DEMO
set idTeam = isnull(@idTeam,idTeam)
where informe in (select informe from @informes)
end
if (@nota is not null)
begin
declare @nota2 varchar(max) = 'Cambio masivo: ' + @nota
EXEC [dbo].[insertar_trazabilidad] @nota2, @cliente, 'Cambio masivo'
end
select @total_informes as total_informes, @total_ln_DEMO as total_contrapartes
end
END
GO
GO
-- =============================================
-- Author: MIS
-- Create date: 12/06/2024
-- Description: Hace un update masivo de la tabla informes, a determinados campos de una lista de informes (p_lista)
-- =============================================
-- select * from informes where informe in (1,2)
--
--exec [informes_updates] 'SELECT', '100842700,100849691'
/*
select informe, informe_padre, fecha_inicio, fecha_comprometida
from informes
where (informe_padre in (100812889,100786818,100787806,100790360,100792111,100792170,100796231,100792045,100800357,100811549,100811494,100813806,100820527,100820551,100820543,100820535,100836632,100836664,100834904,100834896,100836870,100836814,100836886,100839416,100839253,100839277,100840011,100840135,100840255,100840407,100840087,100840439,100840383,100840399,100840159,100840063,100840295,100840327,100840455,100840151,100840415,100840183,100840463,100840071,100840343,100840231,100840119,100840303,100840199,100840239,100840111,100840143,100840447,100840103,100840055,100840079,100840047,100840215,100840367,100840311,100840335,100840175,100840263,100840279,100840127,100840271,100840287,100840039,100840391,100840375,100840423,100842094,100842102,100849404,100849396,100850283,100850331,100850315,100850299,100850275,100850291,100850339,100850323,100850307,100851000,100850982,100850974,100850956,100840431,100852309,100852333,100852301,100852317,100852325,100840095,100851008,100835966,100836053,100836390,100836736,100836414,100835974,100836134,100835901,100836406,100836398,100836240,100836361,100836672,100836198,100836822,100836332,100836298,100836680,100835925,100836075,100836544,100836592,100836472,100836031,100836846,100836504,100836480,100836488,100836282,100836656,100835917,100836752,100836878,100836744,100836712,100836624,100836023,100836097,100836688,100836640,100836528,100836608,100836600,100836536,100836206,100836105,100836576,100836720,100836306,100835958,100836382,100836696,100836552,100836448,100836314,100835950,100836838,100836798,100836430,100836806,100836340,100836166,100836113,100836464,100836422,100836584,100836704,100836150,100836648,100836182,100836174,100836830,100836728,100836496,100835860,100836854,100836790,100835868,100835999,100836616,100836862,100836256,100852829,100852845,100852878,100852821,100852837,100852853)
or informe in (100812889,100786818,100787806,100790360,100792111,100792170,100796231,100792045,100800357,100811549,100811494,100813806,100820527,100820551,100820543,100820535,100836632,100836664,100834904,100834896,100836870,100836814,100836886,100839416,100839253,100839277,100840011,100840135,100840255,100840407,100840087,100840439,100840383,100840399,100840159,100840063,100840295,100840327,100840455,100840151,100840415,100840183,100840463,100840071,100840343,100840231,100840119,100840303,100840199,100840239,100840111,100840143,100840447,100840103,100840055,100840079,100840047,100840215,100840367,100840311,100840335,100840175,100840263,100840279,100840127,100840271,100840287,100840039,100840391,100840375,100840423,100842094,100842102,100849404,100849396,100850283,100850331,100850315,100850299,100850275,100850291,100850339,100850323,100850307,100851000,100850982,100850974,100850956,100840431,100852309,100852333,100852301,100852317,100852325,100840095,100851008,100835966,100836053,100836390,100836736,100836414,100835974,100836134,100835901,100836406,100836398,100836240,100836361,100836672,100836198,100836822,100836332,100836298,100836680,100835925,100836075,100836544,100836592,100836472,100836031,100836846,100836504,100836480,100836488,100836282,100836656,100835917,100836752,100836878,100836744,100836712,100836624,100836023,100836097,100836688,100836640,100836528,100836608,100836600,100836536,100836206,100836105,100836576,100836720,100836306,100835958,100836382,100836696,100836552,100836448,100836314,100835950,100836838,100836798,100836430,100836806,100836340,100836166,100836113,100836464,100836422,100836584,100836704,100836150,100836648,100836182,100836174,100836830,100836728,100836496,100835860,100836854,100836790,100835868,100835999,100836616,100836862,100836256,100852829,100852845,100852878,100852821,100852837,100852853)
)
order by informe_padre, informe
*/
CREATE OR ALTER PROCEDURE dbo.informes_updates
@p_accion varchar(30) = NULL,
@p_lista varchar(max) = NULL,
@estado_informe varchar(1) = NULL,
@prioridad tinyint = NULL,
@fecha_inicio datetime = NULL,
@fecha_comprometida datetime = NULL,
@idioma smallint = NULL,
@ingresado_por int = NULL,
@idTeam int = NULL,
@user_id int = NULL,
@proximo_llamado datetime = NULL,
@asignado int = NULL,
@nota varchar(max) = NULL,
@cliente int = NULL
AS
BEGIN
SET NOCOUNT ON;
set @p_lista = REPLACE (@p_lista,';',',') -- por si lo mandan separados por ;
while (@p_lista like '%,,%') -- por si hay varias comas seguidas
begin
set @p_lista = REPLACE (@p_lista,',,',',')
end
while (@p_lista like '%,') -- si termina en coma, elimina ultimo caracter
begin
set @p_lista = SUBSTRING (@p_lista, 1, Len(@p_lista) - 1)
end
declare @informes table (informe int)
insert into @informes select item from dbo.DelimitedSplit8K(@p_lista, ',')
if (@p_accion = 'SELECT')
begin
select informes.informe, informes.informe_padre, informes.fecha_inicio, informes.fecha_comprometida, LN_demo.idTeam
from informes
left join ln_demo on ln_demo.informe = informes.informe_padre
where informes.informe_padre in (select informe from @informes)
or informes.informe in (select informe from @informes)
union
select informes.informe, informes.informe_padre, informes.fecha_inicio, informes.fecha_comprometida, LN_demo.idTeam
from informes
left join ln_demo on ln_demo.informe = informes.informe
where informes.informe in (select informe_padre
from informes
where informe in (select informe from @informes))
order by informe_padre, informe
end
if (@p_accion = 'UPDATE')
begin
declare @total_informes int = 0
declare @total_ln_DEMO int = 0
-- Si la lista está vacía o no se encuentran esos nros de informe: No hace nada
if (select COUNT(*) from @informes) = 0
begin
select @total_informes as total_informes, @total_ln_DEMO as total_contrapartes
return
end
select @total_informes = COUNT(*) from informes where informe in (select informe from @informes)
select @total_ln_DEMO = COUNT(*) from ln_DEMO where informe in (select informe from @informes)
if (@total_informes = 0 and @total_ln_DEMO = 0)
begin
select @total_informes as total_informes, @total_ln_DEMO as total_contrapartes
return
end
-- Si hay informes
declare @usuario varchar(200) = null
declare @usuario2 varchar(200) = null
declare @usuario3 varchar(200) = null
declare @Team varchar(200) = null
if (@user_id is not null)
begin
select @usuario = users.first_name + ' ' + users.last_name from users where user_id = @user_id
if (@usuario is null) select @usuario = username from users where user_id = @user_id
end
if (@ingresado_por is not null)
begin
select @usuario2 = users.first_name + ' ' + users.last_name from users where user_id = @ingresado_por
if (@usuario2 is null) select @usuario2 = username from users where user_id = @ingresado_por
end
if (@asignado is not null)
begin
select @usuario3 = users.first_name + ' ' + users.last_name from users where user_id = @asignado
if (@usuario3 is null) select @usuario3 = username from users where user_id = @asignado
end
if (@idTeam is not null)
begin
select @Team = nombre from LN_Teams where id_team = @idTeam
end
declare @log varchar(max) = '[' + isnull(@usuario,'') + ' - ' + format(getdate(),'dd/MM/yyyy hh:mm') + ']: Cambio masivo'
if (@estado_informe is not null)
begin
set @log = @log + ' - Estado informe: ' +
case
when @estado_informe = 'I' then 'Ingresado'
when @estado_informe = 'A' then 'No calificado'
when @estado_informe = 'X' then 'Carga Comercial'
when @estado_informe = 'T' then 'Respondido'
when @estado_informe = 'P' then 'En gestión'
when @estado_informe = 'W' then 'En espera'
when @estado_informe = 'M' then 'A Completar'
when @estado_informe = 'E' then 'En proceso'
when @estado_informe = 'F' then 'Finalizado'
when @estado_informe = 'K' then 'Aprobado'
when @estado_informe = 'R' then 'Observado'
when @estado_informe = 'C' then 'Rechazado'
when @estado_informe = 'N' then 'Anulado'
else @estado_informe
end
end
if (@prioridad is not null) set @log = @log + ' - Dedicación: ' + convert(varchar,@prioridad)
if (@fecha_inicio is not null) set @log = @log + ' - Fecha inicio: ' + format(@fecha_inicio,'dd/MM/yyyy hh:mm')
if (@fecha_comprometida is not null) set @log = @log + ' - Fecha comprometida: ' + format(@fecha_comprometida,'dd/MM/yyyy hh:mm')
if (@idioma is not null) set @log = @log + ' - Idioma: ' + @idioma
if (@ingresado_por is not null) set @log = @log + ' - Solicitado por: ' + isnull(@usuario2,convert(varchar,@ingresado_por))
if (@asignado is not null) set @log = @log + ' - Asignado a: ' + isnull(@usuario3,convert(varchar,@asignado))
if (@idTeam is not null) set @log = @log + ' - Team: ' + isnull(@Team,convert(varchar,@idTeam))
if (@proximo_llamado is not null) set @log = @log + ' - Próximo llamado: ' + format(@proximo_llamado,'dd/MM/yyyy hh:mm')
if (@nota is not null) set @log = @log + ' - Nota: ' + @nota
--select @log
if (@total_informes > 0 and
(@estado_informe is not null or
@prioridad is not null or
@fecha_inicio is not null or
@fecha_comprometida is not null or
@idioma is not null or
@asignado is not null or
@idTeam is not null) -- team solo por el log
)
begin
update informes
set estado_informe = isnull(@estado_informe,estado_informe),
prioridad = isnull(@prioridad,prioridad),
fecha_inicio = isnull(@fecha_inicio,fecha_inicio),
fecha_iniciado = isnull(@fecha_inicio,fecha_iniciado),
fecha_comprometida = isnull(@fecha_comprometida,fecha_comprometida),
idioma = isnull(@idioma,idioma),
ingresado_por = isnull(@ingresado_por,ingresado_por),
asignado = isnull(@asignado,asignado),
proximo_llamado = isnull(@proximo_llamado,proximo_llamado),
historial_informe = isnull(convert(varchar(max),historial_informe),'') + isnull(@log,'')
where informe in (select informe from @informes)
end
if (@idTeam is not null and @total_ln_DEMO > 0)
begin
update ln_DEMO
set idTeam = isnull(@idTeam,idTeam)
where informe in (select informe from @informes)
end
if (@nota is not null)
begin
declare @nota2 varchar(max) = 'Cambio masivo: ' + @nota
EXEC [dbo].[insertar_trazabilidad] @nota2, @cliente, 'Cambio masivo'
end
select @total_informes as total_informes, @total_ln_DEMO as total_contrapartes
end
END
GO
Depends On
6Used By
No items found