The SQL Server Stored Procedure returns both the paging result set and the total number. The stored procedure is paged.

Source: Internet
Author: User

The SQL Server Stored Procedure returns both the paging result set and the total number. The stored procedure is paged.

Preface

I haven't touched the database for a long time. I wrote a report stored procedure at home over the weekend. I don't know how to implement it better when I use the stored procedure to implement paging and calculate the total number of records. According to our normal business logic, stored procedure data is first divided by pages, followed by several query conditions. When the paging result set is returned, the total number of records must be returned to the client.

I will summarize such a business stored procedure as follows: 1. At the kernel layer, this is usually the query field or the field to be calculated. 2. query condition layer. If the kernel only queries some fields, the conditions can be spliced at the query condition layer. If the kernel layer is completely statistical business logic, the query conditions must be placed on the kernel layer, such as the SUM and GROUPBY services we commonly use. 3. Add the paging parameter (that is, we currently use ROW_NUMBER to add the rn parameter ). In the stored procedure, we usually declare the variables of each part for merging during execution.

Stored Procedure

CREATE proc [dbo]. [usp_manyidu] (@ seatno nvarchar (30), @ pageIndex int, @ pageSize int, @ rsCount int out) asbegin declare @ SQL nvarchar (max) -- concatenate the kernel SQL declare @ where nvarchar (max) = 'where 1 = 1' -- Query condition concatenation string declare @ cols nvarchar (max) -- query the field and calculated field declare @ sort nvarchar (50) -- sort set @ SQL = 'from dbo. log where seatno is not null and seatno <> ''' group by seatno' set @ cols = 'seatno, SUM (case when manyidu = 0 then 1 else 0 end) as manyi, SUM (case when manyidu = 1 then 1 else 0 end) as yiban, SUM (case when manyidu = 2 then 1 else 0 end) as bumanyi, SUM (case when manyidu IS null or manyidu = ''' then 1 else 0 end) as weipingjia 'set @ sort = 'order by seatno' if (@ seatno <> '') set @ where + = 'and seatno =' + @ seatno declare @ strSQL nvarchar (max) set @ strSQL = n' select * from (select ROW_NUMBER () over ('+ @ sort +') as tmpid, * from (select '+ @ cols + @ SQL +') as tmpTable1 '+ @ where + ') as tmpTable2) as tmpTable3 '+ 'where tmpid between' + STR (@ pageIndex-1) * @ pageSize + 1) + 'and' + STR (@ pageIndex * @ pageSize) print @ strSQL exec (@ strSQL) set @ strSQL = 'select @ total = count (*) from (select '+ @ cols + @ SQL + ') as tmpTable '+ @ where print @ strSQL exec sp_executesql @ strSQL, n' @ total int out', @ total = @ rsCount out endGO

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.