10

Let's say I have a stored procedure which returns a large set of data. Can I write another query to filter the result of stored procedure?

For example:

select * from
EXEC xp_readerrorlog
where LogDate = '2011-02-15'
Tim Post
  • 32,782
  • 15
  • 106
  • 168
Hammad Khan
  • 15,376
  • 14
  • 106
  • 131
  • 1
    possible duplicate of [Select columns from result set of stored procedure](http://stackoverflow.com/questions/209383/select-columns-from-result-set-of-stored-procedure) – Alex K. Feb 21 '12 at 13:39
  • I always use google site:stackoverflow.com select+from+stored+procedure – Alex K. Feb 21 '12 at 14:51

5 Answers5

17

You would need to first insert the results of the stored procedure on a table, and then query those results.

create table #result (LogDate datetime, ProcessInfo varchar(20),Text text)

INSERT INTO #Result
EXEC xp_readerrorlog

SELECT *
FROM #Result
WHERE datepart(yy,LogDate) = '2012'
Hammad Khan
  • 15,376
  • 14
  • 106
  • 131
Lamak
  • 67,466
  • 11
  • 101
  • 112
1

You can't make it part of a query, BUT you could insert the resulting data into a temp table or table variable and then use that for your query.

UnhandledExcepSean
  • 12,164
  • 2
  • 34
  • 49
1

Does returning the error log for just an entire day make the result any more useful? I think it will still be full of useless entries. If you're looking for specific events, why not use one of the filter parameters for xp_readerrorlog? The following wil return all rows in the current log that contain the string 'fail':

EXEC xp_readerrorlog 0, 1, 'fail';
Aaron Bertrand
  • 261,961
  • 36
  • 448
  • 471
0

You can copy output from sp to temporaty table.

insert into #temp
EXEC xp_readerrorlog

and then use where clause with the temp table

Vikram
  • 8,107
  • 30
  • 47
0

or you can make a Table-valued Function

Zyku
  • 1,399
  • 2
  • 22
  • 37