动态存储过程和分页

Dynamic Stored Procedure and Pagination

我有一个我认为被描述为动态存储过程的东西,下面是一个缩写版本

[dbo].[spImageCard_GetStatusByCountryAlphabetically]
            @WhereClause nvarchar(2000)
                    AS
                    BEGIN
                    SET NOCOUNT ON
                    Declare @SelectStatement nvarchar(1800)
                    Declare @OrderClause nvarchar(200)
                    Declare @FullStatement nvarchar(4000)
                    Set @SelectStatement = 'Select Distinct [StampTable].StampId,[SeriesTable].Series, [StampTable].CatalogueNo, [CountryTable].Country, [ImageTable].Image From StampTable
                    left Join SeriesTable on StampTable.Series = SeriesTable.SeriesId
                    left Join ImageTable on StampTable.Image = ImageTable.ImageId
                    left Join CountryTable on StampTable.Country = CountryTable.CountryId
                    left Join StampQuantatiesTable on StampTable.StampId = StampQuantatiesTable.StampId
                    Left Join QuantatiesTable on StampQuantatiesTable.QuantatiesId = QuantatiesTable.QuantatiesId
                    Left Join [StatusTable] on  QuantatiesTable.StatusId = StatusTable.StatusId '
                    Set @OrderClause = 'ORDER BY [CountryTable].Country, [StampTable].CatalogueNo'
                    Set @FullStatement = @SelectStatement + @WhereClause + @OrderClause
                    
                    Execute (@FullStatement)

当被以下对象调用时,这会按预期工作

Declare @WhereClause Nvarchar(2000)
                    Set @WhereClause= 'WHERE StatusTable.Status IN (''Have'',''Want'')
                    AND [CountryTable].Country IN (''Aden'')'
                    Execute spImageCard_GetStatusByCountryAlphabetically @WhereClause = @WhereClause

我现在正在尝试介绍我认为称为分页的内容,我为此简化的独立工作示例是

[dbo].[spPageReturnTest]
            @PageNumber As INT,@RowsInPage As INT
                    AS
                    BEGIN
                    SET NOCOUNT ON
Select stampid, CatalogueNo From StampTable
Order by Stampid
Offset (@PageNumber-1)*@RowsInPage ROWS
Fetch Next @RowsInPage Rows Only

不幸的是,当我尝试引入相关部分(@Parameters 以及 Offset 和 Fetch 行)时,我遇到了很多错误。 如有任何帮助,我们将不胜感激

我的最新版本采纳了您对 sqlString 和 cast 的评论如下:

[dbo].[spImageCard_GetStatusByCountryAlphabetically]
            @WhereClause nvarchar(2000), @PageNumber INT,@RowsInPage INT
                    AS
                    BEGIN
                    SET NOCOUNT ON
                    Declare @SelectStatement nvarchar(1650)
                    Declare @OrderClause nvarchar(200)
                    Declare @FullStatement nvarchar(4000)
                    Declare @Pagination nvarchar(150)
                    Declare @PageNumberString nvarchar(4)
                    Declare @RowsInPageString nvarchar(4)
                    Set @PageNumberString = Cast(@PageNumber As nvarchar(4))
                    Set @RowsInPageString = cast(@RowsInPage As nvarchar(4))
                    Set @SelectStatement = 'Select Distinct [StampTable].StampId,[SeriesTable].Series, [StampTable].CatalogueNo, [CountryTable].Country, [ImageTable].Image From StampTable
                    left Join SeriesTable on StampTable.Series = SeriesTable.SeriesId
                    left Join ImageTable on StampTable.Image = ImageTable.ImageId
                    left Join CountryTable on StampTable.Country = CountryTable.CountryId
                    left Join StampQuantatiesTable on StampTable.StampId = StampQuantatiesTable.StampId
                    Left Join QuantatiesTable on StampQuantatiesTable.QuantatiesId = QuantatiesTable.QuantatiesId
                    Left Join [StatusTable] on  QuantatiesTable.StatusId = StatusTable.StatusId '
                    Set @OrderClause = 'ORDER BY [CountryTable].Country, [StampTable].CatalogueNo'
                    Set @Pagination = 'Offset (('+@PageNumberString+'-1)*'+@RowsInPageString+') ROWS Fetch Next '+@RowsInPageString+' Rows Only'
                    Set @FullStatement = @SelectStatement + @WhereClause + @OrderClause + @Pagination
                    Execute sp_executesql @FullStatement

而我的调用程序是

Declare @WhereClause Nvarchar(2000)
Declare @PageNumber INT
Declare @RowsInPage INT
                        Set @WhereClause= 'WHERE StatusTable.Status IN (''Have'',''Want'')
                    AND [CountryTable].Country IN (''Aden'')'
                        Execute spImageCard_GetStatusByCountryAlphabetically @WhereClause = @WhereClause,@PageNumber=1,@RowsInPage=3

我收到的错误如下

Msg 102, Level 15, State 1, Line 8 Incorrect syntax near 'ROWS'. Msg 153, Level 15, State 2, Line 8 Invalid usage of the option Next in the FETCH statement.

请注意我也在每个字符串的开头尝试了 N' 并且错误是相同的

我终于设法解决了这个问题,我不敢相信一旦我发现了这个错误就可以这么简单地解决它,但同样也不敢相信这个错误是多么容易犯。 构造字符串时,确保每个字符串参数的末尾都有一个 space 否则在我的情况下,当 @OrderClause 和 @Pagination 被连接时,它被读取为

ORDER BY [CountryTable].Country, [StampTable].CatalogueNoOffset (('+@PageNumberString+'-1)*'+@RowsInPageString+') ROWS Fetch Next '+@RowsInPageString+' Rows Only']

CatalogueNo 和 Offset 之间没有 space 当我 运行 通过我保留的许多早期努力时,这是根本问题,但在不同的地方,通常是多个地方