dbo.api_edictos_dni_cuitAB

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

5
Name Data Type Length Description
@p_Cuit varchar 20
@nombre varchar 1000
@nombre_contains varchar 1000
@maxqty int 4
@provincia varchar 100

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*

exec [api_edictos_dni_cuitAB] '5984260','',''

exec api_edictos_dni_cuitAB '','juan carlos gomez','',5

exec api_edictos_dni_cuitAB '20222222226','leonel turchetto','"LEONEL*" NEAR "TURCHETTO*"',20,'Salta'


*/

CREATE OR ALTER PROCEDURE dbo.api_edictos_dni_cuitAB
@p_Cuit varchar(20),
@nombre varchar(1000),
@nombre_contains varchar(1000),
@maxqty int = 20,
@provincia varchar(100)=null

AS
BEGIN
    SET NOCOUNT ON;
 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))

set @nombre = LTRIM(rtrim(@nombre))
if (isnull(@nombre,'') != '' AND 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 

SELECT @es_dni AS es_dni,@es_cuit AS es_cuit

if(@es_cuit=0 and @es_dni = 0)
begin
 begin
  select top (@maxqty) '' as mensaje_error, 
   0 as error, 
   1 as validacion, 'búsqueda por nombre' as mensaje_validacion,
   nombre_persona as nombre_correcto,
     identificacion as cuit_buscado
   from padrones 
   where habilitado = 1 
    and contains(nombre_persona,@nombre_contains) 
   and identificador in(select identificador from identificadores where country_code = 'AR')
 end
end


if (@es_cuit = 1) 
begin
 set @busqueda_igual = (select count(distinct nombre_persona) 
                         from padrones 
         where identificacion = @p_Cuit and habilitado = 1 
        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,
    nombre_persona as nombre_correcto,
      identificacion as cuit_buscado
    from padrones 
    where identificacion = @p_Cuit 
    and habilitado = 1 
    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 contains(nombre_persona,@nombre_contains) 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,
        nombre_persona as nombre_correcto,
          identificacion as cuit_buscado
        from padrones 
       where identificacion = @p_Cuit 
         and habilitado = 1 
          and contains(nombre_persona,@nombre_contains) 
         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 identificador in(select identificador from identificadores where country_code = 'AR'))
    if(@busqueda_no_contains = 0)
    begin
       select 'No se encontraron registros para ese CUIT' as mensaje_error,
               3 as error, 
         0 as validacion, '' as mensaje_validacion, 
         null as nombre_correcto, '' as cuit_buscado
    end
    else
    begin
       select distinct '' as mensaje_error, 
        0 as error, 
        0 as validacion,'Registro encontrado con ese CUIT con otro Nombre' as mensaje_validacion,
        nombre_persona as nombre_correcto,
        identificacion as cuit_buscado
        from padrones 
       where identificacion = @p_Cuit 
         and habilitado = 1 
          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 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,
        nombre_persona as nombre_correcto,
          identificacion as cuit_buscado
        from padrones 
       where cuerpo_clave = @p_Cuit 
         and habilitado = 1   
          and identificador in(select identificador from identificadores where country_code = 'AR')
 end
 else 
 begin 
  if(@busqueda_igual = 0)
  begin
   begin
    select '' as mensaje_error, 
            3 as error, 
      0 as validacion, 'No se encontraron registros para ese DNI' as mensaje_validacion,
      null as nombre_correcto,
      '' as cuit_buscado
   end
  end 
  else
  begin
    select distinct '' as mensaje_error, 
    0 as error, 
    0 as validacion,'Registros encontrado con ese DNI 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 identificador in(select identificador from identificadores where country_code = 'AR')
  end
 end 
 
 
end


END 

GO

Used By

No items found