ORA-01791: not a SELECTed expression 一个不是 bug 的 bug!

[[email protected] ~]$ !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

时间: 2024-10-10 10:53:38

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

ORA-01791: not a SELECTed expression

Student表有3个字段:id,name,age 看这两个SQL语句 (1)select name from student order by id; (2)select distinct(name) from student order by id; 执行结果你可能会说: 第1句返回以id排序的所有name字段 第2句返回以id排序的所有不重复的name字段. 但是执行结果不是这样的,第2句会报ORA-01791: not a SELECTed expression 原因: 一般来说,ord

vue示例之transition-另外发现一个vue(可能的)小bug

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="//cdn.bootcss.com/animate.css/3.5.2/animate.css" rel="stylesheet"> <style&

记一个界面刷新相关的Bug

今天遇到一个比较有意思的bug, 这里简单记录下. Bug的症状是通过拖拉边框把我们客户端主窗口拖小之后,再最大化,会发现窗口显示有问题, 看起来像是刷新问题, 有些地方显示的不对了. 这里要说明的是我这里的主窗口是非常复杂的窗口, 里面集成了很多组件(cpmponent),有很多层的子窗口. 这个问题只有在特定条件下才会发生, 正常情况下都是好的. 遇到这种问题,我们怎么处理? 首先当然是观察症状, 究竟是刷新问题, 还是Layout出错了. 我们可以通过Spy++查看窗口层次是不是正确, 窗

一个代价11万的bug

这个bug不是技术bug或者是程序bug,是典型的业务操作bug. 开发人员混淆了线上数据和本地测试数据,把线上数据切换到本地的数据做测试,结果对这些客户进行了资金调整...就导致了这个悲剧发生 早在这个事故之前,就一直有想法把业务权限和数据权限好好做一下,但是是一直有琐碎的事情就耽误了,终于还是导致可怕的事情发生了 万幸的时候,钱虽然到了用户的金额中,但是线上的数据被没有改变,用户看不到多出来的这笔钱,让我们还有时间去和支付公司协调解决这个问题. 一个代价11万的bug

关于一个困扰一晚上的bug -- java 返回值问题

问题 <span style="font-size:18px;"> tagged=tagData(data, tagger1, tagger2);//tagged是我NER出来的文档. //内容如The fate of [ORG Lehman Brothers] , Secretary [PER Henry M. Paulson Jr] . //注意上面,逗号和句号前面都有空格,我的目的就是要去掉他. OutFile out = new OutFile(outFiles.e

一个int类型引发的bug

一.引言 今天我在项目开发中,遭遇了一个莫名其妙的问题,概括加抽象后形成如下问题:在使用MyBatis的XML语句实现Dao层接口 List<Person> selectBySome(@Param("record") PersonExample example)时候,我写的XML中有这么一句代码: <if test="record.id!=null"> b.id=record.id </if> 结果我及时不对example的id赋

最近调的一个关于视频播放的小bug

上星期接到一个CQ,问题是这样的:下载官方的爱奇艺或者搜狐视频apk在板子上安装之后,无法正常播放在线视频,点击视频播放之后总是弹出对话框“XXX has stopped”,或者整个视频APP闪退. 于是我首先下载了一个爱奇艺的apk开始复现这个问题,然后从log进行分析,在kernel log发现线索: [ 392.674685] android: (01-02 01:17:32) process pid: 3352, tid: 4326 crash![ 392.682842] c0 4326

一个来自Afinal断点下载BUG的解决方案

欢迎各位加入我的Android开发群[257053751] 作为国内第一个Android开发框架Afinal,相信有很多开发者都知道的.虽然随着Android版本的迭代,其中有一些方法有了更好的解决办法作者也不再维护,但从来没有人怀疑Afinal的价值. 最近在重构KJFrameForAndroid框架的一个断点下载的功能,参考了比较多的例子,无意间发现了FinalHttp.download()方法中的一个BUG. 首先跟大家介绍一下afinal中download下载的实现原理.与其他众多下载方

关于一个非常非常无语的bug,与君共勉

今天,哦,不对,是昨天晚上,我花了大概四十分钟去找一个bug,结果还没找到 错误代码" $('#sendAreaInfo').citypicker('reset'); $('#sendAreaInfo').citypicker('destory'); 这段代码是,citypicker的前端控件重置文本框输入框的代码,结果我非常丢脸因为这个错误找去找了四十分钟还是没找出来.然后整个人就暴躁了,就在刚刚,静下心来,冷静地去分析了一下.发现了错误的原因 正确代码: $('#sendAreaInfo')