SQL Server stored procedure result set retrieval

Objective: to retrieve the result set stored procedures performed by filtration

The following describes two common methods, but both need to affirm the table structure; do not know if there is an easier way, if a better way, please let me know.

The system stored procedure to sp_who2 example:

Method 1: Use a temporary table

 1 --1. 创建临时表
 2 CREATE TABLE #tmp(
 3         SPID int,
 4         Status nvarchar(50), 
 5         Login nvarchar(50), 
 6         HostName nvarchar(50), 
 7         BlkBy nvarchar(50), 
 8         DBName nvarchar(50), 
 9         Command nvarchar(50),
 10          CPUTime nvarchar ( 50 ), 
 . 11          diskio nvarchar ( 50 ), 
 12 is          LastBatch nvarchar ( 50 ), 
 13 is          the ProgramName nvarchar ( 50 ),
 14          SPID2 int , 
 15          requestID int 
16      )
 . 17  
18 is  - 2. execute a stored procedure and stored procedures that return result sets into a temporary table 
. 19  iNSERT  INTO #tmp Exec the sp_who2
 20 is 
21 is  - 3. retrieval, a query 
22 is  SELECT  *  from #tmp WHERE the Status =  ' Sleeping ' 
23 is  
24  - 4. Clear the temporary table 
25  the DROP  TABLE #tmp

Method 2: Table Variables

1 --1. 申明表变量
2 declare @tempTable Table(SPID int,Status nvarchar(50), Login nvarchar(50), HostName nvarchar(50), BlkBy nvarchar(50), DBName nvarchar(50), Command nvarchar(50),
3 CPUTime nvarchar(50), DiskIO nvarchar(50), LastBatch nvarchar(50), The ProgramName nvarchar ( 50 ), SPID2 int , requestID int )
 . 4  
. 5  - Returns 2. execute a stored procedure and stores the result of the process set into the table 
. 6  INSERT  INTO  @tempTable  Exec the sp_who2
 . 7  
. 8  - 3. retrieval query 
. 9  SELECT  *  from  @tempTable  WHERE the Status =  ' Sleeping '

 

Guess you like

Origin www.cnblogs.com/Juning/p/11268409.html