SQL Soccer Fixture

时间:2015-08-20 09:27:17

标签: sql-server-2008

我是t-SQL的新手,我的主管给了我一个我正在努力的项目。我试图从给定的数据创建一个夹具。有一个数据长306行,包含每一个匹配。他们有一个Id。有家庭和离开选项。我设法从存储过程中得到一个装置,但问题是每次我运行程序时,一个团队有可能在一周内玩多个匹配。我认为问题在于if exists行,但我找不到如何修复它。你能帮助我吗?

GO
/****** Object:  StoredProcedure [dbo].[Fixture_Creator]    Script Date: 08/20/2015 12:31:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Fixture_Creator]
(
 @Season int,
 @Message nvarchar(MAX) output
)
AS
BEGIN
IF EXISTS(SELECT * FROM Fixture WHERE Fixture.Season = @Season)
BEGIN
set @Message = 'This Season is already given'
END
ELSE
BEGIN
DECLARE @WEEK INT = 1;
DECLARE @MATCH INT = 1;
DECLARE @HOMESCORE INT = 0;
DECLARE @AWAYSCORE INT = 0;
DECLARE @HOMETEAM nvarchar(MAX);
DECLARE @AWAYTEAM nvarchar(MAX);
DECLARE @COUNTER INT = 0;
DECLARE @MATCHID INT;

While(@WEEK<=34)BEGIN
While(@MATCH<=9)BEGIN
SELECT @MATCHID = FLOOR(RAND()*306+1);
SELECT @HOMETEAM = HOME FROM Matches WHERE Id = @MATCHID
SELECT @AWAYTEAM = AWAY FROM Matches WHERE Id = @MATCHID
if not exists(SELECT * FROM Fixture WHERE Season = Season OR Week = @WEEK OR @HOMETEAM = Home OR @AWAYTEAM = Away OR @HOMETEAM = Away OR @AWAYTEAM = Home)
BEGIN
set @HOMESCORE = FLOOR(RAND()*(5));
set @AWAYSCORE = FLOOR(RAND()*(5));
INSERT INTO Fixture(Season, Week, [Match Number],HOME,Away,[Home Score],[Away Score])
VALUES (@SEASON,@WEEK,@MATCH,@HOMETEAM,@AWAYTEAM,@HOMESCORE,@AWAYSCORE);
set @MATCH = @MATCH + 1;
END
END
set @MATCH = 1;
set @WEEK = @WEEK+1;
END
END
END

0 个答案:

没有答案