存储过程分页问题
CREATE PROC Proc_AskPage
@pagesize INT,
@pageindex INT,
@Question_State INT,
@AskClass_Id INT,
@keywords NVARCHAR(100)
AS
SET NOCOUNT ON
BEGIN
DECLARE @indextable TABLE(Id INT identity(1,1),NId INT)
DECLARE @PageLowerBound INT
DECLARE @PageUpperBound INT
SET @PageLowerBound=(@pageindex-1)*@pagesize
SET @PageUpperBound=@PageLowerBound+@pagesize
SET rowcount @PageUpperBound
INSERT INTO @indextable(NId) SELECT Question_Id FROM Ask_Question WHERE Question_State='' + @Question_State + '' AND AskClass_Id='' + @AskClass_Id + '' AND Question_Title LIKE '%'+@Keywords+'%' ORDER BY Question_AddDate DESC
SELECT AQ.Question_Id,AQ.Question_Title,AQ.Question_Content,AQ.Question_AddDate,U.User_Nickname,AC.Class_Name FROM Ask_Question AQ,@indextable T,[User] U,Ask_Class AC WHERE AQ.Question_Id=T.NId
AND T.Id>@PageLowerBound AND T.Id<=@PageUpperBound AND AQ.User_Id=U.User_Id AND AQ.AskClass_Id=AC.Class_Id ORDER BY T.Id
END
SET NOCOUNT OFF
GO
不知道为什么,总是不能检索出正确的记录数。
------解决方案--------------------因为你insert 是查询的一张表
而在查询时又关联了另外2张表[User] ,Ask_Class
你能保证那张主表对应[User] ,Ask_Class 是多对1 或1对1的关系么
如果结果不对,那么就是关系不对