Home » SQL & PL/SQL » SQL & PL/SQL » Can one pass a column name as a parameter to a procedure
Can one pass a column name as a parameter to a procedure [message #912] Tue, 12 March 2002 06:32 Go to next message
Dan
Messages: 61
Registered: February 2000
Member
I am trying to make a 'dynamic' sql statement in Oracle 7.x as follows:

Procedure get_data(x IN varchar2) as
stmt varchar2(200);
cur integer;
Begin
cur := dbms_sql.open_cursor;
stmt := 'Select' ||x|| ' from table';
dbms_sql.parse(cur,stmt,dbms_sql.v7);
.
.
.
.
End get_data;

This procedure works fine if I pass '*', however if I pass a valid column name it fails.

Please help
Re: Can one pass a column name as a parameter to a procedure [message #918 is a reply to message #912] Tue, 12 March 2002 20:55 Go to previous messageGo to next message
Suparna Saha
Messages: 10
Registered: October 2001
Junior Member
I am sending one Dynamic Sql. You run this sql and check in with your requirements.

/****************************************************************************************/
/* Dynamically Execute SQL
/****************************************************************************************/

set serveroutput on
declare
id number;
scolval varchar2(30);
sreturn integer;
scol varchar2(30);
begin
dbms_output.enable(40000);
sCol := 'area_name';
id := dbms_sql.open_cursor;
dbms_sql.parse(id, 'select ' || scol || ' from t_area_mast' , dbms_sql.v7);
dbms_sql.define_column(id, 1, scolval, 30);
sreturn := dbms_sql.execute(id);
loop
if dbms_sql.fetch_rows(id) > 0 then
dbms_sql.column_value(id, 1, scolval);
dbms_output.put_line('t_area_mast = ' || scolval);
else
exit;
end if;
end loop;
dbms_sql.close_cursor(id);
end;
/

I think it will help you.
Re: Can one pass a column name as a parameter to a procedure [message #923 is a reply to message #912] Wed, 13 March 2002 05:52 Go to previous messageGo to next message
sfhollands
Messages: 15
Registered: October 2001
Junior Member
the reason it did not work when u passed a column name as parameter was 'cos there was no space between the select statemtn and the concatenated column name.
just add a space.

Now it will work
stmt := 'Select ' ||x|| ' from table';
Re: Can one pass a column name as a parameter to a procedure [message #935 is a reply to message #918] Thu, 14 March 2002 06:29 Go to previous message
Dan
Messages: 61
Registered: February 2000
Member
Thanks for your help. This worked fine.
Previous Topic: help! second half of procedure not executing
Next Topic: Automate a PL/SQL procedure
Goto Forum:
  


Current Time: Tue Apr 23 09:58:38 CDT 2024