dbo.procesos_masivos_insertar

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

6
Name Data Type Length Description
@p_cliente int 4
@p_cuit varchar 20
@p_dni varchar 20
@p_nombre varchar 180
@p_fecha varchar 20
@p_user_id int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
--delete from procesos_masivos 
--select * from procesos_masivos 
/*
exec [procesos_masivos_insertar] 2254875, '', '4520165', 'TENEMBAUM ENRIQU','',94437923
*/

CREATE OR ALTER PROCEDURE dbo.procesos_masivos_insertar
@p_cliente int, -- mandamos cliente = 1 cuando NO QUEREMOS CARGARLO EN consultar_afip
@p_cuit varchar(20),
@p_dni varchar(20), 
@p_nombre varchar(180),
@p_fecha varchar(20),
@p_user_id int

AS

BEGIN
BEGIN TRY

    SET NOCOUNT ON;

 DECLARE @fecha date = null 
 declare @cuits table (cuit varchar(20), nombre varchar(200))

 if (@p_cuit   = '') set @p_cuit = null 
 if (@p_dni    = '') set @p_dni = null 
 if (@p_nombre = '') set @p_nombre = null 
 if (@p_fecha  = '') set @p_fecha = null 
 --if (@p_fecha  is not null) set @fecha = CONVERT(date, @p_fecha) 
 set @p_dni = ltrim(rtrim(@p_dni))
 if (@p_cuit is null)
 begin
  delete from @cuits
  insert into @cuits exec sp_buscar_cuit null, @p_dni, null 

  if (select COUNT(*) from @cuits) = 1
  begin 
   select @p_cuit = cuit from @cuits
  end
 end 

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

 if (@p_cuit is null)
 begin
  declare @palabra_1 table (palabra varchar(30), id int identity(1,1))
  declare @palabra varchar(100) = null 
  insert into @palabra_1
   exec sp_separo_nombres @p_nombre 
  declare @total int = (select MAX(id) from @palabra_1)
  declare @i int = 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] @p_nombre, @p_dni, @palabra 
    if (select COUNT(*) from @cuits) = 1
    begin 
     select @p_cuit = cuit from @cuits
    end
   end 
   set @i = @i + 1 
  end
 end 
 declare @es_cuit bit = 0 
 if (@p_cuit is not null and LEN(@p_cuit) = 11) set @es_cuit = 1  


 INSERT INTO procesos_masivos (cliente, fecha_proceso, cuit, dni, nombre, fecha, created, created_by, es_cuit) 
 VALUES (@p_cliente, convert(date, getdate()), @p_cuit, @p_dni, @p_nombre, @p_fecha, getdate(), @p_user_id, @es_cuit) 

 if (@es_cuit = 1 and @p_cliente != 1 )
 begin
 insert into consultar_afip(proceso, fecha, procesado, cuit,prioridad) values ('PROCESOS_MASIVOS', CONVERT(date,getdate()), 0, @p_cuit,1)   
 end
 select @p_cuit as cuit

END TRY
BEGIN CATCH
 SELECT ERROR_NUMBER() as error
END CATCH
END
/*
select cuit from consultar_afip where proceso = 'PROCESOS_MASIVOS' and fecha = '2021-10-12'
*/


GO

Used By

No items found