SQL Server 2014 - OFFSET附近的语法不正确

时间:2016-09-01 04:16:14

标签: sql sql-server tsql stored-procedures offset

我正在使用SQL Server 2014管理工作室,我想创建一个存储过程但是我在代码的最后遇到了一个问题,其中错误" OFFSET附近的语法错误"出现。

CREATE PROCEDURE [dbo].[GetGamesPlayed]
    @gameSession int,
    @datestart datetime,
    @dateend datetime,
    @orderBy nvarchar(50) = 'email',
    @sortOrder nvarchar(4) = 'asc',
    @search nvarchar(50) = '',
    @startRow int = 0,
    @rows int = 2147483647
AS
BEGIN
    SET NOCOUNT ON;

    SELECT t.*,
            COUNT(*) OVER() AS filteredRows,
            (SELECT COUNT(id) FROM gameplayed WHERE gameplayed.id_GameSession = @gameSession) AS totalRows
    FROM (SELECT
            DISTINCT GamesTable.[Game ID],
            GamesTable.[Rep Id],
            GamesTable.[User Id],
            GamesTable.[Date],
            GamesTable.[Email],
            GamesTable.[Name],
            GamesTable.[Phone Number],
            GamesTable.[Tokens Played],
            CASE 
                WHEN prizewon.pending = 1 THEN 'Pending'
                WHEN prizewon.skillTestingQCorrect = 1 AND prizewon.rulesAccepted = 1 THEN 'Yes'
                ELSE 'No'
            END AS [Won]

            FROM PrizeWon 
                RIGHT OUTER JOIN (
                    SELECT 
                        GamePlayed.id AS [Game ID],
                        GamePlayed.playedOn AS [Date],
                        users.repId AS [Rep Id],
                        users.id AS [User Id],
                        users.fullName AS [Name],
                        users.email AS [Email],
                        users.phoneNumber AS [Phone Number],
                        GamePlayed.tokensPlayed AS [Tokens Played]

                    FROM GamePlayed 
                            INNER JOIN Users on GamePlayed.id_Users = Users.id 

                    WHERE GamePlayed.id_GameSession = @gameSession) AS GamesTable ON GamesTable.[Game ID] = prizewon.id_GamePlayed

    WHERE   GamesTable.Date >= @datestart
            AND GamesTable.Date <= @dateend
            AND (   GamesTable.[Email] LIKE '%' + @search + '%' 
                OR  GamesTable.[Rep Id] LIKE '%' + @search + '%'
                OR  GamesTable.[Name] LIKE '%' + @search + '%'
                OR  GamesTable.[Game ID] LIKE '%' + @search + '%'
                )
        ) t


--order by cases must be split into datatypes       
ORDER BY 
    CASE WHEN @sortOrder = 'asc' THEN
        CASE @orderBy -- nvarchar
            WHEN 'repid' THEN t.[Rep Id]
            WHEN 'name' THEN t.[Name]
            WHEN 'email' THEN t.[Email]
            WHEN 'phone' THEN t.[Phone Number]
            WHEN 'won' THEN t.Won
        END
    END ASC,
    CASE WHEN @sortOrder = 'asc' THEN
        CASE @orderBy --date
            WHEN 'date' THEN t.Date
        END
    END ASC,
    CASE WHEN @sortOrder = 'asc' THEN
        CASE @orderBy --int
            WHEN 'id' THEN t.[Game ID]
            WHEN 'tokens' THEN t.[Tokens Played]
        END
    END ASC,
    CASE WHEN @sortOrder = 'desc' THEN
        CASE @orderBy -- nvarchar
            WHEN 'repid' THEN t.[Rep Id]
            WHEN 'name' THEN t.[Name]
            WHEN 'email' THEN t.[Email]
            WHEN 'phone' THEN t.[Phone Number]
            WHEN 'won' THEN t.Won
        END
    END DESC,
    CASE WHEN @sortOrder = 'desc' THEN
        CASE @orderBy --date
            WHEN 'date' THEN t.Date
        END
    END DESC,
    CASE WHEN @sortOrder = 'desc' THEN
        CASE @orderBy --int
            WHEN 'id' THEN t.[Game ID]
            WHEN 'tokens' THEN t.[Tokens Played]
        END
    END DESC 

    OFFSET @startRow ROWS FETCH NEXT @rows ROWS ONLY
END

我在其他帖子中看到错误可能会出现,因为OFFSET不在ORDER BY之后,但在这种情况下它是在一个ORDER BY之后。

我看到的另一件事是SQL Server 2008不支持OFFSET,但我使用的是2014年。

我还检查了我使用的变量是否已声明,并且它们都是......

它几乎是唯一让我无法创建程序的错误。如果有人能提供帮助,我会非常感激!

1 个答案:

答案 0 :(得分:1)

经过一番研究后我用了#34; SELECT @@ version&#34;并发现我的数据库版本是2008,它不支持OFFSET。

2012版本的快速更新解决了这个问题。