IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[a_sel_valoresXDefectoAtributo]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[a_sel_valoresXDefectoAtributo]
GO
CREATE PROCEDURE  [dbo].[a_sel_valoresXDefectoAtributo]	
@p_idSubtipoDocumento as int
as begin

select distinct 
atri.id
,atri.atributo
,atri.id_tipoAtributo
,tip.tipoAtributo
,subca.subtipoDocumento_atributo
,opcion.opcionAtributo
from doc_subtipoDocumento_atributo subca
inner join cat_atributo atri on subca.id_atributo=atri.id and subca.active=1
inner join equ_tipoAtributo tip on tip.id=atri.id_tipoAtributo
left join equ_opcionAtributo opcion on opcion.id_atributo=atri.id  and opcion.active=1
AND (ISNUMERIC(subca.subtipoDocumento_atributo)=1 
AND  cast(opcion.id as varchar(20))=subca.subtipoDocumento_atributo)
where subca.id_subtipoDocumento=@p_idSubtipoDocumento
END 