I am trying to order this SP for paging by date, but I don’ t know why the normal way of ordering the results it doesn’ t work. I am becoming crazy! I tried this:USE marketGOALTER PROCEDURE offers_list@CurrentPage int, @PageSize int, @TotalItems int outputASSET NOCOUNT ONDECLARE @v_FirstOID bigintDECLARE @v_LastOID bigintDECLARE @v_FirstDate datetimeDECLARE @v_LastDate datetimeDECLARE @v_MaxRowNumber intSET @v_MaxRowNumber = @CurrentPage * @PageSize SET @TotalItems = 0SELECT @TotalItems = @TotalItems + 1 ,@v_FirstOID = CASE WHEN (@TotalItems = @v_MaxRowNumber - (@PageSize - 1)) THEN abt.Offer_idELSE @v_FirstOIDEND,@v_FirstDate = CASE WHEN (@TotalItems = @v_MaxRowNumber - (@PageSize - 1)) THEN abt.theDateELSE @v_FirstDate END,@v_LastOID = CASE WHEN @TotalItems <= @v_MaxRowNumber THEN abt.Offer_idELSE @v_LastOID END,@v_LastDate = CASE WHEN @TotalItems <= @v_MaxRowNumber THEN abt.theDateELSE @v_LastDate ENDFROM( SELECT Offer_id, theDate FROM Offers AS ofe INNER JOIN TheUsers As us On ofe.User_num = us.User_id INNER JOIN Cities As city On ofe.city_num = city.City_id) AS abt ORDER BY abt.theDate DescSELECT Offer_id, theDate, Title, us.Company_name, city.City_name FROM Offers AS ofe INNER JOIN TheUsers As us On ofe.User_num = us.User_idINNER JOIN Cities As city On ofe.city_num = city.City_idWhere (theDate <= @v_FirstDate) AND (theDate >= @v_LastDate)ORDER BY theDate DescSET NOCOUNT OFFGO
But in this way any row is returned. Instead, if I order by Offer_id then works perfect:...ORDER BY abt.Offer_id DescSELECT Offer_id, theDate, Title, us.Company_name, city.City_name FROM Offers AS ofeINNER JOIN TheUsers As us On ofe.User_num= us.User_idINNER JOIN Cities As city On ofe.city_num = city.City_id WHERE (theDate <= @v_FirstDate) AND (theDate >= @v_LastDate) ORDER BY Offer_id Desc...
And also works in this way:...ORDER BY abt.Offer_id DescSELECT Offer_id, theDate, Title, us.Company_name, city.City_name FROM Offers AS ofeINNER JOIN TheUsers As us On ofe.User_num= us.User_idINNER JOIN Cities As city On ofe.city_num = city.City_id WHERE (theDate <= @v_FirstDate) AND (theDate >= @v_LastDate) ORDER BY theDate Desc...
And if I make a simple query in Offers table, ordering by date, also works.Somebody can help me please? I don’ t know why I can’ t order by date in this SP. What it may happen? Thank you