Note that there are some explanatory texts on larger screens.

plurals
  1. POStored Procedure and output parameter from paging script (SQL Server 2008)
    primarykey
    data
    text
    <p>I have the below stored procedure and would like to only have one SQL statement. At the moment you can see there are two statements, one for the actual paging and one for a count of the total records which needs to be return to my app for paging.</p> <p>However, the below is inefficient as I am getting the total rows from the first query:</p> <pre><code>COUNT(*) OVER(PARTITION BY 1) as TotalRows </code></pre> <p>How can I set TotalRows as my output parameter?</p> <pre><code>ALTER PROCEDURE [dbo].[Nop_LoadAllOptimized] ( @PageSize int = null, @PageNumber int = null, @WarehouseCombinationID int = null, @CategoryId int = null, @OrderBy int = null, @TotalRecords int = null OUTPUT ) AS BEGIN WITH Paging AS ( SELECT rn = (ROW_NUMBER() OVER ( ORDER BY CASE WHEN @OrderBy = 0 AND @CategoryID IS NOT NULL AND @CategoryID &gt; 0 THEN pcm.DisplayOrder END ASC, CASE WHEN @OrderBy = 0 THEN p.[Name] END ASC, CASE WHEN @OrderBy = 5 THEN p.[Name] END ASC, CASE WHEN @OrderBy = 10 THEN wpv.Price END ASC, CASE WHEN @OrderBy = 15 THEN wpv.Price END DESC, CASE WHEN @OrderBy = 20 THEN wpv.Price END DESC, CASE WHEN @OrderBy = 25 THEN wpv.UnitPrice END ASC )),COUNT(*) OVER(PARTITION BY 1) as TotalRows, p.*, pcm.DisplayOrder, wpv.Price, wpv.UnitPrice FROM Nop_Product p INNER JOIN Nop_Product_Category_Mapping pcm ON p.ProductID=pcm.ProductID INNER JOIN Nop_ProductVariant pv ON p.ProductID = pv.ProductID INNER JOIN Nop_ProductVariant_Warehouse_Mapping wpv ON pv.ProductVariantID = wpv.ProductVariantID WHERE pcm.CategoryID = @CategoryId AND (wpv.Published = 1 AND pv.Published = 1 AND p.Published = 1 AND p.Deleted = 0 AND pv.Deleted = 0 and wpv.Deleted = 0) AND wpv.WarehouseID IN (select WarehouseID from Nop_WarehouseCombination where UserWarehouseCombinationID = @WarehouseCombinationID) ) SELECT TOP (@PageSize) * FROM Paging PG WHERE PG.rn &gt; (@PageNumber * @PageSize) - @PageSize SELECT @TotalRecords = COUNT(p.ProductId) FROM Nop_Product p INNER JOIN Nop_Product_Category_Mapping pcm ON p.ProductID=pcm.ProductID INNER JOIN Nop_ProductVariant pv ON p.ProductID = pv.ProductID INNER JOIN Nop_ProductVariant_Warehouse_Mapping wpv ON pv.ProductVariantID = wpv.ProductVariantID WHERE pcm.CategoryID = @CategoryId AND (wpv.Published = 1 AND pv.Published = 1 AND p.Published = 1 AND p.Deleted = 0 AND pv.Deleted = 0 and wpv.Deleted = 0) AND wpv.WarehouseID IN (select WarehouseID from Nop_WarehouseCombination where UserWarehouseCombinationID = @WarehouseCombinationID) END </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload