Thread: create table by trigger


Permlink Replies: 3 - Pages: 1 - Last Post: May 28, 2007 7:25 AM Last Post By: user576967
linuxkk

Posts: 1
Registered: 03/17/02
create table by trigger
Posted: Aug 29, 2002 10:26 PM
Click to report abuse...   Click to reply to this thread Reply
I am building a system. one of module needs to create a new table after a user insert a new record into an existed table. I make a procedure as follows:

CREATE OR REPLACE PROCEDURE my_create AS
v_maxtime usermeta.instime%TYPE;
v_tbname usermeta.uname%TYPE;
v_Sqlstring VARCHAR2(200);
BEGIN
SELECT MAX(instime) INTO v_maxtime FROM usermeta;
SELECT uname INTO v_tbname FROM usermeta WHERE instime=v_maxtime;
DBMS_OUTPUT.PUT_LINE(v_tbname);
v_SqlString:='CREATE TABLE ' || v_tbname || '(' || v_tbname || 'Date TIMESTAMP, '
||v_tbname||'Values NUMBER)';
DBMS_OUTPUT.PUT_LINE(v_sqlstring);
EXECUTE IMMEDIATE v_sqlstring;
END;

If the v_tbname:='Toms', I Want the SQL is 'CREATE TABLE Toms(TomsDate TIMESTAMP, TomsValues NUMBER). And I have a trigger as follows:

CREATE OR REPLACE TRIGGER my_insert
AFTER INSERT ON usermeta
DECLARE
v_maxtime mytemp1.instime%TYPE;
BEGIN
my_create;
END;

I compile it successful but run failed. Error code is: ORA-04092, ORA-06512, ORA-06512 and ORA-04088.
Ora-04092 describe as follows:

ORA-04092 cannot string in a trigger
Cause: A trigger attempted to commit or roll back.
Action: Rewrite the trigger so it does not commit or roll back.

Is a trigger can not commit? Or is there any other error in my program? How should I do to satisfy my need?

amit.kantak

Posts: 6
Registered: 04/02/01
Re: create table by trigger
Posted: Feb 12, 2003 3:41 PM   in response to: linuxkk in response to: linuxkk
Click to report abuse...   Click to reply to this thread Reply
Hi Terry,

You cannot use DDL statements inside a Trigger body as they are as ggod as implicit commits. Commit & rollback is not allowed in triggers.
Lets assume DDLs were allowed in triggers, dont you think it would not be a good idea to create a new table for each row inserted. Rather I would suggest, you create a temp table and in your trigger, each time a new row is inserted, insert the corresponding row into this temp table.

-- Amit
user576967

Posts: 2
Registered: 05/28/07
Re: create table by trigger
Posted: May 28, 2007 7:25 AM   in response to: linuxkk in response to: linuxkk
Click to report abuse...   Click to reply to this thread Reply
Hi There,

You can use AUTONOMUS_TRANSACTION to avoid the commit problem in your
trigger.
The sysntax would be:

create or replace trigger triger_name
after|before clause
for each row optional
declare
PRAGMA AUTONOMUS_TRANSACTION;
begin
//your statements
commit/rollback;
end;
user576967

Posts: 2
Registered: 05/28/07
Re: create table by trigger
Posted: May 28, 2007 7:25 AM   in response to: linuxkk in response to: linuxkk
Click to report abuse...   Click to reply to this thread Reply
Hi There,

You can use AUTONOMUS_TRANSACTION to avoid the commit problem in your
trigger.
The sysntax would be:

create or replace trigger triger_name
after|before clause
for each row optional
declare
PRAGMA AUTONOMUS_TRANSACTION;
begin
//your statements
commit/rollback;
end;
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums