Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Alsayed.

Asked: August 23, 2016 - 10:28 am UTC

Last updated: August 24, 2016 - 1:01 am UTC

Version: 10g

Viewed 1000+ times

You Asked

Hi Chris and Connor,
How can I give an alias a non named column resulting from a subquery?
the case

select *
from
(
select 6*7 from dual
);

I know it's easy to alias it in the subquery like that

select result
from
(
select 6*7 as result from dual
);

but the problem is here in that case when trying to alias the result of a pl/sql created table of varchar2 that wee need to direct to another created table. and here is the full case:


CREATE OR REPLACE TYPE ABC AS TABLE OF VARCHAR2(20);
CREATE TYPE EMPREC AS OBJECT
(
F_NAME VARCHAR2(20),
F_JOB VARCHAR2(20)
);
CREATE TYPE EMP_TABLE AS TABLE OF EMPREC;

DECLARE
L_ABC ABC;
L_EMP_TABLE EMP_TABLE;
BEGIN
SELECT FIRST_NAME BULK COLLECT INTO L_ABC
FROM EMPLOYEES
WHERE DEPARTMENT_ID=50;
DBMS_OUTPUT.PUT_LINE(L_ABC.COUNT);
--SELECT EMPREC(L_ABC.*,'DEVELOPER') -- that will not work
--SELECT EMPREC(TA.*,'DEVELOPER') -- that also will not work
SELECT EMPREC('USINE BOLT','DEVELOPER') -- that will work
BULK COLLECT INTO L_EMP_TABLE
FROM TABLE(L_ABC);
DBMS_OUTPUT.PUT_LINE(L_EMP_TABLE.COUNT);
END;
/

so how to reference the data of the L_ABC table in the select statement
Thanks in advance


and Chris said...

The first part of your question about referencing subqueries is completely separate from the second part!

I'll answer them both anyway.

If you don't give an expression an alias, the column name is whatever the expression is. So in your example the column in the outer query is 6*7. Note this is an invalid name, so you have to wrap it in double quotes:

select "6*7" from
  ( select 6*7 from dual
  ) ;

6*7  
42   


When selecting values from a nested table, it depends upon what you're doing.

If it's a table of single values (e.g. table of varcahr2/number/date) then you reference the column with "column_value":

declare
  l_abc abc;
  l_emp_table emp_table;
begin
  select first_name bulk collect
  into l_abc
  from hr.employees
  where department_id=50;
  dbms_output.put_line ( l_abc.count ) ;
  
  select emprec(t.column_value, null)
  bulk collect
  into l_emp_table
  from table ( l_abc ) t ;
  dbms_output.put_line ( l_emp_table.count ) ;
end;
/

45
45


If you're selecting into a table of objects then you can access the fields of the object as normal:

declare
  l_abc emp_table;
  l_emp_table emp_table;
begin
  select emprec(first_name , job_id)
  bulk collect
  into l_abc
  from hr.employees
  where department_id=50;
  dbms_output.put_line ( l_abc.count ) ;
  
  select emprec(t.f_name, t.f_job)
  bulk collect
  into l_emp_table
  from table ( l_abc ) t  ;
  dbms_output.put_line ( l_emp_table.count ) ;
end;
/

45
45

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Alsayed, August 23, 2016 - 4:40 pm UTC

Thanks Chris
It worked
Chris Saxon
August 24, 2016 - 1:01 am UTC

Glad we could help

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library