开发者

Query to output values which are not present in table

Need help in Oracle query

Requirement:

I have 5 rows in a table lets say ID = 1, 2, 3, 4, 5

Requirement is as such that user may pass any value as input and if that value is not there in table then query should return me the value which is not present.

Ex:

1. If user passes 9 then Oracle query should return the output as 9

2. If user passes 1,2,10 then Oracle query should return the output as 10

as 9 and 10 in above example are not 开发者_JAVA技巧in table.

I am using following query but not getting result.

SELECT ID
FROM TABLE_NAME WHERE ID NOT IN
(SELECT ID 
FROM TABLE_NAME where ID NOT in (1,2,10))


create table z (id number);
Table created.

SQL> insert into z values (1);
1 row created.
SQL> insert into z values (2);
1 row created.
SQL> insert into z values (3);
1 row created.
SQL> insert into z values (4);
1 row created.
SQL> insert into z values (5);
1 row created.

SQL> select 10 id from dual
  2  minus
  3  select id from z;

    ID
----------
    10


You could use a nested table as input:

SQL> CREATE TABLE table_name (ID NUMBER NOT NULL); 

Table created

SQL> INSERT INTO table_name (SELECT ROWNUM FROM dual CONNECT BY LEVEL <= 5); 

5 rows inserted

SQL> CREATE TYPE tab_number AS TABLE OF NUMBER;
  2  / 

Type created

SQL> SELECT *
  2    FROM TABLE(tab_number(1,2,10)) x
  3   WHERE x.column_value NOT IN (SELECT ID FROM table_name);

COLUMN_VALUE
------------
          10
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜