Home Database Mysql Tutorial Transact-SQL 示例

Transact-SQL 示例

Jun 07, 2016 pm 05:45 PM
dynamic external Splicing Example transfer

任务需求是这样的,定义一个存储过程可以根据设定的存储过程参数去查询某个表返回可以分页的,参数的功能如下: 1.返回记录集的第n页(@pageNo) 2.每页显示n条记录(@pageSize) 3.筛选条件(@where) 4.排序规则(@orderby) 5.总记录数(@rows) Ok,在拿

任务需求是这样的,香港虚拟主机,定义一个存储过程可以根据设定的存储过程参数去查询某个表返回可以分页的,参数的功能如下:
1.返回记录集的第n页(@pageNo)
2.每页显示n条记录(@pageSize)
3.筛选条件(@where)
4.排序规则(@orderby)
5.总记录数(@rows)

Ok,在拿到需求后,香港服务器,便开始分析。。。这里需要使用到拼接sql字符串变量然后Exec @sql即可,美国空间,但是实践发现。Exec @sql并不能把总记录数传递给外部的@rows OUTPUT参数上,为此stackoverflow一番发现答案。

下面是本人目前的做法: 

--定义存储过程 -- 示例过程中使用每个数据库都会自带的系统视图sys.objects作为测试用表 CREATE PROCEDURE dbo.Demo1 @pageNo int = 1, @pageSize int = 10, @where nvarchar(1000) = N'', @orderby nvarchar(1000) = N'name asc', @rows int OUTPUT AS BEGIN DECLARE @sql nvarchar(max) SET @sql = N'SELECT @rowsInner = COUNT(1) FROM sys.objects' IF @where N'' SET @sql = @sql + N' WHERE ' + @where SET @sql = @sql + N'; SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderby + N') RowId, object_id, name FROM sys.objects ' IF (@where N'') SET @sql = @sql + N'WHERE ' + @where SET @sql = @sql + N' ) a WHERE a.RowId between (@pageNoInner - 1) * @pageSizeInner + 1 AND @pageNoInner * @pageSizeInner' --若要调试请撤销如下代码的注释并注释处 --SET @rows = 100 --print @sql -- EXEC sp_executesql @sql, N'@pageNoInner int, @pageSizeInner int, @rowsInner int OUTPUT', @pageNoInner = @pageNo, @pageSizeInner = @pageSize, @rowsInner = @rows OUTPUT -- END

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)

Convert VirtualBox fixed disk to dynamic disk and vice versa Convert VirtualBox fixed disk to dynamic disk and vice versa Mar 25, 2024 am 09:36 AM

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

This workbook contains links to one or more external sources that may be unsafe This workbook contains links to one or more external sources that may be unsafe Feb 26, 2024 am 11:07 AM

Does Excel continue to throw This workbook contains links to one or more external sources that may display an unsafe warning message when opening the workbook? Many users have reported that they receive this warning whenever they open an Excel file. Although this warning indicates that there are potentially malicious links in the workbook, it may be triggered even if you have included a trusted external source. This workbook contains links to one or more potentially unsafe external sources if you open Excel. The warning "This workbook contains links to one or more external sources that may be unsafe" appears when you open a file. You can try the following solutions to resolve the issue: Check external links in the workbook and remove untrusted links. . Use the edit link feature

Introduction to Python functions: Introduction and examples of exec function Introduction to Python functions: Introduction and examples of exec function Nov 03, 2023 pm 02:09 PM

Introduction to Python functions: Introduction and examples of exec function Introduction: In Python, exec is a built-in function that is used to execute Python code stored in a string or file. The exec function provides a way to dynamically execute code, allowing the program to generate, modify, and execute code as needed during runtime. This article will introduce how to use the exec function and give some practical code examples. How to use the exec function: The basic syntax of the exec function is as follows: exec

Go language indentation specifications and examples Go language indentation specifications and examples Mar 22, 2024 pm 09:33 PM

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

Oracle DECODE function detailed explanation and usage examples Oracle DECODE function detailed explanation and usage examples Mar 08, 2024 pm 03:51 PM

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

Introduction to Python functions: Usage and examples of isinstance function Introduction to Python functions: Usage and examples of isinstance function Nov 04, 2023 pm 03:15 PM

Introduction to Python functions: Usage and examples of the isinstance function Python is a powerful programming language that provides many built-in functions to make programming more convenient and efficient. One of the very useful built-in functions is the isinstance() function. This article will introduce the usage and examples of the isinstance function and provide specific code examples. The isinstance() function is used to determine whether an object is an instance of a specified class or type. The syntax of this function is as follows

Introduction to Python functions: functions and examples of eval function Introduction to Python functions: functions and examples of eval function Nov 04, 2023 pm 12:24 PM

Introduction to Python functions: functions and examples of the eval function In Python programming, the eval function is a very useful function. The eval function can execute a string as program code, and its function is very powerful. In this article, we will introduce the detailed functions of the eval function, as well as some usage examples. 1. Function of eval function The function of eval function is very simple. It can execute a string as Python code. This means that we can convert a string

Introduction to Python functions: functions and examples of sorted function Introduction to Python functions: functions and examples of sorted function Nov 03, 2023 pm 02:47 PM

Introduction to Python functions: functions and examples of sorted functions Python is a very powerful programming language with a wealth of built-in functions and modules. In this series of articles, we will introduce the commonly used functions of Python one by one and provide corresponding examples to help readers better understand and apply these functions. This article will introduce the functions and examples of the sorted function in detail. The sorted function is used to sort an iterable object and return a new sorted list. Can be used for numbers and words

See all articles