DB2 SQL query result set automatic numbering, return bar number range control research

Source: Internet
Author: User
Tags ibm db2

DB2 SQL query result set automatic numbering, return bar number range control Research Summary:the DB2 SQL statement implements the record of the query automatically numbering, and further realizes the record range control according to the number, the paging query. Environment:IBM DB2 Universal V9.1Windows XP ProfessionalRequirements: Write an SQL statement, in the query results to display the record sequence number, this sequence number is automatically generated according to the order of record. Test Initialize the DB2 SQL script: drop table t_test;CREATE TABLE T_test(BS bigint NOT NULL is NULL generated by default as identity,username varchar () not NULL,password varchar (+),remark varchar ($) );comment on T_test(BS is ' logo ',username is ' user name ',password is ' password ',remark is ' remarks ' );INSERT into t_test (username, password) VALUES (' AAA ', ' AAA '),(' BBB ', ' BBB '),(' CCC ', ' CCC '),(' ddd ', ' ddd '),(' eee ', ' eee '),(' FFF ', ' FFF '),(' GGG ', ' GGG '); implementation:1, the implementation of the query record numberRequirements: Query BS is not equal to 1 of all records. Select T.*, Row_number () over () as Row_noFrom t_test Twhere t.bs <> 1The query results are as follows:BS username password remark Row_no---------------------------------------2 ' BBB ' BBB ' [Null] ' 13 ' CCC ' CCC ' [Null] ' 24 ' ddd ' DDD ' [Null] ' 35 ' eee ' eee ' [Null] ' 46 ' FFF ' FFF ' [Null] ' 57 ' ggg ' GGG ' [Null] ' 6 Description: Row_number () over () is used to generate a numbered sequence, numbering starting from 1, as an integer. As Row_no sets the display name for the numbered column. 2, the realization according to the record scope inquiryRequirements: Query 第2-5条 records are required. SELECT *From (select T.*, Row_number () over () as Row_noFrom T_test t) as Wwhere Row_no >= 2and row_no <=5The query results are as follows:BS username password remark Row_no---------------------------------------2 ' BBB ' BBB ' [Null] ' 23 ' CCC ' CCC ' [Null] ' 34 ' ddd ' DDD ' [Null] ' 45 ' eee ' eee ' [Null] ' 53. Implementing SQL Paging Queryto extend the SQL in 2, you can implement a paged query by passing the range start number as a variable to SQL. Of course, this also requires the support of the program, master this principle is very easy, do not introduce. Summary:The control of record return is the characteristic of database system, and different database systems have different implementations. SQL is generally best for controlling the efficiency of result set records directly. Combined with the advanced programming language, complex paging queries can be implemented. ---------finished---------

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.