Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
1| Name | Data Type | Length | Description |
|---|---|---|---|
| @paso | int | 4 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [YPF] 3
@paso = 1 -- Completa con datos de padron buscando como cuerpo_clave el telefono
@paso = 2 -- Reemplazo los caractares raros en apellido y nombre
@paso = 3 -- Busca Apellido y Nombre enviado exactamente igual al del padron
@paso = 4 -- Busca Apellido igual al de Padron
@paso = 5 -- Busco los casos de apellidos con '
@paso = 6 -- De los que quedaron sin encontrar, vemos si hay otra persona con el mismo cuerpo_clave en padron
Despues del paso 6, volvemos a hacer los pasos 3 4 y 5 por si engancha ahora con la nueva persona
@paso = 7 -- De los que quedaron sin encontrar, busco en padron el del mismo sexo
Despues del paso 7, volvemos a hacer los pasos 3 4 y 5 por si engancha ahora con la nueva persona
*/
CREATE OR ALTER PROCEDURE dbo.YPF
@paso int = null
AS
BEGIN
SET NOCOUNT ON;
declare @tel varchar(150), @total int = 0, @actualizados int = 0
declare @id_padron int,
@cuit varchar(20),
@nombre_persona varchar(150),
@apellidos varchar(150),
@nombres varchar(150),
@habilitado bit,
@sexo char(1)
if (@paso = 1) -- Completa con datos de padron buscando como cuerpo_clave el telefono
begin
DECLARE C_ypf CURSOR FOR
select telefono from YPF_DataEnrich_Total
where cuit is null
OPEN C_ypf
FETCH NEXT FROM C_ypf INTO @tel
WHILE @@fetch_status = 0
BEGIN
set @total = @total + 1
if (ISNUMERIC(@tel) = 1)
begin
set @id_padron = null
set @cuit = null
set @nombre_persona = null
set @apellidos = null
set @nombres = null
set @habilitado = null
set @sexo = null
select top 1 @id_padron = pad.id_padron,
@cuit = pad.identificacion,
@nombre_persona = pad.nombre_persona,
@apellidos = pad.apellidos,
@nombres = pad.nombres,
@habilitado = pad.habilitado,
@sexo = pad.sexo
from padrones pad
where pad.habilitado = 1
and (pad.cuerpo_clave = @tel
or pad.cuerpo_clave = '0' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '00' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '000' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '0000' + LTRIM(rtrim(@tel))
)
if (@id_padron is not null)
begin
set @actualizados = @actualizados + 1
update YPF_DataEnrich_Total
set cuit = @cuit,
nombre_persona = @nombre_persona,
apellido_padron= @apellidos,
nombres_padron = @nombres,
habilitado = @habilitado,
sexo = @sexo,
id_padron = @id_padron
where YPF_DataEnrich_Total.Telefono = @tel
and YPF_DataEnrich_Total.cuit is null
end
end
FETCH NEXT FROM C_ypf INTO @tel
END
CLOSE C_ypf
DEALLOCATE C_ypf
select @total as total, @actualizados as actualizados
end
if (@paso = 2) -- Reemplazo los caractares raros
begin
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ñ','Ñ')
from YPF_DataEnrich_Total where Apellido like '%Ñ%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ñ','Ñ')
from YPF_DataEnrich_Total where Nombre like '%Ñ%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'ñ','ñ')
from YPF_DataEnrich_Total where Apellido like '%ñ%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'ñ','ñ')
from YPF_DataEnrich_Total where Nombre like '%ñ%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ó','O')
from YPF_DataEnrich_Total where Apellido like '%Ó%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ó','O')
from YPF_DataEnrich_Total where Nombre like '%Ó%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ã','I')
from YPF_DataEnrich_Total where Apellido like '%Ã%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ã','I')
from YPF_DataEnrich_Total where Nombre like '%Ã%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'É','E')
from YPF_DataEnrich_Total where Apellido like '%É%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'É','E')
from YPF_DataEnrich_Total where Nombre like '%É'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'é','e')
from YPF_DataEnrich_Total where Apellido like '%é%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'é','e')
from YPF_DataEnrich_Total where Nombre like '%é‰'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ü','U')
from YPF_DataEnrich_Total where Apellido like '%Ü%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ü','U')
from YPF_DataEnrich_Total where Nombre like '%܉'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ã’','O')
from YPF_DataEnrich_Total where Apellido like '%Ã’%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ã’','O')
from YPF_DataEnrich_Total where Nombre like '%Ã’%'
update YPF_DataEnrich_Total
set nombre_persona = replace(nombre_persona,'?','Ñ')
from YPF_DataEnrich_Total where nombre_persona like '%?%'
update YPF_DataEnrich_Total
set nombre_persona = replace(nombre_persona,'#','Ñ')
from YPF_DataEnrich_Total where nombre_persona like '%#%'
end
if (@paso = 3) -- Busca Apellido y Nombre enviado exactamente igual al del padron
begin
update YPF_DataEnrich_Total set compara_nombre = 'IGUAL'
where rtrim(ltrim(nombre_persona)) = rtrim(ltrim(Apellido)) + ' ' + rtrim(ltrim(Nombre))
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'IGUAL'
end
if (@paso = 4) -- Busca Apellido igual al de Padron
begin
update YPF_DataEnrich_Total set compara_nombre = 'APELLIDO IGUAL'
where rtrim(ltrim(nombre_persona)) LIKE rtrim(ltrim(Apellido))+'%'
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'APELLIDO IGUAL'
end
if (@paso = 5) -- Busco los casos de apellidos con '
begin
update YPF_DataEnrich_Total set compara_nombre = 'IGUAL'
where rtrim(ltrim(replace(nombre_persona,'''',''))) = rtrim(ltrim(Apellido)) + ' ' + rtrim(ltrim(Nombre))
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'IGUAL'
update YPF_DataEnrich_Total set compara_nombre = 'APELLIDO IGUAL'
where rtrim(ltrim(replace(nombre_persona,'''',''))) LIKE rtrim(ltrim(Apellido))+'%'
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'APELLIDO IGUAL'
end
declare @idp int
if (@paso = 6) -- De los que quedaron sin encontrar, vemos si hay otra persona con el mismo cuerpo_clave en padron
begin
DECLARE C_ypf CURSOR FOR
select telefono, id_padron from YPF_DataEnrich_Total
where compara_nombre is null and id_padron is not null
OPEN C_ypf
FETCH NEXT FROM C_ypf INTO @tel, @idp
WHILE @@fetch_status = 0
BEGIN
set @total = @total + 1
if (ISNUMERIC(@tel) = 1)
begin
set @id_padron = null
set @cuit = null
set @nombre_persona = null
set @apellidos = null
set @nombres = null
set @habilitado = null
set @sexo = null
select top 1 @id_padron = pad.id_padron,
@cuit = pad.identificacion,
@nombre_persona = pad.nombre_persona,
@apellidos = pad.apellidos,
@nombres = pad.nombres,
@habilitado = pad.habilitado,
@sexo = pad.sexo
from padrones pad
where pad.habilitado = 1
and (pad.cuerpo_clave = @tel
or pad.cuerpo_clave = '0' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '00' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '000' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '0000' + LTRIM(rtrim(@tel))
)
and pad.id_padron != @idp
if (@id_padron is not null)
begin
set @actualizados = @actualizados + 1
update YPF_DataEnrich_Total
set cuit = @cuit,
nombre_persona = @nombre_persona,
apellido_padron= @apellidos,
nombres_padron = @nombres,
habilitado = @habilitado,
sexo = @sexo,
id_padron = @id_padron
where YPF_DataEnrich_Total.Telefono = @tel
and id_padron = @idp
end
end
FETCH NEXT FROM C_ypf INTO @tel, @idp
END
CLOSE C_ypf
DEALLOCATE C_ypf
select @total as total, @actualizados as actualizados
end
if (@paso = 7) -- De los que quedaron sin encontrar, busco en padron el del mismo sexo
begin
declare @genero char(1)
DECLARE C_ypf CURSOR FOR
select telefono, genero, id_padron from YPF_DataEnrich_Total
where compara_nombre is null and id_padron is not null and iif(genero='NULL',null,genero) is not null
OPEN C_ypf
FETCH NEXT FROM C_ypf INTO @tel, @genero, @idp
WHILE @@fetch_status = 0
BEGIN
set @total = @total + 1
if (ISNUMERIC(@tel) = 1)
begin
set @id_padron = null
set @cuit = null
set @nombre_persona = null
set @apellidos = null
set @nombres = null
set @habilitado = null
set @sexo = null
select top 1 @id_padron = pad.id_padron,
@cuit = pad.identificacion,
@nombre_persona = pad.nombre_persona,
@apellidos = pad.apellidos,
@nombres = pad.nombres,
@habilitado = pad.habilitado,
@sexo = pad.sexo
from padrones pad
where pad.habilitado = 1
and (pad.cuerpo_clave = @tel
or pad.cuerpo_clave = '0' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '00' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '000' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '0000' + LTRIM(rtrim(@tel))
)
and pad.id_padron != @idp
and (isnull(pad.sexo,'') = @genero
or (pad.identificacion like '20%' and @genero = 'M')
or (pad.identificacion like '27%' and @genero = 'F'))
if (@id_padron is not null)
begin
set @actualizados = @actualizados + 1
update YPF_DataEnrich_Total
set cuit = @cuit,
nombre_persona = @nombre_persona,
apellido_padron= @apellidos,
nombres_padron = @nombres,
habilitado = @habilitado,
sexo = @sexo,
id_padron = @id_padron
where YPF_DataEnrich_Total.Telefono = @tel
and id_padron = @idp
end
end
FETCH NEXT FROM C_ypf INTO @tel, @genero, @idp
END
CLOSE C_ypf
DEALLOCATE C_ypf
select @total as total, @actualizados as actualizados
end
END
/*
select * from YPF_DataEnrich_Total
where cuit is not null and compara_nombre is not null
*/
GO
GO
/*
exec [YPF] 3
@paso = 1 -- Completa con datos de padron buscando como cuerpo_clave el telefono
@paso = 2 -- Reemplazo los caractares raros en apellido y nombre
@paso = 3 -- Busca Apellido y Nombre enviado exactamente igual al del padron
@paso = 4 -- Busca Apellido igual al de Padron
@paso = 5 -- Busco los casos de apellidos con '
@paso = 6 -- De los que quedaron sin encontrar, vemos si hay otra persona con el mismo cuerpo_clave en padron
Despues del paso 6, volvemos a hacer los pasos 3 4 y 5 por si engancha ahora con la nueva persona
@paso = 7 -- De los que quedaron sin encontrar, busco en padron el del mismo sexo
Despues del paso 7, volvemos a hacer los pasos 3 4 y 5 por si engancha ahora con la nueva persona
*/
CREATE OR ALTER PROCEDURE dbo.YPF
@paso int = null
AS
BEGIN
SET NOCOUNT ON;
declare @tel varchar(150), @total int = 0, @actualizados int = 0
declare @id_padron int,
@cuit varchar(20),
@nombre_persona varchar(150),
@apellidos varchar(150),
@nombres varchar(150),
@habilitado bit,
@sexo char(1)
if (@paso = 1) -- Completa con datos de padron buscando como cuerpo_clave el telefono
begin
DECLARE C_ypf CURSOR FOR
select telefono from YPF_DataEnrich_Total
where cuit is null
OPEN C_ypf
FETCH NEXT FROM C_ypf INTO @tel
WHILE @@fetch_status = 0
BEGIN
set @total = @total + 1
if (ISNUMERIC(@tel) = 1)
begin
set @id_padron = null
set @cuit = null
set @nombre_persona = null
set @apellidos = null
set @nombres = null
set @habilitado = null
set @sexo = null
select top 1 @id_padron = pad.id_padron,
@cuit = pad.identificacion,
@nombre_persona = pad.nombre_persona,
@apellidos = pad.apellidos,
@nombres = pad.nombres,
@habilitado = pad.habilitado,
@sexo = pad.sexo
from padrones pad
where pad.habilitado = 1
and (pad.cuerpo_clave = @tel
or pad.cuerpo_clave = '0' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '00' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '000' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '0000' + LTRIM(rtrim(@tel))
)
if (@id_padron is not null)
begin
set @actualizados = @actualizados + 1
update YPF_DataEnrich_Total
set cuit = @cuit,
nombre_persona = @nombre_persona,
apellido_padron= @apellidos,
nombres_padron = @nombres,
habilitado = @habilitado,
sexo = @sexo,
id_padron = @id_padron
where YPF_DataEnrich_Total.Telefono = @tel
and YPF_DataEnrich_Total.cuit is null
end
end
FETCH NEXT FROM C_ypf INTO @tel
END
CLOSE C_ypf
DEALLOCATE C_ypf
select @total as total, @actualizados as actualizados
end
if (@paso = 2) -- Reemplazo los caractares raros
begin
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ñ','Ñ')
from YPF_DataEnrich_Total where Apellido like '%Ñ%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ñ','Ñ')
from YPF_DataEnrich_Total where Nombre like '%Ñ%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'ñ','ñ')
from YPF_DataEnrich_Total where Apellido like '%ñ%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'ñ','ñ')
from YPF_DataEnrich_Total where Nombre like '%ñ%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ó','O')
from YPF_DataEnrich_Total where Apellido like '%Ó%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ó','O')
from YPF_DataEnrich_Total where Nombre like '%Ó%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ã','I')
from YPF_DataEnrich_Total where Apellido like '%Ã%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ã','I')
from YPF_DataEnrich_Total where Nombre like '%Ã%'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'É','E')
from YPF_DataEnrich_Total where Apellido like '%É%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'É','E')
from YPF_DataEnrich_Total where Nombre like '%É'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'é','e')
from YPF_DataEnrich_Total where Apellido like '%é%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'é','e')
from YPF_DataEnrich_Total where Nombre like '%é‰'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ü','U')
from YPF_DataEnrich_Total where Apellido like '%Ü%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ü','U')
from YPF_DataEnrich_Total where Nombre like '%܉'
update YPF_DataEnrich_Total
set Apellido = replace(Apellido,'Ã’','O')
from YPF_DataEnrich_Total where Apellido like '%Ã’%'
update YPF_DataEnrich_Total
set Nombre = replace(Nombre,'Ã’','O')
from YPF_DataEnrich_Total where Nombre like '%Ã’%'
update YPF_DataEnrich_Total
set nombre_persona = replace(nombre_persona,'?','Ñ')
from YPF_DataEnrich_Total where nombre_persona like '%?%'
update YPF_DataEnrich_Total
set nombre_persona = replace(nombre_persona,'#','Ñ')
from YPF_DataEnrich_Total where nombre_persona like '%#%'
end
if (@paso = 3) -- Busca Apellido y Nombre enviado exactamente igual al del padron
begin
update YPF_DataEnrich_Total set compara_nombre = 'IGUAL'
where rtrim(ltrim(nombre_persona)) = rtrim(ltrim(Apellido)) + ' ' + rtrim(ltrim(Nombre))
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'IGUAL'
end
if (@paso = 4) -- Busca Apellido igual al de Padron
begin
update YPF_DataEnrich_Total set compara_nombre = 'APELLIDO IGUAL'
where rtrim(ltrim(nombre_persona)) LIKE rtrim(ltrim(Apellido))+'%'
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'APELLIDO IGUAL'
end
if (@paso = 5) -- Busco los casos de apellidos con '
begin
update YPF_DataEnrich_Total set compara_nombre = 'IGUAL'
where rtrim(ltrim(replace(nombre_persona,'''',''))) = rtrim(ltrim(Apellido)) + ' ' + rtrim(ltrim(Nombre))
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'IGUAL'
update YPF_DataEnrich_Total set compara_nombre = 'APELLIDO IGUAL'
where rtrim(ltrim(replace(nombre_persona,'''',''))) LIKE rtrim(ltrim(Apellido))+'%'
and cuit is not null and compara_nombre is null
select count(*) as iguales
from YPF_DataEnrich_Total
where compara_nombre = 'APELLIDO IGUAL'
end
declare @idp int
if (@paso = 6) -- De los que quedaron sin encontrar, vemos si hay otra persona con el mismo cuerpo_clave en padron
begin
DECLARE C_ypf CURSOR FOR
select telefono, id_padron from YPF_DataEnrich_Total
where compara_nombre is null and id_padron is not null
OPEN C_ypf
FETCH NEXT FROM C_ypf INTO @tel, @idp
WHILE @@fetch_status = 0
BEGIN
set @total = @total + 1
if (ISNUMERIC(@tel) = 1)
begin
set @id_padron = null
set @cuit = null
set @nombre_persona = null
set @apellidos = null
set @nombres = null
set @habilitado = null
set @sexo = null
select top 1 @id_padron = pad.id_padron,
@cuit = pad.identificacion,
@nombre_persona = pad.nombre_persona,
@apellidos = pad.apellidos,
@nombres = pad.nombres,
@habilitado = pad.habilitado,
@sexo = pad.sexo
from padrones pad
where pad.habilitado = 1
and (pad.cuerpo_clave = @tel
or pad.cuerpo_clave = '0' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '00' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '000' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '0000' + LTRIM(rtrim(@tel))
)
and pad.id_padron != @idp
if (@id_padron is not null)
begin
set @actualizados = @actualizados + 1
update YPF_DataEnrich_Total
set cuit = @cuit,
nombre_persona = @nombre_persona,
apellido_padron= @apellidos,
nombres_padron = @nombres,
habilitado = @habilitado,
sexo = @sexo,
id_padron = @id_padron
where YPF_DataEnrich_Total.Telefono = @tel
and id_padron = @idp
end
end
FETCH NEXT FROM C_ypf INTO @tel, @idp
END
CLOSE C_ypf
DEALLOCATE C_ypf
select @total as total, @actualizados as actualizados
end
if (@paso = 7) -- De los que quedaron sin encontrar, busco en padron el del mismo sexo
begin
declare @genero char(1)
DECLARE C_ypf CURSOR FOR
select telefono, genero, id_padron from YPF_DataEnrich_Total
where compara_nombre is null and id_padron is not null and iif(genero='NULL',null,genero) is not null
OPEN C_ypf
FETCH NEXT FROM C_ypf INTO @tel, @genero, @idp
WHILE @@fetch_status = 0
BEGIN
set @total = @total + 1
if (ISNUMERIC(@tel) = 1)
begin
set @id_padron = null
set @cuit = null
set @nombre_persona = null
set @apellidos = null
set @nombres = null
set @habilitado = null
set @sexo = null
select top 1 @id_padron = pad.id_padron,
@cuit = pad.identificacion,
@nombre_persona = pad.nombre_persona,
@apellidos = pad.apellidos,
@nombres = pad.nombres,
@habilitado = pad.habilitado,
@sexo = pad.sexo
from padrones pad
where pad.habilitado = 1
and (pad.cuerpo_clave = @tel
or pad.cuerpo_clave = '0' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '00' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '000' + LTRIM(rtrim(@tel))
or pad.cuerpo_clave = '0000' + LTRIM(rtrim(@tel))
)
and pad.id_padron != @idp
and (isnull(pad.sexo,'') = @genero
or (pad.identificacion like '20%' and @genero = 'M')
or (pad.identificacion like '27%' and @genero = 'F'))
if (@id_padron is not null)
begin
set @actualizados = @actualizados + 1
update YPF_DataEnrich_Total
set cuit = @cuit,
nombre_persona = @nombre_persona,
apellido_padron= @apellidos,
nombres_padron = @nombres,
habilitado = @habilitado,
sexo = @sexo,
id_padron = @id_padron
where YPF_DataEnrich_Total.Telefono = @tel
and id_padron = @idp
end
end
FETCH NEXT FROM C_ypf INTO @tel, @genero, @idp
END
CLOSE C_ypf
DEALLOCATE C_ypf
select @total as total, @actualizados as actualizados
end
END
/*
select * from YPF_DataEnrich_Total
where cuit is not null and compara_nombre is not null
*/
GO
Depends On
2Used By
No items found