dbTalk Databases Forums  

Dynamically execute PL/SQL statement

comp.databases.oracle comp.databases.oracle


Discuss Dynamically execute PL/SQL statement in the comp.databases.oracle forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Agoston Bejo
 
Posts: n/a

Default Dynamically execute PL/SQL statement - 10-29-2004 , 05:46 AM






I am looking for the PL/SQL equivalent of the VBScript Exec and/or Eval
functions, i.e. I want to be able to dynamically create a statement, then
execute it in the current PL/SQL context, e.g.

declare
x integer := 5;
begin
ExecuteStatement('x := 10');
dbms_output.put_line(x); -- should put "10"
if EvaluateExpression('x*2 = 20') then
dbms_output.put_line('Yes');
else
dbms_output.put_line('No');
end if; -- should put 'Yes'
end;


EXECUTE IMMEDIATE doesn't seem to work, because it runs the statement in the
global context, not inside the context of the current function.



Reply With Quote
  #2  
Old   
Kamal
 
Posts: n/a

Default Re: Dynamically execute PL/SQL statement - 10-29-2004 , 10:40 AM






"Agoston Bejo" <gusz1 (AT) freemail (DOT) hu> wrote


Quote:
[...] because it runs the statement in the
global context, not inside the context of the current function.
You could you use global variables in packages, but it is often a bad
practice, prone to errors.

You can write:

execute immediate ('begin pkname.glob_var0 := something; end;');

Kamal


Reply With Quote
  #3  
Old   
Jan
 
Posts: n/a

Default Re: Dynamically execute PL/SQL statement - 10-30-2004 , 11:39 AM



DECLARE

v NUMBER;

BEGIN

EXECUTE IMMEDIATE
'BEGIN :1:=10;END;'
USING OUT v;

DBMS_OUTPUT.PUT_LINE(v);

END;

Jan

Reply With Quote
  #4  
Old   
GQ
 
Posts: n/a

Default Re: Dynamically execute PL/SQL statement - 10-31-2004 , 11:45 AM



"Agoston Bejo" <gusz1 (AT) freemail (DOT) hu> wrote

Quote:
I am looking for the PL/SQL equivalent of the VBScript Exec and/or Eval
functions, i.e. I want to be able to dynamically create a statement, then
execute it in the current PL/SQL context, e.g.

declare
x integer := 5;
begin
ExecuteStatement('x := 10');
dbms_output.put_line(x); -- should put "10"
if EvaluateExpression('x*2 = 20') then
dbms_output.put_line('Yes');
else
dbms_output.put_line('No');
end if; -- should put 'Yes'
end;


EXECUTE IMMEDIATE doesn't seem to work, because it runs the statement in the
global context, not inside the context of the current function.

What is it you would like to do that you can't do ?
The following is an example using an anonymous block
to create a table in the schema running the script,
followed by the same thing in a procedure (that could take
parameters).

Declare
v_obj number;
v_tblsp varchar2(30);
Begin
Select count(*) into v_obj
from user_tables
where table_name = 'T_EMP';
If v_obj = 0 then
Select tablespace_name into v_tblsp
from user_tables
where table_name = 'EMPLOYEE';
execute immediate 'Create table T_EMP (c1 number primary key, c2
varchar2(20))'||
' tablespace '|| v_tblsp;
end if;
End;
/
Create or replace procedure testx authid current_user as
v_obj number;
v_tblsp varchar2(30);
Begin
Select count(*) into v_obj
from user_tables
where table_name = 'T_EMP';
If v_obj = 0 then
Select tablespace_name into v_tblsp
from user_tables
where table_name = 'EMPLOYEE';
execute immediate 'Create table HR_OWN.T_EMP (c1 number, c2
varchar2(20))'||
' tablespace '|| v_tblsp;
end if;
End;
/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.