zoukankan      html  css  js  c++  java
  • 伪列SQL Pseudocolumns

    ORACLE中常见的伪列的英文解释:

    官方网站:http://docs.oracle.com/cd/B10501_01/appdev.920/a96624/06_ora.htm#1990

    SQL Pseudocolumns

    PL/SQL recognizes the following SQL pseudocolumns, which return specific data items: CURRVAL, LEVEL, NEXTVAL, ROWID, and ROWNUM. Pseudocolumns are not actual columns in a table but they behave like columns. For example, you can select values from a pseudocolumn. However, you cannot insert into, update, or delete from a pseudocolumn. Also, pseudocolumns are allowed in SQL statements, but not in procedural statements.

    CURRVAL and NEXTVAL

    A sequence is a schema object that generates sequential numbers. When you create a sequence, you can specify its initial value and an increment. CURRVAL returns the current value in a specified sequence.

    Before you can reference CURRVAL in a session, you must use NEXTVAL to generate a number. A reference to NEXTVAL stores the current sequence number in CURRVAL. NEXTVAL increments the sequence and returns the next value. To obtain the current or next value in a sequence, you must use dot notation, as follows:

    sequence_name.CURRVAL
    sequence_name.NEXTVAL

    After creating a sequence, you can use it to generate unique sequence numbers for transaction processing. However, you can use CURRVAL and NEXTVAL only in a SELECT list, the VALUES clause, and the SET clause. In the following example, you use a sequence to insert the same employee number into two tables:

    INSERT INTO emp VALUES (empno_seq.NEXTVAL, my_ename, ...);
    INSERT INTO sals VALUES (empno_seq.CURRVAL, my_sal, ...);

    If a transaction generates a sequence number, the sequence is incremented immediately whether you commit or roll back the transaction.

    LEVEL

    You use LEVEL with the SELECT CONNECT BY statement to organize rows from a database table into a tree structure. LEVEL returns the level number of a node in a tree structure. The root is level 1, children of the root are level 2, grandchildren are level 3, and so on.

    In the START WITH clause, you specify a condition that identifies the root of the tree. You specify the direction in which the query walks the tree (down from the root or up from the branches) with the PRIOR operator.

    ROWID

    ROWID returns the rowid (binary address) of a row in a database table. You can use variables of type UROWID to store rowids in a readable format. In the following example, you declare a variable named row_id for that purpose:

    DECLARE
    row_id UROWID;

    When you select or fetch a physical rowid into a UROWID variable, you can use the function ROWIDTOCHAR, which converts the binary value to an 18-byte character string. Then, you can compare the UROWID variable to the ROWID pseudocolumn in the WHERE clause of an UPDATE or DELETE statement to identify the latest row fetched from a cursor. For an example, see "Fetching Across Commits".

    ROWNUM

    ROWNUM returns a number indicating the order in which a row was selected from a table. The first row selected has a ROWNUM of 1, the second row has a ROWNUM of 2, and so on. If a SELECT statement includes an ORDER BY clause, ROWNUMs are assigned to the retrieved rows before the sort is done.

    You can use ROWNUM in an UPDATE statement to assign unique values to each row in a table. Also, you can use ROWNUM in the WHERE clause of a SELECT statement to limit the number of rows retrieved, as follows:

    DECLARE
    CURSOR c1 IS SELECT empno, sal FROM emp
    WHERE sal > 2000 AND ROWNUM < 10; -- returns 10 rows

    The value of ROWNUM increases only when a row is retrieved, so the only meaningful uses of ROWNUM in a WHERE clause are

    ... WHERE ROWNUM < constant;
    ... WHERE ROWNUM <= constant;

    I believe that we are who we choose to be. Nobody‘s going to come and save you, you‘ve got to save yourself. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
  • 相关阅读:
    WIN10系统如何取消右下角的通知菜单,通知图标
    1.1.6版本Druid连接MSSQLServer 2008 R2报错The query timeout value -1 is not valid. #2210
    第一次使用Android Studio时你应该知道的一切配置(三):gradle项目构建
    CentOS 7 Tomcat服务的安装与配置
    centos7中使用yum安装tomcat以及它的启动、停止、重启
    centos7更改为启动桌面或命令行模式
    gradle新建工程,多项目依赖,聚合工程
    Gradle构建多模块项目(转)
    3分钟搞定SpringBoot+Mybatis+druid多数据源和分布式事务
    Spring MVC+Mybatis 多数据源配置
  • 原文地址:https://www.cnblogs.com/caroline/p/2307954.html
Copyright © 2011-2022 走看看