0

以下のSQLServerのストアドプロシージャで作成した構造は

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

このspはsqlserverで完全に実行されます

しかし、問題は、この一時テーブルデータをphp結果セットに取り込む方法です。

4

1 に答える 1

0

一時テーブルを使用する代わりに、テーブル変数を使用しないでください。これを実装するために多くのコードを変更する必要はありません。

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
                    )

一時テーブルは、それらが使用されている範囲でのみ有効です。これが問題の原因となっている可能性があります。上記を試してみると、別の結果が得られるはずです。

于 2013-02-11T08:15:16.170 に答える