DROP PROCEDURE showMatchOnDate
go
CREATE PROCEDURE showMatchOnDate
@MatchDate DATE
AS
DECLARE @tempTeams TABLE
(
Id CHAR(3) PRIMARY KEY,
name VARCHAR(40),
nomatches INT,
owngoals INT,
othergoals INT,
points INT
)
INSERT INTO @tempTeams(Id, name, nomatches, owngoals, othergoals, points)
SELECT * FROM teams
DECLARE @homeGoals INT
DECLARE @outGoals INT
DECLARE @homeID CHAR(3)
DECLARE @outID CHAR(3)
DECLARE @count INT
DECLARE @totalAmount INT
SET @totalAmount = (SELECT COUNT(*) FROM matches WHERE matchdate <= @MatchDate)
SET @count = 1
SELECT @totalAmount
WHILE (@count <= @totalAmount)
BEGIN
SELECT @homeGoals = (SELECT homegoal FROM matches WHERE Id = @count)
SELECT @outGoals = (SELECT outgoal FROM matches WHERE Id = @count)
SELECT @homeID = (SELECT homeid FROM matches WHERE Id = @count)
SELECT @outID = (SELECT outid FROM matches WHERE Id = @count)
UPDATE @tempTeams
SET nomatches = nomatches + 1
WHERE Id = @homeID OR Id = @outID
UPDATE @tempTeams
SET owngoals = owngoals + @homeGoals
WHERE Id = @homeID
UPDATE @tempTeams
SET owngoals = owngoals + @outGoals
WHERE Id = @outID
UPDATE @tempTeams
SET othergoals = othergoals + @outGoals
WHERE Id = @homeID
UPDATE @tempTeams
SET othergoals = othergoals + @homeGoals
WHERE Id = @outID
UPDATE @tempTeams
SET points = points + 3
WHERE Id = @homeID AND @homeGoals > @outGoals
UPDATE @tempTeams
SET points = points + 3
WHERE Id = @outID AND @outGoals > @homeGoals
IF @outGoals = @homeGoals
UPDATE @tempTeams
SET points = points + 1
WHERE Id = @homeID OR (Id = @outID)
SET @count = @count + 1
END
SELECT * FROM @tempTeams
go
По какой-то причине, когда я запускаю это, я получаю дубликат каждого элемента.который я не хочу.Очевидно, я хочу правильное значение, которое должно составлять 154 различных элемента.Но вместо этого я получаю 308, и каждый предмет дублируется.Кто-нибудь знает почему?