问题
I am working on a SQL Server report which shows the data of size 300 K and therefore it is very slow most of the time is spent on the report processing. So I m thinking if I can do some program to get the data from the database per page. This way call from the db and report processing time will reduce. So in other words if I am showing 50 records per page and when I am on page one and click on page 2 or the next button, my report get the data from 51 to 100. When I click on the next button again then I get the data for page 3 which would be 101 to 150.
So is there any way I can achieve.
回答1:
Usually when the report is rendered all the data is pulled and SSRS renders the report which causes delay. If performance is the key here, you could use a stored procedure to retrieve 50 rows at a time, instead of passing all the values directly to SSRS - caveat here is you wont be able to use the native next/previous page buttons for this.
A work around is to create custom links to loop back to the report itself with incremented parameters specifying an index row to start from.
Create a stored procedure that takes in a parameter which specifies the starting row from your table:
CREATE PROCEDURE dbo.usp_GetData
@RowNumber BIGINT
AS
BEGIN
DECLARE @FirstRow BIGINT
DECLARE @LastRow BIGINT
SET @FirstRow = @RowNumber
SET @LastRow = @RowNumber + 50
;WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY num) AS RowNumber
FROM dbo.TestTable
)
SELECT *
FROM CTE
WHERE RowNumber >= @FirstRow
AND RowNumber < @LastRow
END
GO
Create a stored procedure that retrieves the total rows from your table:
CREATE PROCEDURE dbo.usp_GetTotalRows
AS
BEGIN
SELECT COUNT(*) "TotalRows"
FROM dbo.TestTable
END
GO
Create the report and add the two datasets using the two stored procedures.
The @RowNumber
parameter should be automatically generated. You can set the default value to 1 to start the report from row 1.
Create two "custom" buttons on the report (effectively just links back to the same report). You could use text boxes for Previous/Next Page buttons.
For the "Previous Button" Text Box - Properties > Action > Go to report > Specify a report (select the name of your report name). Add Parameter and set the expression to:
=Parameters!RowNumber.Value-50
For the "Next Button" Text Box - Properties > Action > Go to report > Specify a report (select the name of your report name). Add Parameter and set the expression to:
=Parameters!RowNumber.Value+50
You can also change the visibility options for the buttons (such as hiding "Previous Page" button when Parameters!RowNumber.Value = 1
, or hiding the "Next Page" button when Parameters!RowNumber.Value + 50 >= DataSetName!TotalRows.Value
)
来源:https://stackoverflow.com/questions/39417833/ssrs-with-custom-paging