我正在尝试从存储过程中删除游标。
Declare cur_Acc_Map cursor local for
Select Group_No from Account_Group
Where Company_No in (0,@Company_No) and Group_No <> 14
and Parent_Group_No = @Parent_Id
order by Group_Name
open cur_Acc_Map
fetch next from cur_Acc_Map into @Group_No
while @@fetch_status <> -1
begin
exec SP_Account_Map_Process @Company_No, @UserName, @HostName, @Sequence, @Group_No, @Level_Id,@Acc_year,@From_date,@To_date,@DispMode
fetch next from cur_Acc_Map into @Group_No
End
close cur_Acc_Map
deallocate cur_Acc_Map
在这种情况下如何使用游标。
此查询的执行时间为7秒,这太多了,有时需要更多。如何有效地利用这里的连接或者我应该逐个手动迭代获取的记录,这将有所帮助
完整程序如下:
ALTER Procedure [dbo].[SP_Account_Map_Process]
@Company_No int,
@UserName varchar(50),
@HostName varchar(50),
@Sequence int,
@Parent_Id int = 0,
@Level_Id int = 0,
@Acc_year int=0,
@From_date datetime= null,
@To_date datetime =null,
@DispMode int=0
As
Begin
if @Level_Id = 0
Begin
Set @Parent_Id = 0
End
Set @Level_Id = @Level_Id + 1
Declare @Group_No int, @SrNo int
Set @SrNo = isnull((select MAX(Num03) from Rpt_Temp where UserName = @UserName and HostName = @HostName and ReportName = 'ACC_MAP'),0) + 1
print @SrNo
insert into Rpt_Temp (UserName , HostName, ReportName,
Num01 , Num02 , Num03, Num04,Num05, Num06, Num07, Num08,
Str01,Str02 )
select @UserName, @HostName, 'ACC_MAP',
@Company_No, @Sequence, @SrNo, @Level_Id-1, @Parent_Id, Parent_Group_No, 0, 1,
Group_Name,
Case when Account_Group.is_fixed='T' then
LTRIM(RTRIM(cast(@Company_No as varchar(10)) + '.' + Account_Group.Tree_ID))
else
LTRIM(RTRIM(Account_Group.Tree_ID))
end
from Account_Group where Group_No = @Parent_Id
Declare cur_Acc_Map cursor local for
Select Group_No from Account_Group
Where Company_No in (0,@Company_No) and Group_No <> 14
and Parent_Group_No = @Parent_Id
order by Group_Name
open cur_Acc_Map
fetch next from cur_Acc_Map into @Group_No
while @@fetch_status <> -1
begin
exec SP_Account_Map_Process @Company_No, @UserName, @HostName, @Sequence, @Group_No, @Level_Id,@Acc_year,@From_date,@To_date,@DispMode
fetch next from cur_Acc_Map into @Group_No
End
close cur_Acc_Map
deallocate cur_Acc_Map
insert into Rpt_Temp (UserName , HostName, ReportName,
Num01 , Num02 , Num03, Num04,Num05, Num06, Num07, Num08,
Str01,str02)
select @UserName, @HostName, 'ACC_MAP',
@Company_No, @Sequence, @SrNo, @Level_Id, 0, Parent_Group_No, Account_No, 0,
Account_Name,Tree_ID
from Account Where Company_No = @Company_No and Parent_Group_No = @Parent_Id
End