Home Database Mysql Tutorial 两款sql 分页存储过程代码

两款sql 分页存储过程代码

Jun 07, 2016 pm 05:47 PM
sql Two types code Pagination storage process

文章收藏了两款sql 分页存储过程代码,这二款存储过程是二款高效分页存储过程代码,如果你觉得自己写的语句不够,强的话,可以利用我们现成的高效分页存储过程实例代码。

文章收藏了两款sql 代码,这二款存储过程是二款高效分页存储过程代码,如果你觉得自己写的语句不够,强的话,可以利用我们现成的高效分页存储过程实例代码。

create procedure pages
@tablenames varchar(200), --表名,可以是多个表,但不能用别名
@primarykey varchar(100), --主键,可以为空,但@order为空时该值不能为空
@fields varchar(800), --要取出的字段,可以是多个表的字段,可以为空,为空表示select *
@pagesize int, --每页记录数
@currentpage int, --当前页,0表示第1页
@filter varchar(200) = '', --条件,可以为空,不用填 where
@order varchar(200) = '' --排序,可以为空,为空默认按主键升序排列,不用填 order by
as
begin
declare @toprow varchar(12)
declare @temppagesize varchar(12)
if(len(@order)>0)
begin
set @order=' order by '+@order
end
else
begin
set @order=''
end
if (len(@filter) begin
set @filter=' 1=1'
end
if(@currentpage-1 set @currentpage=0
set @toprow= rtrim(ltrim(str(@pagesize*(@currentpage-1))))
set @temppagesize= rtrim(ltrim(str(@pagesize)))
exec('
declare @temptable table(rownum int identity(1,1),gid varchar(12))'+'
declare @datatable table(gid varchar(12))'+'
declare @timer datetime'+'
set @timer = getdate() '+'
set nocount on '+'
insert into @temptable(gid) select '+@primarykey+' from '+@tablenames+' where '+@filter+@order+'
insert into @datatable(gid) select top '+@temppagesize+'gid from @temptable where rownum>'+@toprow+'
set rowcount '+@temppagesize+'

select '+@fields+' from '+@tablenames+' where '+@filter+' and '+@primarykey+' in (select gid from @datatable)'+@order+'
set rowcount 0'+'
print(datediff(millisecond, @timer, getdate()))')
print('insert into @temptable(gid) select '+@primarykey+' from '+@tablenames+' where '+@filter+@order)
print('select '+@fields+' from '+@tablenames+' where '+@filter+' and '+@primarykey+' in(select gid from @datatable) '+@order)
end
go


实例代码二

 

alter procedure [dbo].[common_procedure_selectwithpage]
@sql varchar(5000),
@currentpageno int,
@pagesize int,
@totalnum int output
as
set nocount on
declare @sqlcmd varchar(5000)
------------------------------------------ --查询数据
set @sqlcmd = 'select * from (' + @sql + ') a where rowindex between ' + convert(varchar,(@currentpageno-1) * @pagesize + 1) + ' and ' + convert(varchar,@currentpageno * @pagesize)
exec(@sqlcmd) print (@sqlcmd)
------------------------------------------ --求记录总数
if @totalnum = -1
begin
create table #temp1(num int)
insert into #temp1
exec('select count(*) from (' + @sql + ') a')
select @totalnum=(select * from #temp1)
drop table #temp1
end

用法很简单,但必须在传入的sql中使用row_number() over(...) as rowindex :
declare @sql varchar(5000)
declare @currentpageno int
declare @pagesize int
declare @totalnum int

set @currentpageno = 100
set @pagesize = 10
set @totalnum = -1
set @sql = ' select *, row_number() over (order by 排序字段) as rowindex from 表名 a with (nolock) '

exec [dbo].[common_procedure_selectwithpage] @sql,@currentpageno,@pagesize,@totalnum output

select @totalnum

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Mar 07, 2024 pm 10:43 PM

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.

How to use Copilot to generate code How to use Copilot to generate code Mar 23, 2024 am 10:41 AM

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button

See all articles