|
-
Originally posted by stecal
Oracle does not do "Select ColName , (Select ColName From TableName ) from AnotherTableName"
Oh yes, it does. At least from 8.1.7 and above, not sure about 8.1.6. The only catch is that a subquery in a select list must return only one value - that is select mus result in a single row-single coulumn resultset.
Example:
Code:
SQL> select d.dname,
2 (select sum(sal) from emp
3 where deptno=d.deptno) total_sal
4 from dept d;
DNAME TOTAL_SAL
-------------- ----------
ACCOUNTING 8750
RESEARCH 10875
SALES 9400
OPERATIONS
SQL>
Well, the last example was from 8.1.6. This one is from 9.2.0.1. How does that work now?
SQL> select dname, (select empno, mgr from emp) from dept;
select dname, (select empno, mgr from emp) from dept
*
ERROR at line 1:
ORA-00913: too many values
It works if you use the correct syntax. When a subselect in a select list returns more than one column or more than one row (or both), you must use so called "cursor expression", which is basicaly the same as REFCURSOR in PL/SQL.
Example:
Code:
SQL> SELECT dname,
2 CURSOR(SELECT sal, mgr
3 FROM emp e
4 WHERE e.deptno = d.deptno)
5 FROM dept d
6 WHERE deptno=10;
DNAME CURSOR(SELECTSAL,MGR
-------------- --------------------
ACCOUNTING CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
SAL MGR
---------- ----------
2450 7839
5000
1300 7782
SQL>
Jurij Modic
ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|