dbo.sp_falabella_completa_cuit

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

/*
exec [sp_falabella_completa_cuit]
exec [sp_buscar_cuit] 'COLEANDRO CONCERCION', '4268329', 'COLEANDRO* near *CONCERCION'
select * from padrones where cuerpo_clave = '06719199' 
*/

CREATE OR ALTER PROCEDURE dbo.sp_falabella_completa_cuit

AS
BEGIN
    SET NOCOUNT ON;
 DECLARE @idFalabella int
 DECLARE @dni varchar(8)
 DECLARE @nombre varchar(200)
 DECLARE @nombre2 varchar(200)
 DECLARE @encontados table (cuit varchar(20), nombre varchar(200))   
 DECLARE @cuit_encontrado varchar(20)
 DECLARE @nombre_encontrado varchar(200)
 DECLARE @cuerpo_clave varchar(8) 
 DECLARE @id_padron int

 DECLARE C_cursor CURSOR FOR
 select idFalabella, dni8, nombre
   from falabella2021 
   where validado is null
   --and dni = '4268329'
   and len(dni8) = 8 
   --and idfalabella = 2371293

 OPEN C_cursor
 FETCH NEXT FROM C_cursor INTO @idFalabella, @dni, @nombre
 WHILE @@fetch_status = 0
 BEGIN 
  set @nombre2 = ltrim(rtrim(@nombre))
  set @nombre2 = REPLACE(@nombre2, ' ', '* near *') 
  --set @nombre2 =  '"' + @nombre2 + '"'
  select @nombre, @dni, @nombre2 
  delete from @encontados 
  insert into @encontados 
   exec [sp_buscar_cuit] @nombre, @dni, @nombre2 
  set @cuit_encontrado = null 
  select @cuit_encontrado = cuit, @nombre_encontrado = nombre from @encontados 

  if @cuit_encontrado is not null 
  begin 
   select @id_padron = id_padron, @cuerpo_clave = cuerpo_clave 
   from padrones 
   where identificacion = @cuit_encontrado 
     and habilitado = 1 
     and nombre_persona = @nombre_encontrado
  
   update falabella2021 set identificacion = @cuit_encontrado, validado = 1, 
          nombrePadron = @nombre_encontrado, cuerpo_clave = @cuerpo_clave, id_persona = @id_padron 
    where idFalabella = @idFalabella
  end 
  else 
  begin 
  
   set @nombre2 = SUBSTRING(@nombre, 1, 2) 
   --select @nombre2 
   
   delete from @encontados 
   if (select COUNT(*) from padrones 
        where cuerpo_clave = @dni and SUBSTRING(nombre_persona, 1, 2) = @nombre2 and habilitado = 1) = 1 
   begin 
    insert into @encontados 
    select identificacion, nombre_persona from padrones 
    where cuerpo_clave = @dni and SUBSTRING(nombre_persona, 1, 2) = @nombre2 and habilitado = 1
    set @cuit_encontrado = null 
    select @cuit_encontrado = cuit, @nombre_encontrado = nombre from @encontados 

    if @cuit_encontrado is not null 
    begin 
     select @id_padron = id_padron, @cuerpo_clave = cuerpo_clave 
     from padrones 
     where identificacion = @cuit_encontrado 
       and habilitado = 1 
       and nombre_persona = @nombre_encontrado
  
     update falabella2021 set identificacion = @cuit_encontrado, validado = 0,  
         nombrePadron = @nombre_encontrado, cuerpo_clave = @cuerpo_clave, id_persona = @id_padron 
      where idFalabella = @idFalabella
    end
   end  
   
  end 

  FETCH NEXT FROM C_cursor INTO @idFalabella, @dni, @nombre
 END 
 CLOSE C_cursor
 DEALLOCATE C_cursor
END
GO

Used By

No items found