Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Select in Select

Re: Select in Select

From: fumi <fumi_at_tpts5.seed.net.tw>
Date: 19 Oct 1999 16:28:07 GMT
Message-ID: <7ui66n$s89$2@news.seed.net.tw>

Bessenyei Zsolt <bessenyei_at_flexum.hu> wrote in message news:380AFD1B.CEB99D41_at_flexum.hu...
> Hello,
> When I run this query:
> select field1, (select field2 from table2 where field3=1) from table1
> this error message appears:
> ORA-00936: missing expression
> Exactly the same query runs well on Sybase SQL Anywhere and MSSQL.
> How can I use a SELECT as a field ?
> Zsolt Bessenyei

This feature is ONLY available in Oracle8i, and it's undocumented. So many persons don't believe this statement can work in Oracle. You can do this in Oracle8i:

SQL> select ename, (select dname from dept where deptno=20) from emp;

ENAME (SELECTDNAMEFR
---------- --------------

SMITH      RESEARCH
ALLEN      RESEARCH
WARD       RESEARCH
JONES      RESEARCH
MARTIN     RESEARCH
BLAKE      RESEARCH
CLARK      RESEARCH
SCOTT      RESEARCH
KING       RESEARCH
TURNER     RESEARCH
ADAMS      RESEARCH
JAMES      RESEARCH
FORD       RESEARCH
MILLER     RESEARCH

14 rows selected.

In Oracle8.0 and prior, you must use an alternative way:

SQL> select ename, dname
  2 from emp, (select dname from dept where deptno=20);

ENAME DNAME
---------- --------------

SMITH      RESEARCH
ALLEN      RESEARCH
WARD       RESEARCH
JONES      RESEARCH
MARTIN     RESEARCH
BLAKE      RESEARCH
CLARK      RESEARCH
SCOTT      RESEARCH
KING       RESEARCH
TURNER     RESEARCH
ADAMS      RESEARCH
JAMES      RESEARCH
FORD       RESEARCH
MILLER     RESEARCH

14 rows selected.

The subquery can only return one record in the above examples. If the subquery contains multiple records, you must use the folloing way (only for Oracle8i):

SQL> create or replace type number_nt as table of number;   2 /

Type created.

SQL> select dname, cast(multiset(select empno from emp where deptno=d.deptno) as number_nt)
  2 from dept d;

DNAME
CAST(MULTISET(SELECTEMPNOFROMEMPWHEREDEPTNO=D.DEPTNO)ASNUMBER_NT)

-------------- -------------------------------------------------------------
----
ACCOUNTING     NUMBER_NT(7782, 7839, 7934)
RESEARCH       NUMBER_NT(7369, 7566, 7788, 7876, 7902)
SALES          NUMBER_NT(7499, 7521, 7654, 7698, 7844, 7900)
OPERATIONS     NUMBER_NT()




Received on Tue Oct 19 1999 - 11:28:07 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US