Oracle DUAL表返回“没有选择行”

时间:2015-05-20 06:50:48

标签: java oracle hibernate oracle11g sequence

我收到了这个问题:

insert into biller_onboarding_tbl values(BILLER_ONBOARDING_ID_SEQ.NEXTVAL,'1','blah','j', '1','dsad','das','dasd','dsad','dasd','dasd','dsadsa')

1 rows inserted.

select * from BILLER_ONBOARDING_TBL;

返回所有行 -

select BILLER_ONBOARDING_ID_SEQ.NEXTVAL from dual;

no rows selected

为什么会这样?

2 个答案:

答案 0 :(得分:2)

  

从双重选择BILLER_ONBOARDING_ID_SEQ.NEXTVAL;

双表可能是空的。凭借ParseUser.getCurrentUser().saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { if (null == e) { photo.put("user", ParseUser.getCurrentUser()); photo.pin(); } } }); 权限,您可以删除/截断 SYS.DUAL 表。

但是,即使dual不返回任何行,您仍将获得SYSDB值。 优化器在某种程度上确保始终从双表中获取行

sequence.nextval

因此,双重表中有无行

让我们创建序列并测试:

SQL> conn sys@pdborcl as sysdba
Enter password:
Connected.
SQL> show user
USER is "SYS"
SQL> select * from dual;

D
-
X

SQL> delete from dual;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select * from dual;

no rows selected

解释计划显示 FAST DUAL 操作仍会提取一行:

SQL> create sequence s;

Sequence created.

SQL> select s.nextval from dual;

   NEXTVAL
----------
         1

SQL> select * from dual;

no rows selected

SQL> select s.nextval from dual;

   NEXTVAL
----------
         2

注意 DUAL 是一个特殊的表,由 SYS 拥有。它是数据字典的一部分。操纵数据字典不是一个好主意。不建议这样做。

如果双表未被操作,那么您应该能够从 dba_objects 中看到以下输出:

SQL> explain plan for select s.nextval from dual;

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------
Plan hash value: 3499163060

-----------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Cost (%CPU)| Time     |
-----------------------------------------------------------------
|   0 | SELECT STATEMENT |      |     1 |     2   (0)| 00:00:01 |
|   1 |  SEQUENCE        | S    |       |            |          |
|   2 |   FAST DUAL      |      |     1 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------

9 rows selected.

SQL>

在您的情况下,可能是已创建名为DUAL的自定义表,并且该表不包含任何行。

如果是这种情况,并且尚未操纵SYS.DUAL,请尝试使用:

SQL> column owner format a15
SQL> column object_name format a15
SQL> column object_type format a15
SQL> SELECT owner, object_name, object_type FROM dba_objects WHERE object_name='DUAL';

OWNER           OBJECT_NAME     OBJECT_TYPE
--------------- --------------- ---------------
SYS             DUAL            TABLE
PUBLIC          DUAL            SYNONYM

SQL>

答案 1 :(得分:2)

试试这个,

select BILLER_ONBOARDING_ID_SEQ.NEXTVAL from SYS.dual;

您的数据库中有自定义DUAL。因此,私有同义词可能指向您的表格版本,而不是SYS.DUAL

因此,使用架构名称DUAL查询SYS将解决此问题。

SELECT OWNER,TABLE_NAME FROM ALL_TABLES WHERE TABLE_NAME = 'DUAL';

此查询将列出驻留在SYS架构中的表。

修改

根据评论,我们可以推断您正在寻找

Cannot retrieve the id of the last inserted row in Hibernate using Oracle

在附注中,PL/SQL解决方法与使用INSERT的US RETURNING子句类似。

SET SERVEROUTPUT ON
DECLARE
  l_id t1.id%TYPE;
BEGIN
  INSERT INTO t1 VALUES (t1_seq.nextval, 'FOUR')
  RETURNING id INTO l_id;
  COMMIT;

  DBMS_OUTPUT.put_line('ID=' || l_id);
END;
/
ID=4

PL/SQL procedure successfully completed.

SQL>