Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

Jeronimo

Programmer
Aug 2, 2001
12
US
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
 
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


[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
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. [sig][/sig]
 
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 ? [sig][/sig]
 
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
 
prob a stupid question but:

what does &quot;EXECUTE IMMEDIATE h_var;&quot; do? [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
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. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top