dbo.completar_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
/*
select count(*) from completar_cuit where cuit is  null and procesado = 1
select count(*) from completar_cuit where cuit is not null and procesado = 1
select count(*) from completar_cuit where procesado = 0


--update completar_cuit 
--set completar_cuit.cuit = identificacion, completar_cuit.nombre_padron=nombre_persona, procesado = 1
-- from completar_cuit 
select * from completar_cuit 
inner join padrones on (cuerpo_clave  = format(nrodoc,'########') and habilitado = 1)
where nrodoc > 10000000 and completar_cuit.cuit is null and identificacion is not null 

 update completar_cuit set procesado = 0 
 where procesado = 1 and cuit is null 

*/


CREATE OR ALTER PROCEDURE dbo.completar_cuit_

AS

BEGIN

    SET NOCOUNT ON;
 declare @dni bigint 
 declare @nombre varchar(500) 
 declare @id int 
 declare @p_cuit varchar(20)
 declare @p_nombre_padron varchar(500)


 declare @palabra_1 table (palabra varchar(30), id int identity(1,1))
 declare @palabra varchar(100)
 declare @total int 
 declare @i int



 declare @cuits table (cuit varchar(20), nombre varchar(200))

 DECLARE C_padron CURSOR FOR
  select top 1000 id,
      nombre, 
      nrodoc 
    from completar_cuit 
   where procesado = 0 
    order by id 

 OPEN C_Padron
 FETCH NEXT FROM C_Padron INTO  @id, @nombre, @dni
 WHILE @@fetch_status = 0
 BEGIN 

  set @p_cuit = null 
  set @p_nombre_padron = null 

  set @dni = ltrim(rtrim(@dni))
  if (@p_cuit is null)
  begin
   delete from @cuits
   insert into @cuits exec sp_buscar_cuit null, @dni, null 
   if (select COUNT(*) from @cuits) = 1
   begin 
    select @p_cuit = cuit, @p_nombre_padron = nombre from @cuits
   end
  end 

  if (@p_cuit is null)
  begin
   delete from @cuits
   insert into @cuits 
   exec [sp_buscar_cuit] @nombre, @dni, null 
   if (select COUNT(*) from @cuits) = 1
   begin 
    select @p_cuit = cuit, @p_nombre_padron = nombre  from @cuits
   end
  end 

  if (@p_cuit is null)
  begin
   set @palabra= null 
   insert into @palabra_1
    exec sp_separo_nombres @nombre 
   set @total  = (select MAX(id) from @palabra_1)
   set @i = 1 
   while (@i <= @total and @p_cuit is null) 
   begin
    set @palabra = null 
    select @palabra = palabra from @palabra_1 where id = @i
    if (@palabra is not null)
    begin
     set @palabra = '"' + @palabra + '"'
     delete from @cuits
     insert into @cuits 
     exec [sp_buscar_cuit] @nombre, @dni, @palabra 
     if (select COUNT(*) from @cuits) = 1
     begin 
      select @p_cuit = cuit, @p_nombre_padron = nombre  from @cuits
     end
    end 
    set @i = @i + 1 
   end
  end 
  if (@p_cuit is not null and LEN(@p_cuit) = 11) 
  begin
  update completar_cuit set cuit = @p_cuit, nombre_padron =  @p_nombre_padron  where id = @id 
  end

  update completar_cuit set procesado = 1 where id = @id

  FETCH NEXT FROM C_Padron INTO  @id, @nombre, @dni

 END 
 CLOSE C_Padron
 DEALLOCATE C_Padron


END

GO

Used By

No items found