У меня есть то, что, как мне кажется, описывается как динамическая хранимая процедура, сокращенная версия приведена ниже.
[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
Ошибка, которую я получаю, выглядит следующим образом
Сообщение 102, уровень 15, состояние 1, строка 8 Неверный синтаксис рядом с «ROWS». Сообщение 153, уровень 15, состояние 2, строка 8. Недопустимое использование параметра «Далее» в операторе FETCH.
Обратите внимание, что я также пробовал N'в начале каждой строки, и ошибка та же самая.