CREATE PROCEDURE Pa_Query 
(@pmCampos VARCHAR(1500),
 @pmTabla VARCHAR(1500),
@pmCriterio VARCHAR(2000),
@pmOrden VARCHAR(200),
@pmGroup VARCHAR(500),
@pmHaving VARCHAR(100))

AS

BEGIN
 Declare @pmQry VARCHAR(5800)
 SET @pmQry=''
 SET @pmQry=@pmQry + 'SELECT '+ @pmCampos + '  FROM ' + @pmTabla
 IF Len(@pmCriterio)>0
 BEGIN
  SET @pmQry=@pmQry + '  WHERE '+ @pmCriterio
 END
 IF Len(@pmGroup)>0 
 BEGIN
  SET @pmQry=@pmQry + ' GROUP BY ' + @pmGroup
 END
 IF Len(@pmOrden)>0 
 BEGIN
  SET @pmQry=@pmQry + ' ORDER BY '+ @pmOrden
 END
 IF Len(@pmHaving)>0 
 BEGIN
  SET @pmQry=@pmQry + ' HAVING '+ @pmHaving
 END
EXECUTE  (@pmQry)

RETURN
END

GO
