Stored procedure Code for three kinds of SQL paging queries _mssql

Source: Internet
Author: User
Copy Code code as follows:

--based on Max (MIN) ID
CREATE PROC [dbo]. [PROC_SELECT_ID]
@pageindex int=1,--Current pages
@pagesize int=10,--per page size
@tablename VARCHAR = ',--table name
@fields VARCHAR (1000) = ',--a collection of fields for a query
@keyid VARCHAR = ',--primary key
@condition NVARCHAR (1000) = ',--query criteria
@orderstr VARCHAR,--Sorting criteria
Total records of @totalRecord BIGINT output--
As
IF ISNULL (@orderstr, N ') =n ' SET @orderstr =n ' ORDER by ' + @keyid +n ' DESC '
IF ISNULL (@fields, N ') =n ' SET @fields =n ' * '
IF ISNULL (@condition, N ') =n ' SET @condition =n ' 1=1 '
DECLARE @sql NVARCHAR (4000)
--if (@totalRecord is NULL)
--begin
SET @sql =n ' SELECT @totalRecord =count (*) '
+n ' from ' + @tablename
+n ' WHERE ' + @condition
EXEC sp_executesql @sql, N ' @totalRecord INT output ', @totalRecord output
--end
IF (@pageindex =1)
BEGIN
SET @sql =n ' SELECT top ' +str (@pagesize) +n ' + @fields +n ' from ' + @tablename +n ' WHERE ' + @condition +n ' + @orderstr
EXEC (@sql)
End
ELSE
BEGIN
DECLARE @operatestr char (3), @comparestr char (1)
SET @operatestr = ' MAX '
SET @comparestr = ' > '
IF (@orderstr <> ")
BEGIN
IF (CHARINDEX (' desc ', LOWER (@orderstr)) <>0)
BEGIN
SET @operatestr = ' MIN '
SET @comparestr = ' < '
End
End
SET @sql =n ' SELECT top ' +str (@pagesize) +n ' + @fields +n ' from ' + @tablename +n ' WHERE ' + @keyid + @comparestr
+n ' (SELECT ' + @operatestr +n ' (' + @keyid +n ') from ' + @tablename +n ' WHERE ' + @keyid
+n ' In (@pageindex-1) * @pagesize) +n "+ @keyid +n ' from ' + @tablename +n ' WHERE '
+ @condition +n ' + @orderstr +n ')) and ' + @condition +n ' + @orderstr
EXEC (@sql)
End
Go


--According to Row_number () over
CREATE PROC [dbo]. [Proc_select_page_row]
@pageindex int=1,--Current pages
@pagesize int=10,--per page size
@tablename VARCHAR = ',--table name
@fields VARCHAR (1000) = ' * ',--a collection of fields for a query
@keyid VARCHAR = ',--primary key
@condition NVARCHAR (1000) = ',--query criteria
@orderstr VARCHAR,--Sorting criteria
Total records of @totalRecord BIGINT output--
As
IF ISNULL (@orderstr, N ') =n ' SET @orderstr =n ' ORDER by ' + @keyid +n ' DESC '
IF ISNULL (@fields, N ') =n ' SET @fields =n ' * '
IF ISNULL (@condition, N ') =n ' SET @condition =n ' 1=1 '
DECLARE @sql NVARCHAR (4000)
--IF @totalRecord is NULL
--BEGIN
SET @sql =n ' SELECT @totalRecord =count (*) '
+n ' from ' + @tablename
+n ' WHERE ' + @condition
EXEC sp_executesql @sql, N ' @totalRecord bigint output ', @totalRecord output
--end
IF (@pageindex =1)
BEGIN
SET @sql =n ' SELECT top ' +str (@pagesize) +n ' + @fields +n ' from ' + @tablename +n ' WHERE ' + @condition +n ' + @orderstr
EXEC (@sql)
End
ELSE
BEGIN
DECLARE @StartRecord INT
SET @StartRecord = (@pageindex-1) * @pagesize + 1
SET @sql =n ' select * FROM (select Row_number () over (' + @orderstr +n ') as RowId, ' + @fields +n ' from ' + @tablename +n ') as T WH ERE rowid>= ' +str (@StartRecord) +n ' and rowid<= ' +str (@StartRecord + @pagesize-1)
EXEC (@sql)
End
Go


--based on top ID
CREATE PROC [dbo]. [Proc_select_page_top]
@pageindex int=1,--Current pages
@pagesize int=10,--per page size
@tablename VARCHAR = ',--table name
@fields VARCHAR (1000) = ',--a collection of fields for a query
@keyid VARCHAR = ',--primary key
@condition NVARCHAR (1000) = ',--query criteria
@orderstr VARCHAR,--Sorting criteria
Total records of @totalRecord BIGINT output--
As
IF ISNULL (@orderstr, N ') =n ' SET @orderstr =n ' ORDER by ' + @keyid +n ' DESC '
IF ISNULL (@fields, N ') =n ' SET @fields =n ' * '
IF ISNULL (@condition, N ') =n ' SET @condition =n ' 1=1 '
DECLARE @sql NVARCHAR (4000)
--if (@totalRecord is NULL)
--begin
SET @sql =n ' SELECT @totalRecord =count (*) '
+n ' from ' + @tablename
+n ' WHERE ' + @condition
EXEC sp_executesql @sql, N ' @totalRecord INT output ', @totalRecord output
--end
IF (@pageindex =1)
BEGIN
SET @sql =n ' SELECT top ' +str (@pagesize) +n ' + @fields +n ' from ' + @tablename +n ' WHERE ' + @condition +n ' + @orderstr
EXEC (@sql)
End
ELSE
BEGIN
SET @sql =n ' SELECT top ' +str (@pagesize) +n ' + @fields +n ' from ' + @tablename +n ' WHERE ' + @keyid
+n ' +str ((@pageindex-1) * @pagesize) +n ' + @keyid +n ' from '
+ @tablename +n ' WHERE ' + @condition +n ' + @orderstr +n ') and ' + @condition +n ' + @orderstr
EXEC (@sql)
End
Go

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.