简体   繁体   中英

Oracle DUAL table returns “no rows selected”

I got this query:

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;

returns all rows -

select BILLER_ONBOARDING_ID_SEQ.NEXTVAL from dual;

no rows selected

Why is this happening?

select BILLER_ONBOARDING_ID_SEQ.NEXTVAL from dual;

It might be possible that the dual table is empty. With SYSDB privilege, you could delete/truncate the SYS.DUAL table.

However, you will still get the sequence.nextval value even if dual doesn't return any row. The optimizer is somehow sure to always fetch a row from the dual table.

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

So, there are no rows in the dual table .

Let's create a sequence and test:

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

The explain plan shows that the FAST DUAL operation still fetches a row:

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>

NOTE : DUAL is a special table which is owned by SYS . It is a part of the data dictionary. It is not a good idea to manipulate the data dictionary. It is not recommended.

If the dual table has not been manipulated, then you should be able to see the following output from dba_objects :

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>

In your case, it might be that a custom table named DUAL has been created and which does not contain any row.

If that's the case, and that the SYS.DUAL has not been manipulated, then try using:

select BILLER_ONBOARDING_ID_SEQ.NEXTVAL from SYS.dual;

Try this,

select BILLER_ONBOARDING_ID_SEQ.NEXTVAL from SYS.dual;

You have a custom table DUAL in your database. And hence, the private synonym might point your version of table, instead of SYS.DUAL .

So, Querying DUAL with the schema name SYS , will solve tht.

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

This query would list a table that doesn't reside in SYS schema.

EDIT:

From comments, we could infer that you look for

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


On a Side note, PL/SQL Solution for the same could be like USING RETURNING clause with the INSERT.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM