Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
3| Name | Data Type | Length | Description |
|---|---|---|---|
| @p_Cuit | varchar | 20 | |
| @nombre | varchar | 1000 | |
| @nombre_contains | varchar | 1000 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [api_validacion_dni_cuit] '20000591441','PUGLIESE JOSE', '"PUBLIESE*" NEAR "JOSE*"'
exec [api_validacion_dni_cuit] '59144','pppp', '"pppp*"'
El 19/07/2021 se le sacaron los top 1
*/
CREATE OR ALTER PROCEDURE dbo.api_validacion_dni_cuit
@p_Cuit varchar(20),
@nombre varchar(1000),
@nombre_contains varchar(1000)
AS
BEGIN
SET NOCOUNT ON;
declare @apellido varchar(1000)
if (select CHARINDEX(' ',@nombre)) > 0
set @apellido = SUBSTRING(@nombre,1,(select CHARINDEX(' ',@nombre))-1)
else
set @apellido = @nombre
declare @es_cuit bit = 0
declare @es_dni bit = 0
declare @error bit = 0
declare @busqueda_igual int, @busqueda_contains int, @busqueda_no_contains int, @nombre_encontrado varchar(1000), @cuit_buscado varchar(20)
set @p_cuit = LTRIM(rtrim(@p_cuit))
if (LEN(@p_cuit) = 0)
begin
set @error = 1
select 'Debe informar un CUIT/DNI para poder realizar la consulta' as mensaje_error,
1 as error,
0 as validacion, '' as mensaje_validacion,
'' as nombre_correcto,
'' as cuit_buscado
end
set @nombre = LTRIM(rtrim(@nombre))
if (LEN(@nombre) < 3)
begin
set @error = 1
select 'Debe informar un nombre que contenga almenos 3 caracteres para poder realizar la consulta' as mensaje_error,
2 as error,
0 as validacion, '' as mensaje_validacion,
'' as nombre_correcto,
'' as cuit_buscado
end
if (LEN(@p_Cuit) = 11)
begin
set @es_cuit = 1
end
if (LEN(@p_cuit) >= 4 and LEN(@p_Cuit) <= 8)
begin
set @es_dni = 1
set @p_Cuit = right('00000000'+@p_Cuit,8);
end
if (@es_cuit = 0 and @es_dni = 0)
begin
set @error = 1
select 'El tamaño del CUIT/DNI enviado es inválido' as mensaje_error,
4 as error,
0 as validacion, '' as mensaje_validacion,
'' as nombre_correcto,
'' as cuit_buscado
end
if (@es_cuit = 1)
begin
set @busqueda_igual = (select count(distinct nombre_persona)
from padrones
where identificacion = @p_Cuit and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR'))
if( @busqueda_igual = 1)
begin
select /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion,'El registro es exactamente igual' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR')
end
else
begin
if(@busqueda_igual = 0)
begin
set @busqueda_contains =
(select count(distinct nombre_persona)
from padrones where identificacion = @p_Cuit and habilitado = 1
and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_contains = 1)
begin
select /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion, 'El registro coincide pero con una o más variantes en su nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR')
end
else
begin
set @busqueda_no_contains = (select count(distinct nombre_persona) from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_no_contains = 0)
begin
select 'No se encontraron registros para ese CUIT/DNI' as mensaje_error,
3 as error,
0 as validacion, '' as mensaje_validacion,
null as nombre_correcto, '' as cuit_buscado
end
else
begin
select distinct /*top 1*/ '' as mensaje_error,
0 as error,
0 as validacion,'Registro encontrado con ese DNI/CUIT con otro Nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR')
end
end
end
end
end
if (@es_dni = 1)
begin
set @busqueda_igual = (select count(distinct nombre_persona) from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR'))
if( @busqueda_igual = 1)
begin
select /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion, 'El registro es exactamente igual' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR')
end
else
begin
if(@busqueda_igual = 0)
begin
set @busqueda_contains = (select count(distinct nombre_persona) from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_contains = 1)
begin
select distinct /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion, 'El registro coincide pero con una o más variantes en su nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR')
/*union
select distinct '' as mensaje_error,
0 as error,
0 as validacion,'Registro encontrado con ese DNI/CUIT con otro Nombre' as mensaje_validacion,
nombre_persona as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificacion not in
(
select
identificacion
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR')
)
and identificador in(select identificador from identificadores where country_code = 'AR')*/
end
else
begin
set @busqueda_no_contains = (select count(distinct nombre_persona) from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_no_contains = 0)
begin
select '' as mensaje_error,
3 as error,
0 as validacion, 'No se encontraron registros para ese CUIT/DNI' as mensaje_validacion,
null as nombre_correcto,
'' as cuit_buscado
end
else
begin
select distinct /*top 1*/ '' as mensaje_error,
0 as error,
0 as validacion,'Registro encontrado con ese DNI/CUIT con otro Nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR')
end
end
end
end
end
END
GO
GO
/*
exec [api_validacion_dni_cuit] '20000591441','PUGLIESE JOSE', '"PUBLIESE*" NEAR "JOSE*"'
exec [api_validacion_dni_cuit] '59144','pppp', '"pppp*"'
El 19/07/2021 se le sacaron los top 1
*/
CREATE OR ALTER PROCEDURE dbo.api_validacion_dni_cuit
@p_Cuit varchar(20),
@nombre varchar(1000),
@nombre_contains varchar(1000)
AS
BEGIN
SET NOCOUNT ON;
declare @apellido varchar(1000)
if (select CHARINDEX(' ',@nombre)) > 0
set @apellido = SUBSTRING(@nombre,1,(select CHARINDEX(' ',@nombre))-1)
else
set @apellido = @nombre
declare @es_cuit bit = 0
declare @es_dni bit = 0
declare @error bit = 0
declare @busqueda_igual int, @busqueda_contains int, @busqueda_no_contains int, @nombre_encontrado varchar(1000), @cuit_buscado varchar(20)
set @p_cuit = LTRIM(rtrim(@p_cuit))
if (LEN(@p_cuit) = 0)
begin
set @error = 1
select 'Debe informar un CUIT/DNI para poder realizar la consulta' as mensaje_error,
1 as error,
0 as validacion, '' as mensaje_validacion,
'' as nombre_correcto,
'' as cuit_buscado
end
set @nombre = LTRIM(rtrim(@nombre))
if (LEN(@nombre) < 3)
begin
set @error = 1
select 'Debe informar un nombre que contenga almenos 3 caracteres para poder realizar la consulta' as mensaje_error,
2 as error,
0 as validacion, '' as mensaje_validacion,
'' as nombre_correcto,
'' as cuit_buscado
end
if (LEN(@p_Cuit) = 11)
begin
set @es_cuit = 1
end
if (LEN(@p_cuit) >= 4 and LEN(@p_Cuit) <= 8)
begin
set @es_dni = 1
set @p_Cuit = right('00000000'+@p_Cuit,8);
end
if (@es_cuit = 0 and @es_dni = 0)
begin
set @error = 1
select 'El tamaño del CUIT/DNI enviado es inválido' as mensaje_error,
4 as error,
0 as validacion, '' as mensaje_validacion,
'' as nombre_correcto,
'' as cuit_buscado
end
if (@es_cuit = 1)
begin
set @busqueda_igual = (select count(distinct nombre_persona)
from padrones
where identificacion = @p_Cuit and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR'))
if( @busqueda_igual = 1)
begin
select /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion,'El registro es exactamente igual' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR')
end
else
begin
if(@busqueda_igual = 0)
begin
set @busqueda_contains =
(select count(distinct nombre_persona)
from padrones where identificacion = @p_Cuit and habilitado = 1
and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_contains = 1)
begin
select /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion, 'El registro coincide pero con una o más variantes en su nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR')
end
else
begin
set @busqueda_no_contains = (select count(distinct nombre_persona) from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_no_contains = 0)
begin
select 'No se encontraron registros para ese CUIT/DNI' as mensaje_error,
3 as error,
0 as validacion, '' as mensaje_validacion,
null as nombre_correcto, '' as cuit_buscado
end
else
begin
select distinct /*top 1*/ '' as mensaje_error,
0 as error,
0 as validacion,'Registro encontrado con ese DNI/CUIT con otro Nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where identificacion = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR')
end
end
end
end
end
if (@es_dni = 1)
begin
set @busqueda_igual = (select count(distinct nombre_persona) from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR'))
if( @busqueda_igual = 1)
begin
select /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion, 'El registro es exactamente igual' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and nombre_persona = @nombre
and identificador in(select identificador from identificadores where country_code = 'AR')
end
else
begin
if(@busqueda_igual = 0)
begin
set @busqueda_contains = (select count(distinct nombre_persona) from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_contains = 1)
begin
select distinct /*top 1*/ '' as mensaje_error,
0 as error,
1 as validacion, 'El registro coincide pero con una o más variantes en su nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR')
/*union
select distinct '' as mensaje_error,
0 as error,
0 as validacion,'Registro encontrado con ese DNI/CUIT con otro Nombre' as mensaje_validacion,
nombre_persona as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificacion not in
(
select
identificacion
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and (contains(nombre_persona,@nombre_contains) or contains(nombre_persona,@apellido))
and identificador in(select identificador from identificadores where country_code = 'AR')
)
and identificador in(select identificador from identificadores where country_code = 'AR')*/
end
else
begin
set @busqueda_no_contains = (select count(distinct nombre_persona) from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR'))
if(@busqueda_no_contains = 0)
begin
select '' as mensaje_error,
3 as error,
0 as validacion, 'No se encontraron registros para ese CUIT/DNI' as mensaje_validacion,
null as nombre_correcto,
'' as cuit_buscado
end
else
begin
select distinct /*top 1*/ '' as mensaje_error,
0 as error,
0 as validacion,'Registro encontrado con ese DNI/CUIT con otro Nombre' as mensaje_validacion,
upper(nombre_persona) as nombre_correcto,
identificacion as cuit_buscado
from padrones
where cuerpo_clave = @p_Cuit
and habilitado = 1 and tipo_persona != 'J'
and identificador in(select identificador from identificadores where country_code = 'AR')
end
end
end
end
end
END
GO
Depends On
2Used By
No items found