Добрый день! Есть процедураб выводящая данные из нескольких таблиц.
Если ее выполнить подряд несколько раз - она показывает каждый раз разную выборку данных.
Если точнее - выводит циклично результат по частям.
Не могу понять где ошибка, ведь с одинаковыми исходными параметрами и результат должен быть один. Чую, что подвох в реализации paging, но моих знаний не хватает чтобы ошибку найти.
Вот код:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AllSitesGet]
--Params
@PageStart INT = 0, --Used for paging
@PageSize INT = 200, --Used for paging
@SortColumn INT = 5, --Used for Sorting
@SortDirection INT = 1 --Used for Sorting
AS
BEGIN
SET NOCOUNT ON
--=========================================================
--Paging Variables
--=========================================================
DECLARE @TotalRows INT
--=========================================================
--Temporary table for paging...
--=========================================================
CREATE TABLE #Results (
RowID INT IDENTITY(1,1),
SiteName VARCHAR(250),
OID VARCHAR(256),
IPAddress VARCHAR(256),
Enabled BIT,
ProtocolID INT,
PSVersion VARCHAR(32),
Port INT,
RCMServiceAddress VARCHAR(256),
RCM_Enabled BIT
)
--=========================================================
--Select all records in the Solution table
--=========================================================
BEGIN
INSERT #Results (
SiteName,
OID,
IPAddress,
Enabled,
ProtocolID,
PSVersion,
Port,
RCMServiceAddress,
RCM_Enabled
)
SELECT
SiteName,
OID,
IPAddress,
Enabled,
ProtocolID,
PSVersion,
Port,
RCMServiceAddress,
RCM_Enabled
FROM dbo.Site
ORDER BY
CASE WHEN @SortColumn = 5 AND @SortDirection = 2
THEN SiteName END DESC,
CASE WHEN @SortColumn = 5 AND @SortDirection != 2
THEN SiteName END,
CASE WHEN @SortColumn = 2 AND @SortDirection = 2
THEN OID END DESC,
CASE WHEN @SortColumn = 2 AND @SortDirection != 2
THEN OID END,
CASE WHEN @SortColumn = 1 AND @SortDirection = 2
THEN IPAddress END DESC,
CASE WHEN @SortColumn = 1 AND @SortDirection != 2
THEN IPAddress END,
CASE WHEN @SortColumn = 6 AND @SortDirection = 2
THEN Enabled END DESC,
CASE WHEN @SortColumn = 6 AND @SortDirection != 2
THEN Enabled END,
CASE WHEN @SortColumn = 7 AND @SortDirection = 2
THEN ProtocolID END DESC,
CASE WHEN @SortColumn = 7 AND @SortDirection != 2
THEN ProtocolID END,
CASE WHEN @SortColumn = 3 AND @SortDirection = 2
THEN PSVersion END DESC,
CASE WHEN @SortColumn = 3 AND @SortDirection != 2
THEN PSVersion END,
CASE WHEN @SortColumn = 8 AND @SortDirection = 2
THEN Port END DESC,
CASE WHEN @SortColumn = 8 AND @SortDirection != 2
THEN Port END,
CASE WHEN @SortColumn = 4 AND @SortDirection = 2
THEN RCMServiceAddress END DESC,
CASE WHEN @SortColumn = 4 AND @SortDirection != 2
THEN RCMServiceAddress END,
CASE WHEN @SortColumn = 9 AND @SortDirection = 2
THEN RCM_Enabled END DESC,
CASE WHEN @SortColumn = 9 AND @SortDirection != 2
THEN RCM_Enabled END
END
--=========================================================
--Paging Logic..
--=========================================================
SELECT @TotalRows = COUNT(*)
FROM #Results
SET ROWCOUNT @PageSize
--=========================================================
--Select with paging
--=========================================================
SELECT
RowID,
SiteName,
OID,
IPAddress,
Enabled,
ProtocolID,
PSVersion,
Port,
RCMServiceAddress,
RCM_Enabled,
@TotalRows TotalRows
FROM #Results
WHERE RowID > @PageStart
--=========================================================
--Cleanup
--=========================================================
DROP TABLE #Results
--=========================================================
--Check for any flags caused by SQL
--=========================================================
IF @@ERROR <> 0
BEGIN
RAISERROR ('AllSitesGet failed - Statement Aborted',16,1)
RETURN
END
SET NOCOUNT OFF
END