×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PL/SQL:how to transform a string in an executable sql statment?

PL/SQL:how to transform a string in an executable sql statment?

PL/SQL:how to transform a string in an executable sql statment?

(OP)
Hi,
I have a problem concerning PL/SQL, and I want to describe it on a concrete example:
Let's suppose that you have a table called "Sqlstatement_Table". Here's the code for that table:
                         CREATE TABLE Sqlstatement_Table(
                         Statement VARCHAR2(200));

Let there be only one entry in the table: the string 'Select * FROM X_Table'.
OK, and now, I want to execute this statement. Therefore, I write the following code:

                         DECLARE
                         h_var VARCHAR2(200);
                         BEGIN
                         select Statement into h_var from Sqlstatement_Table;
                         ??? EXECUTE IMMEDIATE h_var; ???
                         END;
                         /

The variable h_var contains now the string 'Select * FROM X_Table'. It doesn't work like this; the line between ??? is  not correct. But how can I transform this string in an executable SQL statement? How can I make ORACLE execute this  code?

  -Thanks for your help!
            Jeronimo

RE: PL/SQL:how to transform a string in an executable sql statment?

Hi Jeronimo,

The code you posted, if you type it into sql/plus exactly as below, will run.

Sql/plus will see the final / and run the block.

DECLARE
  h_var VARCHAR2(200);
BEGIN
  Select Statement into h_var from Sqlstatement_Table;
END;
/

or if you save that text into a file called cmd.sql you could run it like this:

sqlplus user/pass @cmd


Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: PL/SQL:how to transform a string in an executable sql statment?

This sort of construct may work for other forms of DML statement (e.g. inserts), but it won't work for selects. Select statements return a result set (the column values), but you haven't provided anywhere for your results to be put.

RE: PL/SQL:how to transform a string in an executable sql statment?

Oops, typing too slow, you got in before me Mike.
I was assuming the problem was dynamically executing the statement stored in the table row ?

RE: PL/SQL:how to transform a string in an executable sql statment?

(OP)
Hi Mike, hi yaffle

thanks for your replies! Mike, I'm sorry, but it does not work. But let me describe the problem in more details: I have the following tables:

create table demo_table(
dummy VARCHAR2(200));

insert into demo_table values('Hello!');

create table sqlstatement_table(
statement VARCHAR2(200));

insert into Sqlstatement_Table values('SELECT * FROM demo_table')

Ok.  Now, I want to execute the statement which is still a string. I use the following PL/SQL code from sqlplus and get an error:

SQL> DECLARE
  2   h_var VARCHAR2(200);
  3  
  4  BEGIN
  5  
  6   select Statement into h_var from Sqlstatement_Table;
  7   EXECUTE IMMEDIATE h_var;
  8  
  9  END;
 10  /

*
ERROR at line 3:
ORA-03113: end-of-file on communication channel

And now, nothing works anymore:

SQL> select * from user_catalog;
select * from user_catalog
                         *
ERROR at line 1:
ORA-01041: internal error. hostdef extension doesn't exist

Does anybody know what happened here? I have to restart sqlplus to continue.

Jeron

RE: PL/SQL:how to transform a string in an executable sql statment?

Mike -
EXECUTE IMMEDIATE is the 8i way of running a dynamic statement, as in:

EXECUTE IMMEDIATE 'CREATE TABLE x (y DATE)';

The error Jeron shows indicates to me that his session is getting terminated for some reason.  And as Yaffle points out, the SELECT statement will run into problems because there is no place to return the results to.

Jeron - try your code using a DML or DDL statement and see if you get better results.

RE: PL/SQL:how to transform a string in an executable sql statment?

Hi,

many thanks for your help! In fact, the "execute immediate"-statement deos not work with a select. The ony way of dealing with this problem is by using dynamic sql. The key is the DBMS_SQL package (see also http://www.oracle.com/oramag/webcolumns/vallam.html). I finally could solve this problem!

Jeron

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close