从游标到临时表的结果,其中包含行数和执行时间

时间:2018-12-03 12:25:47

标签: sql sql-server stored-procedures cursor temp-tables

需要使用游标从Employee表中获取100个ID,然后执行存储过程,并将每个ID的行数和执行时间放入一个临时表中。有些想法,如何计算存储过程将捕获的行数以及执行时间?

declare @temptable table
( ID nvarchar
 , numberOfRows int
 , executionTime int)

declare @id nvarchar(15)

declare db_cursor CURSOR FOR
select top 100 NationalIdNumber
from HumanResources.Employee

open db_cursor
fetch next from db_cursor into @id

while @@fetch_status = 0

begin

insert into @temptable

exec [dbo].[uspEmployeeData] @id

fetch next from db_cursor into @id

end

close db_cursor
deallocate db_cursor

我正在使用SQL Server 2014 Standard Edition

2 个答案:

答案 0 :(得分:1)

You can do something like:

declare @tableresults TABLE (@id INT, row_count INT, durationms int)

DECLARE @starttime DATETIME

open db_cursor
fetch next from db_cursor into @id

while @@fetch_status = 0

begin

    SET @starttime = GETDATE()

    insert into @temptable
    exec [dbo].[uspEmployeeData] @id

    INSERT INTO @tableresults
    VALUES (@id, @@rowcount, DATEDIFF(millisecond, @starttime, GETDATE() )

    fetch next from db_cursor into @id
end

答案 1 :(得分:0)

找到我的第一个Cursor查询。

注意:

我在这里使用Microsoft SQL Server 2012

我从临时表中获取ID。而您使用的是实际表格。

然后您询问临时表。但是我创建了实际的表。

您必须从CountAgainstId过程开始。

CREATE TABLE #Employee
(
 EmpID int
 , EmpName varchar (50) NOT NULL
 , Salary int NOT NULL
 , Address varchar (200) NOT NULL
)
GO
INSERT INTO #Employee(EmpID,EmpName,Salary,Address) VALUES(1,'Mohan',12000,'Noida')
, (1,'Mohan',12000,'Noida')
, (2,'Pavan',25000,'Delhi')
, (3,'Amit',22000,'Dehradun')
, (4,'Sonu',22000,'Noida'), (4,'Sonu',22000,'Noida')
, (5,'Deepak',28000,'Gurgaon'), (5,'Deepak',28000,'Gurgaon'), (5,'Deepak',28000,'Gurgaon')
GO 

-- I inserted same rows.

SELECT * FROM #Employee 

#Employee:

EmpID   EmpName     Salary      Address
1       Mohan       12000       Noida
1       Mohan       12000       Noida
2       Pavan       25000       Delhi
3       Amit        22000       Dehradun
4       Sonu        22000       Noida
4       Sonu        22000       Noida
5       Deepak      28000       Gurgaon
5       Deepak      28000       Gurgaon
5       Deepak      28000       Gurgaon

光标查询:

create procedure RowCounts (
 @EmpId int
 , @Nos int output 
 , @RunTime int output
)
as 
begin 
 declare @StartTime datetime
 , @EndTime datetime

 set @StartTime = (select getdate ())
 set @nos = (select COUNT (*) from #employee where EmpID = @EmpId)
 set @EndTime = (select GETDATE ())

 /*
   Do further, what do you want.
 */
 set @RunTime = DATEDIFF (MILLISECOND, @StartTime, @EndTime)
end

*****************************************

create procedure CountAgainstId as
begin

 declare @RowCount int
 , @RunTime int
 , @EmpId int
 , @i int = 0

 declare CountCursors cursor
 static for
  select empid from #employee
 open CountCursors

 fetch next from CountCursors into @empid

 if OBJECT_ID ('dbo.Summaries') is null -- object_id() is not recognised the temp tables
  begin 
   create table Summaries (Empid int, NoOfRow int, ExecutionTime int)
  end
  else 
  begin
   truncate table Summaries  
  end

 while @@FETCH_STATUS = 0
 begin 

  if not exists (
   select * from Summaries where empid = @EmpId  -- for removing the duplicate empid's
  )
  begin

   exec RowCounts @EmpId, @RowCount output, @RunTime output

   insert into Summaries 
   values (@EmpId, @RowCount, @RunTime)

  end

  fetch next from CountCursors into @empid
 end

 CLOSE CountCursors
 DEALLOCATE CountCursors

 select * from Summaries
end

摘要(输出表):

Empid   NoOfRow     ExecutionTime
1       2           0
2       1           0
3       1           0
4       2           0
5       3           0

让我知道,你得到了什么。