[ora11@lixora ~]$ !sql

sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on Wed Aug 27 09:50:54 2014

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select distinct sal, empno from scott.emp order by deptno;

SAL      EMPNO

---------- ----------

      2450       7782

      5000       7839

      1300       7934

      2975       7566

      3000       7902

      1100       7876

       800       7369

      3000       7788

      1250       7521

      1500       7844

      1600       7499

SAL      EMPNO

---------- ----------

       950       7900

      2850       7698

      1250       7654

14 rows selected.

------把 empno 换成 ename

SQL> select distinct sal,ename from scott.emp order by deptno;

select distinct sal,ename from scott.emp order by deptno

                                                  *

ERROR at line 1:

ORA-01791: not a SELECTed expression

-----把,ename,empno 都加到select 中:

SQL> select distinct sal,ename,empno from scott.emp order by deptno;

SAL ENAME                     EMPNO

---------- -------------------- ----------

      2450 CLARK                      7782

      5000 KING                       7839

      1300 MILLER                     7934

      2975 JONES                      7566

      3000 FORD                       7902

      1100 ADAMS                      7876

       800 SMITH                      7369

      3000 SCOTT                      7788

      1250 WARD                       7521

      1500 TURNER                     7844

      1600 ALLEN                      7499

SAL ENAME                     EMPNO

---------- -------------------- ----------

       950 JAMES                      7900

      2850 BLAKE                      7698

      1250 MARTIN                     7654

14 rows selected.

SQL> select distinct sal,empno,sal from scott.emp order by deptno;

SAL      EMPNO        SAL

---------- ---------- ----------

      2450       7782       2450

      5000       7839       5000

      1300       7934       1300

      2975       7566       2975

      3000       7902       3000

      1100       7876       1100

       800       7369        800

      3000       7788       3000

      1250       7521       1250

      1500       7844       1500

      1600       7499       1600

SAL      EMPNO        SAL

---------- ---------- ----------

       950       7900        950

      2850       7698       2850

      1250       7654       1250

14 rows selected.

这里为啥 empno 换成ename 后就无法运行了呢?

在做下下面測试:

---去掉empno 列上的主键

SQL> select distinct sal, empno from scott.t_emp order by deptno;

select distinct sal, empno from scott.t_emp order by deptno

                                                     *

ERROR at line 1:

ORA-01791: not a SELECTed expression

---加入empno 列上的主键

SQL> alter table t_emp add constraint pk_t_emp primary key(empno) ;

Table altered.

SQL> desc t_emp

 Name                                                                                Null?    Type

 ----------------------------------------------------------------------------------- -------- --------------------------------------------------------

 EMPNO                                                                               NOT NULL NUMBER(4)

 ENAME                                                                                        VARCHAR2(10)

 JOB                                                                                          VARCHAR2(9)

 MGR                                                                                          NUMBER(4)

 HIREDATE                                                                                     DATE

 SAL                                                                                          NUMBER(7,2)

 COMM                                                                                         NUMBER(7,2)

 DEPTNO                                                                                       NUMBER(2)

SQL> select distinct sal, empno from scott.t_emp order by deptno;

SAL      EMPNO

---------- ----------

      2450       7782

      5000       7839

      1300       7934

      2975       7566

      3000       7902

      1100       7876

       800       7369

      3000       7788

      1250       7521

      1500       7844

      1600       7499

       950       7900

      2850       7698

      1250       7654

14 rows selected.

问题总结:

至于为什么会有这个不是bug 的bug 存在,事实上是开发给挖的坑,然后让 dba 往里跳:

 

FYI:

ORA-01791: not a SELECTed expression after upgrade to 11.2.0.4 (Doc ID 1600974.1)

http://docs.oracle.com/cd/E11882_01/server.112/e10592/statements_10002.htm#SQLRF20039

##########################################################

Restrictions on the ORDER BY Clause The following restrictions apply to the ORDER BY clause:

•If you have specified the DISTINCT operator in this statement, then this clause cannot refer to columns unless they appear in the select list.

•An order_by_clause can contain no more than 255 expressions.

•You cannot order by a LOB, LONG, or LONG RAW column, nested table, or varray.

•If you specify a group_by_clause in the same statement, then this order_by_clause is restricted to the following expressions:

◦Constants

◦Aggregate functions

◦Analytic functions

◦The functions USER, UID, and SYSDATE

◦Expressions identical to those in the group_by_clause

◦Expressions comprising the preceding expressions that evaluate to the same value for all rows in a group

##################################################################

ORA-01791: not a SELECTed expression after upgrade to 11.2.0.4 (Doc ID 1600974.1)  To Bottom

 

________________________________________

 

In this Document

 Symptoms

Changes

Cause

Solution

References

________________________________________

APPLIES TO:

Oracle Database - Enterprise Edition - Version 11.2.0.4 and later

Information in this document applies to any platform.

SYMPTOMS

a select DISTINCT query and the order by column does not reference a select list item after upgrade to 11.2.0.4

SQL> select distinct sal, empno from scott.emp order by deptno;

select distinct sal, empno from scott.emp order by deptno

                                                   *

ERROR at line 1:

ORA-01791: not a SELECTed expression

But it was working on previous release ..

SQL> select distinct sal, empno from scott.emp order by deptno;

SAL      EMPNO

---------- ----------

      2450       7782

      5000       7839

 

CHANGES

 upgrade to 11.2.0.4

CAUSE

 The issue have been investigated in the following bug:

Bug:17655864 - ORA-01791: NOT A SELECTED EXPRESSION AFTER 11.2.0.4 PATCH

which is closed as not a bug. and this is expected behvior .

so the correct behavior is on 11.2.0.4 and not older versions.

This is due to

BUG 13768663 - SELECT WORKS IN 10.2 AND 11.2.0.3 FAILS 11.1.0.7 ORA-01791

Invalid query which should raise ORA-1791 is working fine without any error starting from 11.2.0.1.This is fixed in 11.2.0.4 and hence you may get the error ORA-1791 in 11.2.0.4.

SOLUTION

The expected behaviour for this statement is that it should report ORA-01791 That is, this is a select DISTINCT query and the order by column does not reference a select list item. This is a documented restriction of the order by clause.

http://docs.oracle.com/cd/E11882_01/server.112/e10592/statements_10002.htm#SQLRF20039

 

This behaviour is corrected through bugfix 13768663.

so please add the orderby column in the select statement

SQL> select distinct sal, empno, deptno from scott.emp order by deptno;

SAL      EMPNO     DEPTNO

---------- ---------- ----------

      2450       7782         10

      5000       7839         10

      1300       7934         10

REFERENCES

BUG:17655864 - ORA-01791: NOT A SELECTED EXPRESSION AFTER 11.2.0.4 PATCH

NOTE:13768663.8 - Bug 13768663 - ORA-1791 not reported in 11.2 when expected

BUG:13768663 - SELECT WORKS IN 10.2 AND 11.2.0.3 FAILS 11.1.0.7 ORA-01791

版权声明:本文博主原创文章,博客,未经同意,不得转载。

ORA-01791: not a SELECTed expression 一种是不 bug 的 bug!的更多相关文章

  1. ORA-01791: not a SELECTed expression

    Student表有3个字段:id,name,age 看这两个SQL语句 (1)select name from student order by id; (2)select distinct(name ...

  2. 基于Keil C的覆盖分析,总结出编程中可能出现的几种不可预知的BUG

    基于Keil C的覆盖分析,总结出编程中可能出现的几种不可预知的BUG,供各位网友参考 1.编译时出现递归警告,我看到很多网友都采用再入属性解决,对于再入函数,Keil C不对它进行覆盖分析,采用模拟 ...

  3. ORA 各种oraclesql错误

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  4. Oracle的tnsnames.ora配置(PLSQL Developer)

    首先打开tnsnames.ora的存放目录,一般为D:\app\Administrator\product\11.2.0\client_1\network\admin,就看安装具体位置了. 步骤阅读 ...

  5. select选中获取索引三种写法

    $('#someId').prop('selectedIndex'); $('option:selected', '#someId').index(); $('#someId option').ind ...

  6. 不可不知的表达式树(1)Expression初探

    说起Lambda表达式,大家基本都很熟悉了,而表达式树(Expression Trees),则属于80%的工作中往往都用不到的那种技术,所以即便不是什么新技术,很多人对其理解都并不透彻.此文意图从表达 ...

  7. 转载《Oracle的tnsnames.ora配置(PLSQL Developer)》

    源地址:https://www.cnblogs.com/qq3245792286/p/6212617.html. 首先打开tnsnames.ora的存放目录,一般为D:\app\Administrat ...

  8. 常见的几种 CSS 水平垂直居中解决办法

    用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂 ...

  9. 【密码】Oracle用户密码系列

    [密码]Oracle用户密码系列 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识, ...

随机推荐

  1. elf 文件格式探秘——程序运行背后的故事

    摘要:本文主要讲解elf文件格式,通过readelf命令结合底层的相关数据结构,讲解相关内容,分析程序运行的基本原理. 本文来源:elf 文件格式探秘——程序运行背后的故事 http://blog.c ...

  2. ThreadSafeClientConnManager的20个例子

    Java Code Examples for org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager The following are ...

  3. Android应用开发-小巫CSDN博客clientJsoup篇

    Android应用开发-小巫CSDN博客clientJsoup篇 距上一篇博客已经过去了两个星期,小巫也认为很抱歉,由于在忙着做另外一个项目,差点儿抽不出空来,这不小巫会把剩下的博文全部在国庆补上.本 ...

  4. 使用 DBMS_REPAIR 修复坏块

    对于Oracle数据块物理损坏的情形,在我们有备份的情况下可以直接使用备份来恢复.对于通过备份恢复,Oracel为我们提供了很多种方式,冷备,基于用户管理方式,RMAN方式等等.对于这几种方式我们需要 ...

  5. javaEE jdbc编程步骤

    1.载入数据库驱动(jar文件) //须要下载一个数据库的jar包,并导入对应的JDBC项目中,创建路径! Class.forName("com.mysql.jdbc.Driver" ...

  6. Xamarin.Android中使用android:onClick="xxx"属性

    原文:Xamarin.Android中使用android:onClick="xxx"属性 在原生Android开发中,为一个View增加点击事件,有三种方式: 1.使用匿名对象 ( ...

  7. web网站架构

    浅谈web网站架构演变过程   前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变.   该系统具备的功能:   用户模块:用户注册和管理 商品模块:商品展示和管 ...

  8. Asp.net 视频摘要

    Asp.net一遍又一遍视频最近,例如中,大多数的实现.由于原因的版本号,.当然学过是学过.总结不可缺少. 先宏观后微观.刚学完,感觉知识特别乱,所以先画了张图: watermark/2/text/a ...

  9. 基于最简单的FFmpeg采样读取内存读写:存储转

    ===================================================== 基于最简单的FFmpeg样品系列读写内存列表: 最简单的基于FFmpeg的内存读写的样例:内 ...

  10. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...