dbo.api_busqueda_padron_new

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@p_identificador varchar 100
@max_rows int 4
@type char 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: Leonel Turchetto
-- Create date: 2018-03-12
-- Description: Búsqueda de Padron
-- =============================================
create OR ALTER PROCEDURE dbo.api_busqueda_padron_new
@p_identificador varchar(100),
@max_rows int,
@type char(1)
AS
BEGIN
 SET NOCOUNT ON;
  declare @tax_id varchar(20),
          @name   varchar(200),
          @typep   varchar(20)
  declare @domicilios table (id int, domicilio varchar(200), pai_nombre varchar(20), prv_nombre varchar(100), loc_nombre varchar(100), codpos varchar(10), tipo_domicilio varchar(10), created datetime)
  declare @resultado  table (tax_id varchar(20), name varchar(200), type varchar(20), address varchar(200), city varchar(200), state varchar(200))
  
 if(@type != '')
 begin

   DECLARE C_padron CURSOR FOR
   select distinct top(@max_rows) identificacion as tax_id,
           nombre_persona as name,
           iif(tipo_persona='F','PERSON','COMPANY') as type
   from dbo.padrones
   inner join identificadores on identificadores.identificador = padrones.identificador
   WHERE CONTAINS((nombre_persona,identificacion,cuerpo_clave),@p_identificador) 
            and country_code in ('AR') 
            and habilitado = 1
          and tipo_persona = @type
    
 end
 else
 begin
      DECLARE C_padron CURSOR FOR
   SELECT distinct top(@max_rows) identificacion as tax_id,
             nombre_persona as name,
             iif(tipo_persona='F','PERSON','COMPANY') as type
    from dbo.padrones
    inner join identificadores on identificadores.identificador = padrones.identificador
   WHERE CONTAINS((nombre_persona,identificacion,cuerpo_clave),@p_identificador) 
        and country_code in ('AR') 
        and habilitado = 1
 end
 OPEN C_Padron
 FETCH NEXT FROM C_Padron INTO @tax_id, @name, @typep      
 WHILE @@fetch_status = 0
  BEGIN 
        insert into @domicilios
       exec sp_obtener_domicilios @tax_id    
        
        if ((select count(*) from @domicilios where tipo_domicilio = 'D') = 0)
        begin 
           insert into @resultado values (@tax_id,@name,@typep, null, null, null)
        end 
        else
        begin
           insert into @resultado 
           select @tax_id,@name,@typep,
                  domicilio, 
                  loc_nombre,
                  prv_nombre
                  from @domicilios
                  where tipo_domicilio = 'D'
        end 
        delete from @domicilios
        FETCH NEXT FROM C_Padron INTO @tax_id, @name, @typep 
   END 
  CLOSE C_Padron
  DEALLOCATE C_Padron   

    SELECT distinct top(@max_rows) tax_id,
           name,
           type, 
           address,
           city,
           state,
           'Argentina' as country
           from @resultado

 
END
GO

Used By

No items found