View Single Post
Old 06.10.2006, 07:01   #3
m4rk
Архивный участник
 
Join Date: Jan 2006
Posts: 607
Default

Quote:


CREATE PROCEDURE dbo.lin_NominateHeroes
(
@now_season INT,
@new_step INT,
@new_season_start_time INT
)
AS
SET NOCOUNT ON

UPDATE user_nobless
SET hero_type = 0

CREATE TABLE #hero_candidate (char_id INT, main_class INT, match_count INT, olympiad_point INT)
INSERT INTO #hero_candidate
SELECT un.char_id, ud.subjob0_class, un.match_count, un.olympiad_point
FROM user_nobless un, user_data ud
WHERE un.char_id = ud.char_id AND un.match_count >= 5 AND un.olympiad_win_count >= 1

CREATE TABLE #highest_score (main_class INT, olympiad_point INT, match_count INT)

DECLARE @hero_candidate_class INT
DECLARE hero_candidate_cursor CURSOR FOR
SELECT DISTINCT main_class FROM #hero_candidate

OPEN hero_candidate_cursor

FETCH NEXT FROM hero_candidate_cursor INTO @hero_candidate_class

WHILE @@FETCH_STATUS = 0
BEGIN

INSERT INTO #highest_score
SELECT TOP 1 main_class, olympiad_point, match_count
FROM #hero_candidate
WHERE main_class = @hero_candidate_class
ORDER BY olympiad_point DESC, match_count DESC

FETCH NEXT FROM hero_candidate_cursor INTO @hero_candidate_class
END

CLOSE hero_candidate_cursor
DEALLOCATE hero_candidate_cursor

CREATE TABLE #highest_score_nobless (char_id INT, main_class INT)
INSERT INTO #highest_score_nobless
SELECT c.char_id, c.main_class FROM #hero_candidate c, #highest_score s
WHERE c.main_class = s.main_class AND c.olympiad_point = s.olympiad_point AND c.match_count = s.match_count

CREATE TABLE #hero (char_id INT, main_class INT)
INSERT INTO #hero
SELECT char_id, main_class FROM #highest_score_nobless WHERE main_class IN (SELECT main_class FROM #highest_score_nobless GROUP BY main_class HAVING COUNT(main_class) = 1)

UPDATE user_nobless
SET hero_type = 1, win_count = win_count+1
WHERE char_id IN (SELECT char_id FROM #hero)

UPDATE user_nobless
SET previous_point = olympiad_point

UPDATE user_nobless
SET olympiad_point = 0, match_count = 0, olympiad_win_count = 0, olympiad_lose_count = 0

INSERT INTO olympiad_result
SELECT @now_season, main_class, char_id, olympiad_point, match_count FROM #hero_candidate

INSERT INTO olympiad
(step, season_start_time) VALUES (@new_step, @new_season_start_time)

SELECT @@IDENTITY





GO
I put this as a log of what I try to fix it, in case anyone has the same problem.

So... I will check all the tables affected by insert commands:
hero_candidate
highest_score
highest_score_nobless
hero
olympiad result
m4rk is offline