Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- [sp_balances_2007_grupos_actualiza] '30695556531'
CREATE OR ALTER PROCEDURE dbo.sp_balances_2007_grupos_empleados_2
AS
BEGIN
SET NOCOUNT ON;
declare @id int,
@cuit varchar(20),
@fecha_balance date,
@desde char(4),
@hasta char(4),
@anio int,
@aniomes int,
@mes int,
@tabla1 varchar(100),
@tabla2 varchar(100),
@periodo1 int,
@periodo2 int,
@cant_empleados int,
@sueldos decimal(20,2)
/*-- para año 2011 -- */
UPDATE balances_por_grupo
set cant_empleados = null, sueldos = null, empleados_periodo = null
where fecha_balance between '2016-01-01' and '2016-12-31'
DECLARE C_balances CURSOR FOR
select id, cuit, fecha_balance
from balances_por_grupo
--where cuit = '30709723274'
--where id > 16470
/*-- para año 2011 -- */
where fecha_balance between '2016-01-01' and '2016-12-31'
order by id
OPEN C_balances
FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
WHILE @@fetch_status = 0
BEGIN
set @anio = DATEPART(year,@fecha_balance)
set @mes = DATEPART(month,@fecha_balance)
set @aniomes = convert(varchar(2),@anio-2000) + right('00'+convert(varchar(2),DATEPART(month,@fecha_balance)),2)
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
select @tabla1 = 'empleados',
@periodo1 = max(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = max(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
--select @periodo1, @periodo2
if (@periodo1 is null and @periodo2 is null)
begin
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
if (@mes < 6)
begin
set @anio = DATEPART(year,@fecha_balance)-1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = max(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = max(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
else
begin
set @anio = DATEPART(year,@fecha_balance)+1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = MIN(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = MIN(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
if (@periodo1 is null and @periodo2 is null)
begin
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
set @anio = DATEPART(year,@fecha_balance)
set @anio = DATEPART(year,@fecha_balance)+1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = MIN(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = MIN(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
if (@periodo1 is null and @periodo2 is null)
begin
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
set @anio = DATEPART(year,@fecha_balance)
set @anio = DATEPART(year,@fecha_balance)-1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = max(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = max(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
--select @periodo1, @periodo2
if (@periodo1 is null and @periodo2 is null)
begin
FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
continue
end
end
if (@periodo1 is not null)
begin
set @cant_empleados = (select count(distinct cuil) from empleados where cuit = @cuit and periodo = @periodo1)
set @sueldos = (select sum(remimp) from empleados a
where a.cuit = @cuit
and a.periodo = @periodo1
and not exists (select * from empleados b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.id_empleado > a.id_empleado))
/*
select @cant_empleados = count(cuil),
@sueldos = sum(remimp)
from empleados a
where a.cuit = @cuit
and a.periodo = @periodo1
and id_empleado in (select id_empleado
from empleados b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.created = (select max(created)
from empleados c
where b.cuit = c.cuit
and b.cuil = c.cuil
and b.periodo = c.periodo)
)
*/
update balances_por_grupo
set cant_empleados = @cant_empleados, sueldos = @sueldos, empleados_periodo = @periodo1 where id = @id
end
else
begin
set @cant_empleados = (select count(distinct cuil) from empleados_historico where cuit = @cuit and periodo = @periodo2)
set @sueldos = (select sum(remimp) from empleados_historico a
where a.cuit = @cuit
and a.periodo = @periodo2
and not exists (select * from empleados_historico b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.id_empleado > a.id_empleado))
/*
select @cant_empleados = count(cuil),
@sueldos = sum(remimp)
from empleados_historico a
where a.cuit = @cuit
and a.periodo = @periodo2
and id_empleado in (select id_empleado
from empleados_historico b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.created = (select max(created)
from empleados_historico c
where b.cuit = c.cuit
and b.cuil = c.cuil
and b.periodo = c.periodo)
)
*/
update balances_por_grupo
set cant_empleados = @cant_empleados, sueldos = @sueldos, empleados_periodo = @periodo2 where id = @id
end
FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
END
CLOSE C_Balances
DEALLOCATE C_Balances
END
GO
GO
-- [sp_balances_2007_grupos_actualiza] '30695556531'
CREATE OR ALTER PROCEDURE dbo.sp_balances_2007_grupos_empleados_2
AS
BEGIN
SET NOCOUNT ON;
declare @id int,
@cuit varchar(20),
@fecha_balance date,
@desde char(4),
@hasta char(4),
@anio int,
@aniomes int,
@mes int,
@tabla1 varchar(100),
@tabla2 varchar(100),
@periodo1 int,
@periodo2 int,
@cant_empleados int,
@sueldos decimal(20,2)
/*-- para año 2011 -- */
UPDATE balances_por_grupo
set cant_empleados = null, sueldos = null, empleados_periodo = null
where fecha_balance between '2016-01-01' and '2016-12-31'
DECLARE C_balances CURSOR FOR
select id, cuit, fecha_balance
from balances_por_grupo
--where cuit = '30709723274'
--where id > 16470
/*-- para año 2011 -- */
where fecha_balance between '2016-01-01' and '2016-12-31'
order by id
OPEN C_balances
FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
WHILE @@fetch_status = 0
BEGIN
set @anio = DATEPART(year,@fecha_balance)
set @mes = DATEPART(month,@fecha_balance)
set @aniomes = convert(varchar(2),@anio-2000) + right('00'+convert(varchar(2),DATEPART(month,@fecha_balance)),2)
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
select @tabla1 = 'empleados',
@periodo1 = max(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = max(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
--select @periodo1, @periodo2
if (@periodo1 is null and @periodo2 is null)
begin
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
if (@mes < 6)
begin
set @anio = DATEPART(year,@fecha_balance)-1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = max(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = max(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
else
begin
set @anio = DATEPART(year,@fecha_balance)+1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = MIN(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = MIN(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
if (@periodo1 is null and @periodo2 is null)
begin
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
set @anio = DATEPART(year,@fecha_balance)
set @anio = DATEPART(year,@fecha_balance)+1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = MIN(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = MIN(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
if (@periodo1 is null and @periodo2 is null)
begin
set @tabla1 = null
set @tabla2 = null
set @periodo1 = null
set @periodo2 = null
set @anio = DATEPART(year,@fecha_balance)
set @anio = DATEPART(year,@fecha_balance)-1
set @desde = convert(varchar,@anio-2000) + '01'
set @hasta = convert(varchar,@anio-2000) + '12'
select @tabla1 = 'empleados',
@periodo1 = max(periodo)
from empleados
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
select @tabla2 = 'historico',
@periodo2 = max(periodo)
from empleados_historico
where cuit = @cuit
and periodo between @desde and @hasta
and CONVERT(VARCHAR,periodo) not like '%12'
and CONVERT(VARCHAR,periodo) not like '%06'
and remimp > 0
end
--select @periodo1, @periodo2
if (@periodo1 is null and @periodo2 is null)
begin
FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
continue
end
end
if (@periodo1 is not null)
begin
set @cant_empleados = (select count(distinct cuil) from empleados where cuit = @cuit and periodo = @periodo1)
set @sueldos = (select sum(remimp) from empleados a
where a.cuit = @cuit
and a.periodo = @periodo1
and not exists (select * from empleados b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.id_empleado > a.id_empleado))
/*
select @cant_empleados = count(cuil),
@sueldos = sum(remimp)
from empleados a
where a.cuit = @cuit
and a.periodo = @periodo1
and id_empleado in (select id_empleado
from empleados b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.created = (select max(created)
from empleados c
where b.cuit = c.cuit
and b.cuil = c.cuil
and b.periodo = c.periodo)
)
*/
update balances_por_grupo
set cant_empleados = @cant_empleados, sueldos = @sueldos, empleados_periodo = @periodo1 where id = @id
end
else
begin
set @cant_empleados = (select count(distinct cuil) from empleados_historico where cuit = @cuit and periodo = @periodo2)
set @sueldos = (select sum(remimp) from empleados_historico a
where a.cuit = @cuit
and a.periodo = @periodo2
and not exists (select * from empleados_historico b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.id_empleado > a.id_empleado))
/*
select @cant_empleados = count(cuil),
@sueldos = sum(remimp)
from empleados_historico a
where a.cuit = @cuit
and a.periodo = @periodo2
and id_empleado in (select id_empleado
from empleados_historico b
where b.cuit = a.cuit
and b.cuil = a.cuil
and b.periodo = a.periodo
and b.created = (select max(created)
from empleados_historico c
where b.cuit = c.cuit
and b.cuil = c.cuil
and b.periodo = c.periodo)
)
*/
update balances_por_grupo
set cant_empleados = @cant_empleados, sueldos = @sueldos, empleados_periodo = @periodo2 where id = @id
end
FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
END
CLOSE C_Balances
DEALLOCATE C_Balances
END
GO
Depends On
3Used By
No items found