Mssql oracle paging query SQL

Source: Internet
Author: User

1. mssql

 

Use two TOP commands to obtain the paging data we want, for example:

Select * from
(
Select top ( PageSize

) * From
(
Select top ( PageSize

* PageIndex

)*
From articles
Order by pubtime DESC
)
Order by pubtime ASC
)
Order by pubtime DESC 2, Oracle

For Oracle databases, there are several differences that seriously impede the implementation of the above methods. For example, Oracle does not support the TOP Keyword: however, this does not seem very serious, because it provides the rownum implicit cursor, it can implement features similar to TOP, such:

Select top 10... from where...

To write

Select... from... where... and rownum <= 10

Rownum is the record number (1, 2, 3 ...), however, there is one troublesome thing: if the SQL statement contains ORDER... in sorting, rownum is actually sorted by "Number" first! In this way, this serial number is not applicable if it is not processed.

 

It can be divided into the following pages: (number Ora1)

Select * from
(
Select a. *, rownum R
From
(
SELECT *
FROM Articles
Order by PubTime DESC
, ArticleID -- // Add one or more fields (such as primary key fields,
ROWID) to make the sorting result unique




)
WHERE rownum <= PageUpperBound
) B
WHERE r> PageLowerBound;

The blue part can be changed to any SQL SELECT statement, which is convenient.

PS: Oracle

Of
Rownum

Not Supported
>,>=, =, Between... and

, Can only use the above symbols
(<

,
<=

,
! =)

Related Article

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.