dbo.sp_buscar_cuit

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@W_Nombre varchar 200
@W_NroDoc varchar 12
@nombre_contains varchar 8000

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/*


select * from padrones where  cuerpo_clave = '45499253'
        and habilitado = 1 
        and padrones.tipo_persona='F'


exec [sp_buscar_cuit] 'BONIFACIO GABIEL MIJAEL', '45499253', null
select * from padrones where cuerpo_clave = '00059144' or cuerpo_clave = '59144'
update padrones set cuerpo_clave = '59144' where id_padron = 24237210
SELECT *
  from padrones 
 where cuerpo_clave = '04520165'
 and habilitado = 1
 and identificador  in( select identificador from identificadores where identificadores.country_code = 'AR') 
*/

CREATE OR ALTER PROCEDURE dbo.sp_buscar_cuit
/* OJO!! SI SE CAMBIA LA SALIDA DE ESTE STORED, VER QUE STORED LO LLAMAN PARA CAMBIAR EL DISEÑO DE LA TABLA DONDE SE GUARDEN LOS RESULTADOS */
@W_Nombre VARCHAR(200),
@W_NroDoc VARCHAR(12),
@nombre_contains varchar(8000) = null
AS
BEGIN
    SET NOCOUNT ON;
 declare @cantidad int = 0
 declare @W_Nombre1 VARCHAR(200)
 declare @W_Cuit1   VARCHAR(12)
 declare @iguales   BIT
 declare @encontro  BIT
 declare @resultado BIT 
 declare @W_NroDoc2 VARCHAR(12)


 if(@W_NroDoc is not null and @W_NroDoc != '' and ISNUMERIC(@W_Nrodoc) = 1 )
 begin
 set @W_NroDoc2 = right('00000000'+@W_NroDoc,8);
 set @w_nrodoc = convert(int,@w_nrodoc)

--select @w_nrodoc

 set @cantidad = (SELECT COUNT(*)
    from padrones 
   where (cuerpo_clave = @W_Nrodoc or cuerpo_clave = @W_NroDoc2)
   and habilitado = 1
   and identificador  in 
   ('CUIL','CUIT'))
   --( select identificador from identificadores where identificadores.country_code = 'AR')) 

 if (@cantidad = 0) 
 begin
  select null as cuit, null as nombre
 end 

 if (@cantidad = 1) 
 begin
    SELECT identificacion as cuit, nombre_persona as nombre
     from padrones 
    where (cuerpo_clave = @W_Nrodoc or cuerpo_clave = @W_NroDoc2)
    and habilitado = 1
   and identificador  in
   ('CUIL','CUIT')
   --( select identificador from identificadores where identificadores.country_code = 'AR')
 end 

 set @encontro = 0

 if (@cantidad > 1 and @W_Nombre is not null) 
  begin
  set @encontro = (select count(*) from padrones where (cuerpo_clave = @W_Nrodoc or cuerpo_clave = @W_NroDoc2) and nombre_persona like @W_nombre + '%'
                   and habilitado = 1
                and identificador  in
       ('CUIL','CUIT'))
      --( select identificador from identificadores where identificadores.country_code = 'AR')) 
  if(@encontro = 1)
  begin
  select identificacion as cuit,nombre_persona as nombre from padrones where (cuerpo_clave = @W_Nrodoc or cuerpo_clave = @W_NroDoc2) and  nombre_persona like @W_nombre + '%'
      and habilitado = 1
   and identificador  in
       ('CUIL','CUIT')
      --( select identificador from identificadores where identificadores.country_code = 'AR')
  return
  end
 end  

 if (@cantidad > 1 and @nombre_contains is not null) 
  begin
  set @encontro = (select count(*) from padrones where (cuerpo_clave = @W_Nrodoc or cuerpo_clave = @W_NroDoc2) and contains(nombre_persona,@nombre_contains)
                and habilitado = 1
                and identificador  in
       ('CUIL','CUIT'))
      --( select identificador from identificadores where identificadores.country_code = 'AR')) 
  if(@encontro = 1)
  begin
  select identificacion as cuit,nombre_persona as nombre from padrones where (cuerpo_clave = @W_Nrodoc or cuerpo_clave = @W_NroDoc2) and contains(nombre_persona,@nombre_contains)
   and habilitado = 1
   and identificador  in
       ('CUIL','CUIT')
      --( select identificador from identificadores where identificadores.country_code = 'AR')
  return 
  end
  if(@encontro = 0)
  begin
   select null as cuit, null  as nombre
   return 
  end
  if(@encontro > 1)
  begin
   select null as cuit, null  as nombre
   return 

  end
  /*
   select null as cuit, null as nombre
   if (len(@W_nombre) >= 4) 
   begin
   DECLARE cursor1 CURSOR FOR
   SELECT identificacion, nombre_persona 
     from padrones 
    where cuerpo_clave = @W_Nrodoc
      and habilitado = 1
      and identificador = 'CUIT'
    OPEN cursor1 
    Fetch next from cursor1 INTO @W_Cuit1, @W_nombre1
    WHILE (@@FETCH_STATUS <> -1 )
   BEGIN
   exec @iguales = sp_compara_nombres @W_Nombre, @W_Nombre1
   if (@iguales = 1)
   begin
   set @encontro = 1
   select @W_Cuit1 as cuit, @W_Nombre1 as nombre
   break
   end 
   Fetch next from cursor1 INTO @W_Cuit1, @W_nombre1
   END 
   CLOSE cursor1
   DEALLOCATE cursor1 
   end 
   */

  if (@encontro = 0)
  begin
   select null as cuit, null  as nombre
   return 
  end 
 end 
 end
 else
 begin
  if (@nombre_contains is not null) 
  begin
  set @encontro = (select count(*) from padrones where contains(nombre_persona,@nombre_contains)  
      and habilitado = 1
      and identificador  in
       ('CUIL','CUIT'))
      --( select identificador from identificadores where identificadores.country_code = 'AR')) 
   if(@encontro = 1)
   begin
   select identificacion as cuit,nombre_persona as nombre from padrones where contains(nombre_persona,@nombre_contains)
      and habilitado = 1
      and identificador  in
       ('CUIL','CUIT')
      --( select identificador from identificadores where identificadores.country_code = 'AR')
   return 
   end
   if(@encontro = 0)
   begin
    select null as cuit, null  as nombre
    return 
   end
   if(@encontro > 1)
   begin
    select null as cuit, null  as nombre
    return 
   end
  end 
 end  
END 

GO

Depends On

1