简体   繁体   English

我无法从存储过程获取临时表数据

[英]I can't fetch temp table data from store procudere

I created on store procedure in sql server below is the structure 我在SQL Server下面的存储过程中创建的是以下结构

ALTER PROCEDURE [dbo].[list_final_player_report]
    -- Add the parameters for the stored procedure here
    @teamid int
AS
BEGIN
CREATE TABLE #TempTable (
                        playername varchar(50),
                        RANK_in_speedladdar INT,
                        rank_in_120s int,
                        rank_in_cone int,
                        rank_in_beep int,
                        rank_in_cooper int,
                        rank_in_pushups int,
                        rank_in_situp int,
                        rank_in_pullup int,
                        rank_in_40s int,
                        rank_in_pushopbattle int,
                        rank_in_vertical_jump int,
                        rank_in_shuttle int
                    )
DECLARE @playerid int
DECLARE @firstname nvarchar(10) 

-- Define the cursor
DECLARE cursor1 cursor for
select Players.PlayerID as PlayerID , players.firstname  from Players where Teamid=@teamid

-- We should open the cursor
OPEN cursor1

-- We need to Fetch the rows into @EmpId variable
FETCH cursor1 into @playerid,@firstname

--Looping
WHILE(@@fetch_status=0)
BEGIN

insert into #TempTable 
values(@firstname,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =1 and ChallengeStats.PlayerID=@playerid ),
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =2 and ChallengeStats.PlayerID=@playerid ),
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =3 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =4 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =5 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =6 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =7 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =8 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =9 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =10 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =11 and ChallengeStats.PlayerID=@playerid ) ,
(select  (sum(Total))/count(PlayerID)  from ChallengeStats WHERE ChallengeStats.ChallengeID =12 and ChallengeStats.PlayerID=@playerid )  
)

FETCH cursor1 into @playerid,@firstname
END

close cursor1
deallocate cursor1 
select * from #TempTable
END

this sp is perfectly execute in sqlserver 这个sp在sqlserver中完美执行

but the problem is now how to get this temp table data into php resultset 但是现在的问题是如何将这个临时表数据放入php结果集中

instead of using a temp table, why not use a Table variable. 而不使用临时表,为什么不使用Table变量。 You should not have to change much code in order to get this implemented. 您不必为了实现此功能而更改太多代码。

DECLARE @TempTable TABLE (
                        playername varchar(50),
                        RANK_in_speedladdar INT,
                        rank_in_120s int,
                        rank_in_cone int,
                        rank_in_beep int,
                        rank_in_cooper int,
                        rank_in_pushups int,
                        rank_in_situp int,
                        rank_in_pullup int,
                        rank_in_40s int,
                        rank_in_pushopbattle int,
                        rank_in_vertical_jump int,
                        rank_in_shuttle int
                    )

Temp tables are only good during the scope of where they are used, which is what could be causing your issue. 临时表仅在使用它们的范围内才是好的,这可能会导致您的问题。 Try the above, and you should get a different result. 尝试上述操作,您将获得不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM