我在gridview中使用此存储过程进行自定义分页,当我使用ORDER BY
时出现错误
ALTER PROCEDURE [dbo].[spGetAllTender]
@pageIndex int = 0
, @pageSize int = 0
AS
BEGIN
SET NOCOUNT ON;
/* Set Starting Row Index and Ending Row Index */
-------------------------------------------------
DECLARE @startRowIndex int = 0
SET @startRowIndex = (@pageIndex * @pageSize) + 1
DECLARE @endRowIndex int = 0
SET @endRowIndex = (@startRowIndex + @pageSize - 1)
/* recordCount required for paging in front end */
DECLARE @recordCount int = 0
SET @recordCount = 0
-------------------------------------------------
SELECT
@recordCount = COUNT(*)
FROM [vwTenderDetail] A WITH (NOLOCK)
SELECT
TenderID,
TenderRefNumber,
TenderTitle,
subProductCatName,
CreatedDate,
UserName,
ModifiedDate,
ModifiedUserName,
tRecordCount
FROM
(SELECT
TD.TenderID,
TD.TenderRefNumber,
TD.TenderTitle,
TD.SubProductCatName,
TD.CreatedDate,
TD.UserName,
TD.ModifiedDate,
TD.ModifiedUserName,
ROW_NUMBER() OVER (ORDER BY TD.TenderID) AS RowNum,
@recordCount AS tRecordCount,
TD.Enabled,
COUNT(TC.TenderID) AS NoOFCorrigendum
FROM
[vwTenderDetail] TD (NOLOCK)
LEFT JOIN
TenderCorrigendum TC (NOLOCK) ON TD.TenderID = TC.TenderID
WHERE
TD.Enabled = 1
GROUP BY
TD.TenderID, TD.TenderRefNumber, TD.TenderTitle, TD.SubProductCatName,
TD.CreatedDate, TD.UserName, TD.ModifiedDate, TD.ModifiedUserName, TD.Enabled
ORDER BY
TenderID DESC) AS TEMP
WHERE
RowNum BETWEEN @startRowIndex AND @endRowIndex
END
我收到此错误:
Msg 1033,Level 15,State 1,Procedure spGetAllTender,Line 64
ORDER BY子句在视图,内联函数,派生表中无效, 子查询和公用表表达式,除非是TOP或FOR XML 也指明了。
当我在order by
之后使用select top 10(PageSize)
行,然后根据标准对这10行进行排序
答案 0 :(得分:0)
将order by
放在外部查询上。不确定这是否是你想要的,但它会使错误消失:
ALTER PROCEDURE [dbo].[spGetAllTender]
@pageIndex int = 0
, @pageSize int = 0
AS
BEGIN
SET NOCOUNT ON;
/* Set Starting Row Index and Ending Row Index */
-------------------------------------------------
DECLARE @startRowIndex int = 0
SET @startRowIndex = (@pageIndex * @pageSize) + 1
DECLARE @endRowIndex int = 0
SET @endRowIndex = (@startRowIndex + @pageSize - 1)
/* recordCount required for paging in front end */
DECLARE @recordCount int = 0
SET @recordCount = 0
-------------------------------------------------
SELECT
@recordCount = COUNT(*)
FROM [vwTenderDetail] A WITH (NOLOCK)
SELECT
TenderID,
TenderRefNumber,
TenderTitle,
subProductCatName,
CreatedDate,
UserName,
ModifiedDate,
ModifiedUserName,
tRecordCount
FROM (SELECT
TD.TenderID,
TD.TenderRefNumber,
TD.TenderTitle,
TD.SubProductCatName,
TD.CreatedDate,
TD.UserName,
TD.ModifiedDate,
TD.ModifiedUserName,
ROW_NUMBER() OVER (
ORDER BY TD.TenderID
) AS RowNum,
@recordCount AS tRecordCount,
TD.Enabled,
COUNT(TC.TenderID) AS NoOFCorrigendum
FROM [vwTenderDetail] TD (NOLOCK)
LEFT JOIN TenderCorrigendum TC (NOLOCK)
ON TD.TenderID = TC.TenderID
WHERE TD.Enabled = 1
GROUP BY TD.TenderID,
TD.TenderRefNumber,
TD.TenderTitle,
TD.SubProductCatName,
TD.CreatedDate,
TD.UserName,
TD.ModifiedDate,
TD.ModifiedUserName,
TD.Enabled
) AS TEMP
WHERE RowNum BETWEEN @startRowIndex
AND @endRowIndex
ORDER BY TenderID DESC
END
答案 1 :(得分:0)
CREATE PROCEDURE [dbo].[spGetAllTender]
@pageIndex int= 0
, @pageSize int= 0
AS
BEGIN
SET NOCOUNT ON;
/* Set Starting Row Index and Ending Row Index */
DECLARE @startRowIndex int= 0
SET @startRowIndex =(@pageIndex * @pageSize)+ 1
DECLARE @endRowIndex int= 0
SET @endRowIndex =(@startRowIndex + @pageSize - 1)
/* recordCount required for paging in front end */
DECLARE @recordCount int= 0
SET @recordCount = 0
SELECT
@recordCount =COUNT(*)
FROM [vwTenderDetail] A WITH (NOLOCK)
SELECT
TenderID,
TenderRefNumber,
TenderTitle,
subProductCatName,
CreatedDate,
UserName,
ModifiedDate,
ModifiedUserName,
tRecordCount
FROM (
SELECT
TD.TenderID,
TD.TenderRefNumber,
TD.TenderTitle,
TD.SubProductCatName,
TD.CreatedDate,
TD.UserName,
TD.ModifiedDate,
TD.ModifiedUserName,
ROW_NUMBER()OVER (
ORDER BY TD.TenderID desc
)AS RowNum,
@recordCount AS tRecordCount,
TD.Enabled,
COUNT(TC.TenderID)AS NoOFCorrigendum
FROM
[vwTenderDetail] TD(NOLOCK)
LEFT JOIN
TenderCorrigendum TC(NOLOCK)
ON
TD.TenderID = TC.TenderID
WHERE
TD.Enabled= 1
GROUP BY
TD.TenderID,
TD.TenderRefNumber,
TD.TenderTitle,
TD.SubProductCatName,
TD.CreatedDate,
TD.UserName,
TD.ModifiedDate,
TD.ModifiedUserName,
TD.Enabled
)AS TEMP
WHERE RowNum BETWEEN @startRowIndex
AND @endRowIndex
END