Home Database Mysql Tutorial SQL分页存储过程

SQL分页存储过程

Jun 07, 2016 pm 02:54 PM
sql code share Pagination storage newbie rookie process

新手,菜鸟,第一次分享代码,请多提意见,多指正。谢谢。 SQL 分页 存储过程 -- =============================================-- Author:xyy-- Create date: 2011-04-06-- Description:分页,所有参数中除了@strwhere中可以而带方括号([]),其他均不可以带--

新手,菜鸟,第一次分享代码,请多提意见,多指正。谢谢。 SQL 分页 存储过程
-- =============================================
-- Author:		xyy
-- Create date: 2011-04-06
-- Description:	分页,所有参数中除了@strwhere中可以而带方括号([]),其他均不可以带
-- =============================================
CREATE PROCEDURE [dbo].[sp_All_Pager]
	@tblName nvarchar(255),--表名
	@fldNames nvarchar(1000)='*',--字段列表(默认所有字段)
	@fldOrderName nvarchar(255),--主键字段(排序用)
	@orderType bit =0,--排序类型,非0则降序(默认升序)
	@pageIndex int=1,--页码
	@pageSize int =20,--页大小
	@strWhere nvarchar(1500)='',--查询条件,不带where
	@limitCount int =5000, --限制最多取5000条数据
	@totalCount int output --总记录数
AS
BEGIN
	SET NOCOUNT ON;
	declare @strSQL nvarchar(4000)--主语句
	declare @strTable nvarchar(500)--主查询临时表
	declare @strOrderSQL nvarchar(1000)--总数查询语句
	declare @strOutParam nvarchar(500)--输出参数
	declare @strTmp nvarchar(255)--临时分页字句 max 和min
	declare @strOrder nvarchar(255)--排序参数
		
	--设置排序
	if(@orderType != 0)
	begin
		set @strTmp = ' <(select min '
		set @strOrder = ' order by [' + @fldOrderName +'] desc'
	end
	else
	begin
		set @strTmp = ' >(select max '
		set @strOrder = ' order by [' + @fldOrderName +'] asc'
	end
	--设置主查询临时表
	if(@strWhere!='')
		set @strTable='(select top '+STR(@limitCount)+' '+@fldNames+' from ['+@tblName+'] where '+@strWhere+') as tmpTable'
    else
		set @strTable='(select top '+STR(@limitCount)+' '+@fldNames+' from ['+@tblName+']) as tmpTable'
	--设置总数查询语句和主语句
	set @strOrderSQL=N'select @totalCountOut=count(1) from '+@strTable
	set @strOutParam=N'@totalCountOut int output'
	if (@pageIndex = 1)
		set @strSQL = 'select top ' + str(@pageSize) +' '+@fldNames+ ' from ' + @strTable + @strOrder
	else
		set @strSQL = 'select top ' + str(@PageSize) +' '+@fldNames+ ' from ' + @strTable + ' where [' + @fldOrderName + ']' + @strTmp + '(['+ @fldOrderName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldOrderName + '] from ' + @strTable + @strOrder + ') as tblTmp) ' + @strOrder
	exec sp_executesql @strOrderSQL,@strOutParam,@totalCountOut=@totalCount output
	exec sp_executesql @strSQL
END

GO


Copy after login
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

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).

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the &quot;Share to&quot; option at the bottom, and then select the first &quot;WeChat Moments&quot; allows you to share content to WeChat Moments.

How to share files with friends on Baidu Netdisk How to share files with friends on Baidu Netdisk Mar 25, 2024 pm 06:52 PM

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the &quot;Shared Members&quot; column 】, and finally check all

Git installation process on Ubuntu Git installation process on Ubuntu Mar 20, 2024 pm 04:51 PM

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

Create and run Linux ".a" files Create and run Linux ".a" files Mar 20, 2024 pm 04:46 PM

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension &quot;.a&quot; have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run &quot;.a&quot; files. This article will introduce how to comprehensively install and configure the Linux &quot;.a&quot; file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux &quot;.a&quot; file. What is L

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 &quot;PowerPlatformTool&quot; and click the Install button

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

See all articles