If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > function to create table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-03, 08:56
ohanus ohanus is offline
Registered User
 
Join Date: Jan 2003
Posts: 41
function to create table

Hello,

how can I create table with pl/sql? I want to create function do this but error occured.
I used this for create function
CREATE OR REPLACE FUNCTION CREATE_OBJ
RETURN VARCHAR
IS
num NUMBER;
BEGIN
CREATE TABLE WST.test (
id NUMBER
);
commit;

END;
/

Any idea?

Ondrej
Reply With Quote
  #2 (permalink)  
Old 09-10-03, 09:20
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: function to create table

You can't perform DDL (like CREATE TABLE) directly from within PL/SQL. You would have to use dynamic SQL:

CREATE OR REPLACE FUNCTION CREATE_OBJ
RETURN VARCHAR
IS
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE WST.test (id NUMBER)';
RETURN 'something';
END;
/

There is no need to COMMIT the DDL, that happens automatically. And a function has to RETURN something.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 09-10-03, 09:31
ohanus ohanus is offline
Registered User
 
Join Date: Jan 2003
Posts: 41
Unhappy Re: function to create table

Well, I try you write... compilation is ok but when I use the function in sql I get error:
select CREATE_OBJ from dual;

ORA-14552 cannot perform a DDL, commit or rollback inside a query or DML
ORA-06512: "WST.CREATE_OBJ_1", line 3



Quote:
Originally posted by andrewst
You can't perform DDL (like CREATE TABLE) directly from within PL/SQL. You would have to use dynamic SQL:

CREATE OR REPLACE FUNCTION CREATE_OBJ
RETURN VARCHAR
IS
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE WST.test (id NUMBER)';
RETURN 'something';
END;
/

There is no need to COMMIT the DDL, that happens automatically. And a function has to RETURN something.
Reply With Quote
  #4 (permalink)  
Old 09-10-03, 09:44
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: function to create table

That's correct, as the error says, you can't perform DDL (like CREATE TABLE) inside a query (like SELECT).

But you can do this:

SQL> VAR x VARCHAR2(100)
SQL> EXEC :x := CREATE_OBJ;

But really, I wouldn't create a FUNCTION to perform DDL like CREATE TABLE, I would just use a SQL Plus script.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 09-10-03, 10:08
ohanus ohanus is offline
Registered User
 
Join Date: Jan 2003
Posts: 41
Re: function to create table

Great!!

It works fine after setting all needed privileges... thank you very much.

But I have another question. I want modified function CREATE_OBJ, I need give table name like parameter. So the function should be like this

CREATE OR REPLACE FUNCTION CREATE_OBJ (tName VARCHAR) RETURN VARCHAR IS
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE '+tName+' (id NUMBER)';
RETURN 'something';
END;

I'm sure this is not correct syntax.



Quote:
Originally posted by andrewst
That's correct, as the error says, you can't perform DDL (like CREATE TABLE) inside a query (like SELECT).

But you can do this:

SQL> VAR x VARCHAR2(100)
SQL> EXEC :x := CREATE_OBJ;

But really, I wouldn't create a FUNCTION to perform DDL like CREATE TABLE, I would just use a SQL Plus script.
Reply With Quote
  #6 (permalink)  
Old 09-10-03, 11:04
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: function to create table

Very nearly: just use || instead of +
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 09-11-03, 03:44
ohanus ohanus is offline
Registered User
 
Join Date: Jan 2003
Posts: 41
Re: function to create table

Yeaaa, that's it!!!

Thank you very much...

Ondrej

Quote:
Originally posted by andrewst
Very nearly: just use || instead of +
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On