我的一个ms sql server存储过程

USE [DatabaseTest.local]
GO
/****** Object: StoredProcedure [dbo].[GetBbsTreeNodeDetailInfoByIdStr] Script Date: 06/28/2019 14:54:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetBbsTreeNodeDetailInfoByIdStr]
@IdStr nvarchar(1000)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Declare @idSqlStr nvarchar(1000);
Declare @tempNodeId int;
Declare @resultSql nvarchar(1000);

declare @error int
set @error=0

-- 临时保存传入的节点id的表
IF EXISTS(Select 1 From Sysobjects Where Name='#temp_table_NodeIdTable')
DROP table #temp_table_NodeIdTable
create Table #temp_table_NodeIdTable( Id int );

--保存将要输出的内容的表
IF EXISTS(Select 1 From Sysobjects Where Name='#temp_table_NodeInfoTable')
DROP table #temp_table_NodeInfoTable
create Table #temp_table_NodeInfoTable(
Id int,
PostCount int,
ReplyCount int,
RecentUpdateTime Datetime
);

--保存可能的各级节点的id
IF EXISTS(Select 1 From Sysobjects Where Name='#temp_table_NodeIdTable_2')
DROP table #temp_table_NodeIdTable_2
create Table #temp_table_NodeIdTable_2 ( Id int );

--保存所有帖子的id
IF EXISTS(Select 1 From Sysobjects Where Name='#temp_table_PostIdTable')
DROP table #temp_table_PostIdTable
create Table #temp_table_PostIdTable ( Id int );


set @idSqlStr = 'select col='''+ replace(@IdStr,',',''' union all select ''')+''''
insert into #temp_table_NodeIdTable exec(@idSqlStr);
declare ids_cursor cursor for select * from #temp_table_NodeIdTable;

declare @tmpPostCount int;
declare @tmpReplyCount int;
declare @tmpRecentUpdateTime datetime;

BEGIN
open ids_cursor
fetch next from ids_cursor into @tempNodeId
while @@FETCH_STATUS = 0 --返回被 FETCH语句执行的最后游标的状态--
BEGIN

--查出所有节点id
truncate table #temp_table_NodeIdTable_2;
truncate table #temp_table_PostIdTable;

with
cte
as
(
select id from [xt_BBS_LevelTree] where Id = @tempNodeId
union all
select a.id from [xt_BBS_LevelTree] a,cte b where a.pid = b.id
)
insert into #temp_table_NodeIdTable_2 select * from cte; --暂存此节点,以及此节点的下级节点的所有的节点id

--开始统计

--所有帖子的id
insert into #temp_table_PostIdTable select * from xt_BBS_PostDetail where LevelTreeId in ( select * from #temp_table_NodeIdTable_2);
--此节点下所有帖子的总数
select @tmpPostCount = COUNT(1) from xt_BBS_PostDetail where LevelTreeId in ( select * from #temp_table_NodeIdTable_2);
select @tmpReplyCount = COUNT(1) from xt_BBS_ReplyDetail where PostId in ( select * from #temp_table_PostIdTable);
select top(1) @tmpRecentUpdateTime = CreateTime from xt_BBS_ReplyDetail where PostId in ( select * from #temp_table_PostIdTable) order by Id desc;

--插入一条
insert into #temp_table_NodeInfoTable(Id, PostCount, ReplyCount, RecentUpdateTime) values(@tempNodeId, @tmpPostCount,@tmpReplyCount,GETDATE());

set @error= @error + @@ERROR --记录每次运行sql后是否正确,0正确

fetch next from ids_cursor into @tempNodeId --转到下一个游标,没有会死循环
END
close ids_cursor --关闭游标
END

SELECT * from #temp_table_NodeInfoTable;
END