Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: PL/SQL
On Wed, 18 Nov 1998 10:56:12 GMT, h.haag_at_fnt.de (Horst Haag) wrote:
>Hi,
>
>I am trying to access a table within a oracle function.
>The function looks similar to:
>
>The function get an id as argument it then select in one main table (elements)
>the tablename where to look for an specific attribute in the detail table
>(v_class).
>While compiling the function oracle tells me that v_class is no
>table.
>Hints ver welcome !!
>Thanks.
>
>create or replace function get_ident
>(v_comp_elid in varchar2)
>RETURN number
>is
> v_ident number;
> v_class varchar2(20);
>begin
>--
>-- Select tablename from elements
>--
> select
> e.tablename into v_class
> from
> elements e
> where
> e.elid=v_comp_elid;
>--
>-- Now select ident from table
>--
> select
> ident into v_ident
> from
> v_class
> where elid=v_comp_elid;
>--
v_class is not a table name. It is a variable.
You can't use a variable as the table name. Oracle tries to verify all queries during compilation of procedures and functions. You will need to use dynamic SQL to accomplish this.
eg.
create or replace function get_ident
(v_comp_elid in varchar2)
RETURN number
is
v_ident number; v_class varchar2(20); l_cursor number; l_status number;
-- -- Select tablename from elements -- select e.tablename into v_class from elements e where e.elid=v_comp_elid;
-- -- Now select ident from table -- select ident into v_ident from v_class
-- -- Using Dynamic SQL to select ident from table --
' where elid = ''' || v_comp_elid || '''', dbms_sql.native );
Assuming that the input v_comp_elid will always only produce exactly one row, this will work.
Hope this helps.
chris.
--
Chirstopher Beck
Oracle Corporation
clbeck_at_us.oracle.com
Reston, VA.
![]() |
![]() |