Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Oracle / Oracle Server / July 2005

Tip: Looking for answers? Try searching our database.

Dynamic sql failure

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
william_hulse@hotmail.com - 29 Jul 2005 18:20 GMT
Hi all

The following:

begin
 execute immediate 'create table c (col1 number,col2 varchar2(30))';
 insert into c
 values (123,'data');
end;

fails with:

ORA-06550: line 3, column 15:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 3, column 3:
PL/SQL: SQL Statement ignored

Even though commenting out the insert enables the table creation

Any ideas would be great

Cheers

Will
Sybrand Bakker - 29 Jul 2005 18:53 GMT
>Hi all
>
[quoted text clipped - 20 lines]
>
>Will

1 has been discussed before
2 convert the code above into ordinary sql removing the begin end;
pair and you will be ok
3 It is generally considered bad programming to create tables on the
fly in pl/sql

--
Sybrand Bakker, Senior Oracle DBA
Jim Kennedy - 30 Jul 2005 03:35 GMT
> Hi all
>
[quoted text clipped - 20 lines]
>
> Will

Why are you creating a table?  Also for insert it is considered better to do
insert into tablex(col1,col2,..) values (...);

Jim
william_hulse@hotmail.com - 31 Jul 2005 10:48 GMT
Thanks all for your useful comments

Have received an answer from another group fyi:

Oracle evaluates the whole block before attempting to execute, but the
string used in execute immediate is not validated until it runs.  So,
it looks at exeucte immediate 'anything' and considers that valid, then

looks at the insert statement and cannot validate it, because the table

to insert into does not exist yet.  If you are going to create the
table dynamically, then you must also insert into it dynamically, as
demonstrated below.

scott@ORA92> begin
 2    execute immediate 'create table c (col1 number,col2
varchar2(30))';
 3    execute immediate 'insert into c values (123,''data'')';
 4  end;
 5  /

PL/SQL procedure successfully completed.

scott@ORA92> desc c
Name                                      Null?    Type
------------------------------­----------- --------
----------------------------
COL1                                               NUMBER
COL2                                               VARCHAR2(30)

scott@ORA92> select * from c
 2  /

     COL1 COL2
---------- ------------------------------
      123 data

scott@ORA92>
Sybrand Bakker - 31 Jul 2005 11:07 GMT
>table dynamically, then you must also insert into it dynamically, as
>demonstrated below.

Of course this a horrible approach as it will result in hard parses
all over the place.
The very reason Oracle invented pl/sql is to get rid of the hard
parses.

--
Sybrand Bakker, Senior Oracle DBA
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.