dbo.obtener_vinculaciones_indirectas

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@p_cuit varchar 20

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*


exec obtener_vinculaciones_indirectas '20248728745'

*/


CREATE OR ALTER PROCEDURE dbo.obtener_vinculaciones_indirectas
@p_cuit varchar(20)
AS
BEGIN
    SET NOCOUNT ON;
 
 declare @r_cuit_vinculado varchar(20)
 declare @o_cuit_vinculado varchar(20)
 declare @cuit_vinculado varchar(20),@nombre_persona varchar(200),@detalle varchar(max)
 declare @respuesta table(cuit_vinculada varchar(20),nombre_persona varchar(200),detalle varchar(max))
 
 declare cur_vinculaciones
 cursor for

  select cuit_vinculada 
   from vinculaciones right join tipo_vinculaciones on
 id_tipo_vinculacion = cod_vinculacion INNER JOIN PADRONES ON PADRONES.identificacion = cuit_vinculada where cuit_sociedad = @p_cuit  and visible = 1 
 and id_tipo_vinculacion   in (3,4)
 union all
 select cuit_sociedad as cuit_vinculada
   from vinculaciones right join tipo_vinculaciones on
 id_tipo_vinculacion = cod_vinculacion INNER JOIN PADRONES ON PADRONES.identificacion = cuit_sociedad where cuit_vinculada = @p_cuit  and visible = 1 
 and id_tipo_vinculacion   in (3,4)
 

 open cur_vinculaciones
 fetch next from cur_vinculaciones
 into @r_cuit_vinculado


 WHILE(@@FETCH_STATUS <> -1)
 BEGIN
  declare cur_indirectas
  cursor for
   select  cuit_vinculada as cuit_vinculada,nombre_persona,vinculaciones.detalle from vinculaciones right join tipo_vinculaciones on
   id_tipo_vinculacion = cod_vinculacion INNER JOIN PADRONES ON PADRONES.identificacion = cuit_vinculada where cuit_vinculada != @p_cuit and cuit_sociedad = @r_cuit_vinculado  and visible = 1 
   and id_tipo_vinculacion   in (3,4)
   union all
   select cuit_sociedad as cuit_vinculada,nombre_persona,
    detalle from vinculaciones right join tipo_vinculaciones on
   id_tipo_vinculacion = cod_vinculacion INNER JOIN PADRONES ON PADRONES.identificacion = cuit_sociedad where cuit_sociedad != @p_cuit and cuit_vinculada = @r_cuit_vinculado  and visible = 1 
   and id_tipo_vinculacion   in (3,4)

  open cur_indirectas
  fetch next from cur_indirectas into @cuit_vinculado,@nombre_persona,@detalle
  WHILE(@@FETCH_STATUS <> -1)
  BEGIN
   insert into @respuesta values(@cuit_vinculado,@nombre_persona,@detalle)
   fetch next from cur_indirectas into @cuit_vinculado,@nombre_persona,@detalle
  END
  close cur_indirectas
  DEALLOCATE cur_indirectas
 fetch next from cur_vinculaciones into @r_cuit_vinculado
 END
 close cur_vinculaciones
 DEALLOCATE cur_vinculaciones
 SELECT distinct cuit_vinculada,nombre_persona FROM @respuesta
END 

GO

Used By

No items found