
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sel_consultas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sel_consultas]
GO


CREATE PROCEDURE [dbo].[sel_consultas]
 @p_filtros as varchar (8000) = '1=1',
 @p_orde as varchar (8000) = 'id',
 @p_tabla as varchar (8000) = '*',
 @p_campo  as varchar (8000) ='*'
  AS
 
BEGIN
SET LANGUAGE ENGLISH ;
SET DATEFORMAT DMY ;

declare @nombreSPConsulta varchar(100)
declare @SqlString nvarchar(2000)
declare @ParamDef nvarchar(2000)
set @nombreSPConsulta =( SELECT top 1  ROUTINE_NAME 
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_NAME  LIKE 'sel_'+@p_tabla +'%'  order by len (ROUTINE_NAME) asc)

set @SqlString = N'exec '+@nombreSPConsulta+' @p_filtro, @p_orden, @p_campos'
set @ParamDef = N'@p_filtro varchar (8000), @p_orden varchar (8000), @p_campos varchar (8000)'
EXECUTE sp_executesql @SqlString ,@ParamDef, @p_filtro=@p_filtros  , @p_orden=@p_orde , @p_campos =@p_campo

END

GO
