如果声明使用计数

时间:2016-06-27 16:17:14

标签: sql sql-server

我在下面显示了一个联赛表,所有球队都互相比赛:

enter image description here

根据他们的LeagueID,我想确定如果玩的游戏数量达到了超过其限制,那么就会提出错误,说“赛季结束了”。

我对IF声明有点失落,需要一些帮助才能确定。我想在下面的例子中说,如果播放的游戏数量超过22,则提出错误,但我不想使用22的固定值,因为我需要假装联盟可以在某个时候扩展更多团队的未来。

如何做到这一点?目前计算计算每个联赛的所有球队,乘以2确定主场和客场,然后减2,因为每支球队都没有自己发挥。

CREATE PROCEDURE [dbo].[League_Table_Insert]
    @LeagueName VARCHAR(30)
AS
SET NOCOUNT ON
BEGIN

    DECLARE @LeagueID INT
    DECLARE @TotalMatches INT

    SELECT 
    @LeagueID = LeagueID FROM dbo.League
    WHERE LeagueName = @LeagueName

    SELECT [Position], [TeamName], [Played], [Wins], [Loss], [Draws], [Points], [Goals_Scored], [Goals_Against], [Goal_Difference], [LeagueID]  
    FROM League_Table
    WHERE LeagueID = @LeagueID
    ORDER BY Points DESC, Goal_Difference DESC;

    --select [Position], [TeamName], [Played], [Wins], [Loss], [Draws], [Points], [Goals_Scored], [Goals_Against], [Goal_Difference], [LeagueID]   from League_Table

    SELECT @TotalMatches = SUM(COUNT(*) * 2 - 2) FROM dbo.League_Table WHERE LeagueID = @LeagueID

        IF  >= 
    BEGIN
        RAISERROR('season finished', 16, 1);
        RETURN;
    END
END

1 个答案:

答案 0 :(得分:0)

IF (SELECT MIN(Played) FROM dbo.League_Table WHERE LeagueID = @LeagueID) >= @TotalMatches

假设您的总比赛计算是正确的,那么if语句会查找该联赛的最低比赛数并检查总数