who i can i use a column name as a parameter in a procedure or function in a package in oracle

4 views
Skip to first unread message

ahmed eitah

unread,
Mar 16, 2010, 1:32:04 PM3/16/10
to oracle-...@googlegroups.com, egypt-pr...@yahoogroups.com
dear group
i need to write a procedure or function in a package that accepts a column name that is not known at compilling time(column name is the parameter).this can be done in sql plus but in a procedure it treats it as a constant value.for example

PROCEDURE QUARY3(p_cursor OUT empcur,ayhaga in varchar2)is
  BEGIN
     
      OPEN p_cursor FOR                
      select f_no,ayhaga FROM hsabattree;
     
 END QUARY3;

 i get a constant value 'ayhaga' in all the tuples. what i need is if there is a column in hsabattree table called ahmad and a column called khaled.what i want ayhaga = 'ahmad' then ahmad column is selected.in
another query ayhaga = 'khaled' then khaled column is selected.when i tried to use : as sql at compilling the package i was promted to enter a value for ayhaga parameter.
can any one help. thanks in advance
                                                                                                      ahmad


ddf

unread,
Mar 16, 2010, 4:21:23 PM3/16/10
to Oracle in World

You cannot use varables directly in a select list; Oracle does not
perform substitutions outside of the WHERE clause. Building such
statements requires dynamic SQL, which you cannot use in the manner
you've illustrated. As an example for correctly using dynamic SQL:

declare
sqltxt varchar2(200);
ayhaga varchar2(40):='ahmad';
type ctyp is record(hsabattree.f_no%type, hsabattree.ahmad
%type);
type ccoll is table of ctyp index by binary_integer;
coll_v ccoll;
begin
sqltxt := 'select f_no,'||ayhaga||' FROM hsabattree';
execute immediate sqltxt bulk collect into coll_v;

for coll_v.first..coll_V.last loop
dbms_output.put_line(coll_v.f_no||' '||
coll_v.ahmad);
end loop;

end;
/

I know of no way to do what you posted using a dynamic cursor.


David Fitzjarrell

Reply all
Reply to author
Forward
0 new messages