我正在尝试创建一个用于在aspx页面上显示分页数据的SP。我写了以下代码 -
Create PROCEDURE [dbo].[sp_GetAllAssignmentData_Paged]
@currentPage INT=1,
@pageSize INT=20
AS
BEGIN
SET NOCOUNT ON;
with AssignmentData As(
select ROW_NUMBER() over (order by a.StockNo desc) AS [Row],
a.StockNo,c.ClaimNo,v.[Year],v.Make,v.Model,
c.DOAssign,c.InsuranceComp,c.Location,c.Status
from
dbo.Assignments a,
dbo.Assignment_ClaimInfo c,
dbo.Assignment_VehicleInfo v
where
(a.AssignmentID=c.AssignmentID) and
(v.AssignmentID=c.AssignmentID)
order by a.StockNo desc
)
SELECT StockNo, ClaimNo, [Year], Make, Model, DOAssign, InsuranceComp, Location, [Status]
FROM AssignmentData
WHERE Row between ((@currentPage - 1) * @pageSize + 1) and (@currentPage*@pageSize) END
当我尝试创建此SP时,会生成以下错误消息 - 除非还指定了TOP或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。
有人可以纠正我的错误吗?
感谢您分享宝贵的时间。
答案 0 :(得分:6)
内部集合AssignmentData的排序是没有意义的,因为它是从该集合中选择将决定排序的。因此,不允许这样做。移动线条
order by a.StockNo desc
到你的最终选择
答案 1 :(得分:1)
将orderby移动到WITH块之外。
答案 2 :(得分:1)
你的CTE最后有一个无效的命令 - 把它取出来,一切都应该好。
select ROW_NUMBER() over (order by a.StockNo desc) AS [Row],
a.StockNo,c.ClaimNo,v.[Year],v.Make,v.Model,
c.DOAssign,c.InsuranceComp,c.Location,c.Status
from
dbo.Assignments a,
dbo.Assignment_ClaimInfo c,
dbo.Assignment_VehicleInfo v
where
(a.AssignmentID=c.AssignmentID) and
(v.AssignmentID=c.AssignmentID)
order by a.StockNo desc -- This is the problem.
答案 3 :(得分:0)
创建PROCEDURE [dbo]。[sp_GetAllAssignmentData_Paged] @currentPage INT = 1,@ pageSize INT = 20ASBEGIN SET NOCOUNT ON; with AssignmentData As(选择ROW_NUMBER()结束(按a.StockNo desc排序)AS [Row],a.StockNo,c.ClaimNo,v。[Year],v.Make,v.Model,c.DOAssign,c。 InsuranceComp,c.Location,c.Status from dbo.Assignments a,dbo.Assignment_ClaimInfo c,dbo.Assignment_VehicleInfo v where(a.AssignmentID = c.AssignmentID)and(v.AssignmentID = c.AssignmentID))SELECT StockNo,ClaimNo, [年],制作,模型,DOAssign,InsuranceComp,位置,[状态] FROM AssignmentData WHERE行之间((@ currentPage - 1)* @pageSize + 1)和(@ currentPage * @ pageSize) 由a.StockNo desc订购 END