Sql 从游标到带有行数和执行时间的临时表的结果
需要使用游标从Employee表中获取100个ID,然后执行存储过程,并将每个ID的行数和执行时间放入一个临时表中。一些想法,如何计算存储过程将捕获的行数和执行时间Sql 从游标到带有行数和执行时间的临时表的结果,sql,sql-server,stored-procedures,cursor,temp-tables,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 NationalIdNumbe
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标准版您可以执行以下操作:
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
查找我的第一个
光标
查询
注意:
这里我使用的是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
#员工:
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 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
摘要(输出表):
让我知道,您得到了什么。向调用的proc添加一个
输出
参数以返回所需的行数,可以根据需要在外部脚本中使用,您不一次插入100吗?为什么使用光标?如果停止使用光标进行插入,时间将显著减少。但说真的,我不知道你想在这里做什么。你必须先插入到诱惑中,然后再插入到tableresults中,明白吗?