CREATE TEMPORARY TABLE USING EXECUTE IMMEDIATE
Hi All,
i have a question,
how can i create a temporary table using EXECUTE IMMEDIATE ??
Like:
CREATE GLOBAL TEMPORARY table new_table as (Select * from old_table);
Thanks,
Edited by: xDeviates on Jun 11, 2012 3:13 PM
It looks like you are approaching the problem incorrectly. As I suggested in Dynamic Select, it sounds like you, at most, want a function that returns a SYS_REFCURSOR (it's still not obvious to me why you would even want/ need to resort to dynamic SQL in the first place)
CREATE OR REPLACE FUNCTION get_dynamic_cursor( p_table_name IN VARCHAR2 )
RETURN sys_refcursor
IS
l_rc sys_refcursor;
BEGIN
OPEN l_rc FOR 'SELECT * FROM ' || dbms_assert.sql_object_name( p_table_name );
RETURN l_rc;
END;which you can then call from your application
SQL> variable rc refcursor;
SQL> exec :rc := get_dynamic_cursor( 'EMP' );
PL/SQL procedure successfully completed.
SQL> print rc
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 801
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1601 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1251 500
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2976
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1251 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2851
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7782 CLARK MANAGER 7839 09-JUN-81 2451
10
7788 SCOTT ANALYST 7566 19-APR-87 3001
20
7839 KING PRESIDENT 17-NOV-81 5001
10
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7844 TURNER SALESMAN 7698 08-SEP-81 1501 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1101
20
7900 JAMES CLERK 7698 03-DEC-81 951
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7902 FORD ANALYST 7566 03-DEC-81 3001
20
7934 MILLER CLERK 7782 23-JAN-82 1301
10
14 rows selected.Justin
Similar Messages
-
Create temp table using EXECUTE IMMEDIATE
Is there any performance issue in creating globally temp table
using EXECUTE IMMEDIATE or creating globally temp table from
SQL PLUS.
Any response will be greatly appreciated.
nullAnish,
Creating tables is likely to be an expensive operation.
Performance issues can only be considered in comparison to
alternatives.
Alternatives include: PLSQL tables, cursors and/or recoding so
that tmp tables are not required. (One of our consultants reckons
that sqlserver temp tables are usually used to get around
limitations in sqlserver, ie slightly more complicated sql
statements could be used instead of simpler sql and temporary
tables).
I would think creating the temp table once during sqlplus would
be cheaper than creating and deleting it repeatedly at run time.
Note that EXECUTE IMMEDIATE may do an implicit commit (dbms_sql
certainly does). This may be got over my using the PRAGMA
AUTONOMOUS_TRANSACTION; direction which places a
procedure/function in a seperate transaction.
Turloch
P.S. We have some difficulty in getting information back from the
field/customer sites. If you have questions and answers that are
likely to be useful to other Oracle Migration Workbench
users, and migrators in general, please send them in for possible
inclusion in our Frequently Asked Question list.
Oracle Migration Workbench Team
Anish (guest) wrote:
: Is there any performance issue in creating globally temp table
: using EXECUTE IMMEDIATE or creating globally temp table from
: SQL PLUS.
: Any response will be greatly appreciated.
Oracle Technology Network
http://technet.oracle.com
null -
Selecting data from a table using Execute Immediate in 9i
Hi,
I was wondering how I would be able to do a SELECT on a table using EXECUTE IMMEDIATE. When I tried it (with the proc below), I got the following below. Can anyone tell me what I am doing wrong?
Using Scott/Tiger I tried this:
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE PROCEDURE P2
2 IS
3 v_SQL VARCHAR2(100);
4 BEGIN
5 v_SQL := 'Select * from Emp';
6 EXECUTE IMMEDIATE v_SQL;
7* END;
SQL> /
Procedure created.
SQL> exec p2
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> exec p2
PL/SQL procedure successfully completed.
SQL>
Thanks in advance.
Sincerely,
Nikhil Kulkarni1 CREATE OR REPLACE PROCEDURE P2
2 IS
3 v_SQL VARCHAR2(100);
erec emp%rowtype;
4 BEGIN
5 v_SQL := 'Select * from Emp';
6 EXECUTE IMMEDIATE v_SQL into erec;
7* END;
SQL> / -
Create temporary Tables using SQL
Hello,
I'm wondering if SAP allows the creation of new Tables without SDK objects,
I want to create temporary tables using SQL scripts an compile them when an specific addon is connected and erase them when the addon disconnects,
Do you think this is allowed?.
thanks,
GabrielaYou could always have a second DB to create your temp tables in. This is the way I've done this, as well as created my own views and stored procedures in it. No updating of the primary table necessary. The way I named things was:
Company_DB - Company Database
Company_DB-Extern - My own stuff
Then, in sap, you can just do [Company_DB-Extern]..Object to call it, or you do the same from withing your project. -
Oracle 11G Copying a table using execute immediate
I want to copy the contents of one table into another using execute immediate.
This keeps failing with the following error
ORA-00903: invalid table name
ORA-06512: at "TABLE_COPY", line 6
ORA-06512: at line 8
create or replace
procedure TABLE_COPY(
TABLE1 varchar2,
TABLE2 varchar2)
is
begin
execute immediate 'insert into '||TABLE2||' (select * from '||TABLE1||')';
end;
Edited by: user9213000 on 24-Jan-2013 07:38user9213000 wrote:
I want to copy the contents of one table into another using execute immediate.
This keeps failing with the following error
ORA-00903: invalid table name
ORA-06512: at "TABLE_COPY", line 6
ORA-06512: at line 8
create or replace
procedure TABLE_COPY(
TABLE1 varchar2,
TABLE2 varchar2)
is
begin
execute immediate 'insert into '||TABLE2||' (select * from '||TABLE1||')';
end;
Edited by: user9213000 on 24-Jan-2013 07:38The standard advice when (ab)using EXECUTE IMMEDIATE is to compose the SQL statement in a single VARCHAR2 variable
Then print the variable before passing it to EXECUTE IMMEDIATE.
COPY the statement & PASTE into sqlplus to validate its correctness. -
Problem with alter table in execute immediate
We have PL/SQL scripts which modify the data structure, add data etc to en existing database. These scripts basically ensure that the db is compatible with what the software expects.
The reason for doing it in PL/SQL rather than SQL script is A) The scripts are launched using GUI so that they are more user friendly. sqlplus is launched as a background process which executes these scripts. All the scripts have EXIT FAILURE WHENEVER SQLERROR as first line to ensure that the control does not hang in the sqlplus background process.
Going from one version to other, we have added a few tables to the database and also modified a few tables. since (i think) DDL is not allowed in PL/SQL block, we had to resort to putting them in EXECUTE IMMEDIATE enclosures.
Now for the real question,
If I create a table using EXECUTE IMMEDIATE clause, I can immediately have insert as a next statement to insert data in this table. but, if I alter the table and add a column to the existing table, I cannot immediately add data to that column, it throws an error saying 'invalid identifier'
At code level, the following is allowed :
EXECUTE IMMEDIATE 'CREATE TABLE SP_TEST_TABLE
ID NUMBER,
NAME Varchar2(40)
INSERT INTO SP_TEST_TABLE(ID, NAME) Values(1, 'SP');
but I get error for the following :
EXECUTE IMMEDIATE 'ALTER TABLE SP_TEST_TWO ADD
ANOTHER_COLUMN number
UPDATE SP_TEST_TWO SET ANOTHER_COLUMN = 1;
In this case, it says ANOTHER_COLUMN invalid identifier
Does anybody know why?
any workaround will be greatly appreciated.
--SPFriends,
Thanks to all of you for your help. The spelling mistakes might have occurred becuase i changed the actual script to something 'short and complete' to show the problem.
I could solve the problem by having more than one PL/SQL block within my file. something like :
BEGIN
--alter table statements here
END;
BEGIN
--insert the values in column here.
END;
I am still not sure why the error is presented only on alter table statement and not for create table statement. Probably somebody with the knowledge of oracle internals will be able to throw more light on it. I am trying to get the naswers, if I get the answer, I'll surely post it here.
Regards,
--Saurabh -
Problem in using CREATE TABLE with Execute Immediate
I'm trying to create a table using Native Dynamic SQL. the code of the pl/sql block is
BEGIN
EXECUTE IMMEDIATE 'create table demo (ddate date)';
END;
The problem is that the above block is executed successfully as an anonymous PL/SQL block. The same block when written in a procedure it gives an error
"ORA-01031 Insufficient privelages"
at the time of execution. The procedure is complied successfully.
nullYour user needs direct system privs to create tables. You are receiving your privs properly by the role RESOURCE. Connect as system and grant CREATE TABLE directly to your user - that should do it.
Regards
Peter Larsen -
Using execute immediate creating a table from another
hi friend i wanted to create a table from a select statement in a pl sql procedure. i am using execute immediate but getting problems with it pls can anyone help me.
here is the query i am using
EXECUTE IMMEDIATE 'CREATE TABLE table_name AS (SELECT * FROM a_view WHERE column_name LIKE '%some_string%');
i need to know if this can be done and if yes how. pls help me its bit urgent too. the schema name is same and the privileges are available to create tables too.Your syntax is wrong.
If you would use a syntax higlighted editor, it would show.
Yout try to execute a string where another string is embedded. Please use two times single quote or use the 'q' function:
This one is correct:
EXECUTE IMMEDIATE 'CREATE TABLE table_name AS (SELECT * FROM a_view WHERE column_name LIKE ''%some_string%'')';
or this one:
EXECUTE IMMEDIATE q'|CREATE TABLE table_name AS (SELECT * FROM a_view WHERE column_name LIKE '%some_string%')|';
good luck -
Create Partition tables in PL/SQL using Execute Immediate
Hi
I have to create a partiton table in PL/SQL using Execute Immediate. I concat the necessary Create Table syntax into a variable and use Execute Immediate variable name. This gives a error ORA-00900: invalid SQL statement. However if i cut and paste the SQL statement from DBMS_OUTPUT, the table creation goes through without any problem.
What could be the issue. Has anyone face this before please.
I am using 10G DBThanks for your reply. It is a big code. I am pasting the part required.
v_sqlstmtout :='CREATE TABLE a_10(MYDATE DATE NOT NULL,ID NUMBER(14) NOT NULL)';
v_sqlstmtout := v_sqlstmtout || ' PARTITION BY RANGE (MYDATE) ';
v_sqlstmtout := v_sqlstmtout || 'SUBPARTITION BY HASH(id) ';
v_sqlstmtout := v_sqlstmtout || 'SUBPARTITION TEMPLATE(';
v_sqlstmtout := v_sqlstmtout || 'SUBPARTITION SP1,SUBPARTITION SP2) ';
v_sqlstmtout := v_sqlstmtout || '(PARTITION mth_dummy VALUES LESS THAN ';
v_sqlstmtout := v_sqlstmtout || '('||V_SQLSTMT3||')' || v_sqlstmt||')';
EXECUTE IMMEDIATE ''''||v_sqlstmtout||'''';
variables are substituted through data from different tables.
The output looks like the following
CREATE TABLE a_10(MYDATE DATE NOT NULL,ID NUMBER(14) NOT NULL)
PARTITION BY RANGE (mydate) SUBPARTITION BY HASH(id) SUBPARTITION
TEMPLATE(SUBPARTITION SP1,SUBPARTITION SP2) (PARTITION mth_dummy VALUES
LESS THAN ('01-MAY-2006'), PARTITION mth_JAN2007 VALUES LESS THAN
('01-FEB-2007'))
The above is the output from DBMS_OUTPUT. If i run this statement the table is created. Please help.. -
Error while insert data using execute immediate in dynamic table in oracle
Error while insert data using execute immediate in dynamic table created in oracle 11g .
first the dynamic nested table (op_sample) was created using the executed immediate...
object is
CREATE OR REPLACE TYPE ASI.sub_mark AS OBJECT (
mark1 number,
mark2 number
t_sub_mark is a class of type sub_mark
CREATE OR REPLACE TYPE ASI.t_sub_mark is table of sub_mark;
create table sam1(id number,name varchar2(30));
nested table is created below:
begin
EXECUTE IMMEDIATE ' create table '||op_sample||'
(id number,name varchar2(30),subject_obj t_sub_mark) nested table subject_obj store as nest_tab return as value';
end;
now data from sam1 table and object (subject_obj) are inserted into the dynamic table
declare
subject_obj t_sub_mark;
begin
subject_obj:= t_sub_mark();
EXECUTE IMMEDIATE 'insert into op_sample (select id,name,subject_obj from sam1) ';
end;
and got the below error:
ORA-00904: "SUBJECT_OBJ": invalid identifier
ORA-06512: at line 7
then when we tried to insert the data into the dynam_table with the subject_marks object as null,we received the following error..
execute immediate 'insert into '||dynam_table ||'
(SELECT887684 wrote:
ORA-00904: "SUBJECT_OBJ": invalid identifier
ORA-06512: at line 7The problem is that your variable subject_obj is not in scope inside the dynamic SQL you are building. The SQL engine does not know your PL/SQL variable, so it tries to find a column named SUBJECT_OBJ in your SAM1 table.
If you need to use dynamic SQL for this, then you must bind the variable. Something like this:
EXECUTE IMMEDIATE 'insert into op_sample (select id,name,:bind_subject_obj from sam1) ' USING subject_obj;Alternatively you might figure out to use static SQL rather than dynamic SQL (if possible for your project.) In static SQL the PL/SQL engine binds the variables for you automatically. -
What is the syntax for creating global temporary table using a select query
hii
I'm creating a global temporary table using a select query ..how to mention 'on commit preserve rows' that?
create global temporary table t1 as select * from trn_ordbase on commit preserve rows;
but this is invalid syntax,so how to mention on commit preserve rows in this???if i dont mention it ,by default its considering as on commit delete rows.
Please help me out of this problem.create global temporary table t1 as select * from trn_ordbase on commit preserve rows;You CANNOT use this syntax.
http://download-east.oracle.com/docs/cd/B19188_01/doc/B15917/sqcmd.htm
http://download-east.oracle.com/docs/cd/B19188_01/doc/B15917/glob_tab.gif
http://download-east.oracle.com/docs/cd/B19188_01/doc/B15917/cre_tabl.gif -
Delete From More than 1 table without using execute immediate
Hi,
Am new to PL/SQL, I had been asked to delete few of the table for my ETL jobs in Oracle 10G R2. I have to delete(truncate) few tables and the table names are in another table with a flag to delete it or not. So, when ever I run the job it should check for the flag and for those flag which is 'Y' then for all those tables should be deleted without using the Execute Immediate, because I dont have privilages to use "Execute Immediate" statement.
Can anyone help me in how to do this.
Regards
SenthilThen tell you DBA's, or better yet their boss, that they need some additional training in how Oracle actually works.
Yes, dynamic sql can be a bad thing when it is used to generate hundreds of identical queries that differ ony in the literals used in predicates, but for something like a set of delte table statements or truncate table statements, dynamic sql is no different in terms of the effect on the shared pool that hard coding the sql statements.
This is a bad use of dynamic sql, because it generates a lot of nearly identical statements due to the lack of bind variables. It is the type of thing your DBA's should, correctly, bring out the lead pipe for.
DECLARE
l_sql VARCHAR2(4000);
BEGIN
FOR r in (SELECT account_no FROM accounts_to_delete) LOOP
l_sql := 'DELETE FROM accounts WHERE account_no = '||r.account_no;
EXECUTE IMMEDIATE l_sql;
END LOOP;
END;This will result in one sql statement in the shared pool for every row in accounts_to_delete. Although there is much else wrong with this example, from the bind variable perspective it should be re-written to use bind variables like:
DECLARE
l_sql VARCHAR2(4000);
l_acct NUMBER;
BEGIN
FOR r in (SELECT account_no FROM accounts_to_delete) LOOP
l_sql := 'DELETE FROM accounts WHERE account_no = :b1';
EXECUTE IMMEDIATE l_sql USING l_acct;
END LOOP;
END;However, since you cannot bind object names into sql statements, the difference in terms of the number of statements that end up in the shared pool between this:
DECLARE
l_sql VARCHAR2(4000);
BEGIN
FOR r in (SELECT table_name, delete_tab, trunc_tab
FROM tables_to_delete) LOOP
IF r.delete_tab = 'Y' THEN
l_sql := 'DELETE FROM '||r.table_name;
ELSIF r.trunc_tab = 'Y' THEN
l_sql := 'TRUNCATE TABLE '||r.table_name;
ELSE
l_sql := NULL;
END IF;
EXECUTE IMMEDIATE l_sql;
END LOOP;
END;and something like this:
BEGIN
DELETE FROM tab1;
DELETE FROM tab2;
EXECUTE IMMEDIATE 'TRUNCTE TABLE tab3';
END;or this as a sql script
DELETE FROM tab1;
DELETE FROM tab2;
TRUNCTE TABLE tab3;is absolutley nothing.
Note that if you are truncating some of the tables, and wnat/need to use a stored procedure, you are going to have to use dynamic sql for the truncates anyway since trncate is ddl, and you cannot do ddl in pl/sql wiothout using dynamic sql.
John -
Have problem when using EXECUTE IMMEDIATE.
Hi, i'm new here... i having problem when trying to use execute immediate statement... :(
procedure code as below:
create procedure copy_row(
p_table varchar2,
p_key varchar2,
p_keyval varchar2,
p_user varchar2,
p_frmto varchar2 default 'FROM')
IS
V_SQL VARCHAR2(500);
BEGIN
if upper(p_frmto)='FROM' then
V_SQL:='INSERT INTO '||P_TABLE||
' SELECT * FROM '||P_USER||'.'||P_TABLE||' WHERE '||P_KEY||' = '''||P_KEYVAL||'''';
elsif upper(p_frmto)='TO' then
V_SQL:='INSERT INTO '||P_USER||'.'||P_TABLE||
' SELECT * FROM '||P_TABLE||' WHERE '||P_KEY||' = '''||P_KEYVAL||'''';
end if;
EXECUTE IMMEDIATE V_SQL;
exception
when others then
raise_application_error(-20000,'PROCEDURE: COPY_ROW - UNABLE TO COPY ROWS! SQL='||V_SQL,TRUE);
end;
is there any limitation on using EXECUTE IMMEDIATE command? i need to use procedure because i'm using old form builder.... but the database server was 9i which able to use this command, so... i decide to use procedure....
i've done some testing on this.... assume the structure of schema1.table1 and schema2.table1 are same....
SQL> CREATE PROCEDURE TESTING IS
2 BEGIN
3 exeCute IMMEDIATE 'INSERT INTO schema1.table1 SELECT * FROM schema2.table1 WHERE column1 = ''abc''';
4 END;
5 /
Procedure created.
SQL> BEGIN
2 TESTING;
3 END;
4 /
BEGIN
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at "SCHEMA1.TESTING", line 3
ORA-06512: at line 2
any idea?? or have better solution other than this?
database server:9i
sqlplus:SQL*Plus: Release 8.0.6.0.0Do you have an access to tables you are going to insert into / select from ?
Dynamic SQL checks the access permissions at runtime, not at the compilation stage:
SQL> create user master identified by master default tablespace users temporary
2 tablespace temp;
User created.
SQL> alter user master quota unlimited on users;
User altered.
SQL> grant create session to master;
Grant succeeded.
SQL> grant create table to master;
Grant succeeded.
SQL> grant create procedure to master;
Grant succeeded.
SQL> conn master/master
Connected.
SQL> create procedure wrong_prc
2 is
3 begin
4 execute immediate 'insert into master.t select * from scott.t';
5 end;
6 /
Procedure created.
SQL> create table t (id number);
Table created.
SQL> exec wrong_prc;
BEGIN wrong_prc; END;
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at "MASTER.WRONG_PRC", line 4
ORA-06512: at line 1
SQL> conn scott/tiger
Connected.
SQL> grant select on t to master;
Grant succeeded.
SQL> conn master/master
Connected.
SQL> exec wrong_prc;
PL/SQL procedure successfully completed.
SQL> select * from t;
ID
1And don't use literals in dynamic SQL - you should use binding variables instead.
Rgds. -
Problem in Update statement using Execute Immediate
Hi All,
I am facing problem in update statement.
I am creating dynamic sql and use "execute immediate" statement to execute a update statement.
But it is not updating any thing there in the table.
I have created a query like :
update_query='Update '|| Table_Name ||' t set t.process_status =''Y'' where t.tid=:A';
Execute immediate update_query using V_Id;
commit;
But it is not updating the table.
I have a question , is execute immediate only does insert and delete?
Thanks
AshokSQL> select * from t;
TID P
101 N
SQL> declare
2 V_Id number := 101;
3 Table_Name varchar2(30) := 'T';
4 update_query varchar2(1000);
5 begin
6 update_query := 'Update '|| Table_Name ||' t set t.process_status =''Y'' where t.tid=:A';
7 Execute immediate update_query using V_Id;
8 commit;
9 end;
10 /
PL/SQL procedure successfully completed.
SQL> select * from t;
TID P
101 Y -
Why or When should we use Execute Immediate in PLSQL??
Hi Frnds,
Long Ago i have received a interview question that ...
How can U create a table in the PLSQL object(Function or procedure)?
But the thing y should we use execute immediate?
In which scenario we should we should use????????????
Why or When should we use Execute Immediate in PLSQL????OR
http://stackoverflow.com/questions/18375990/oracle-what-does-execute-immediate-means
For DML you'd use it when running statements that you don't have available at compile time, e.g. if the column list is based on a selection from the user.
In your case it's being used because DDL cannot be run as static SQL from within PL/SQL. Only certain query, DML and TCL commands are valid. Anything else has to be treated as dynamic.
I'd say it's rare to need to use DDL from a PL/SQL block. TRUNCATE might be reasonable; if you find anything creating or dropping objects on the fly then that might be more of a concern as it can suggest a suboptimal data model.
EXECUTE IMMEDIATE itself does not automatically commit; but if you execute DDL then that will behave the same as if you ran it outside PL/SQL, so it will commit in your case, yes.
Incidentally, I'm not sure why your code is using an intermediate variable to hold the statement; that's useful if you want to display what it's going to run maybe, but you don't seem to be doing that. What you have could be done as:
EXECUTE IMMEDIATE 'TRUNCATE TABLE BD_BIDS_EXT_DET';
Thank you
Maybe you are looking for
-
How do I share one library on two different computers?
I have a desktop G5 that has the majority of my music on it. I also have a MacBook Pro. They both have home sharing turned on. However, if I'm on the road with my MacBook Pro, I don't see a way to access the "shared" music from my home-based G5. Seem
-
How to send a form in DW 7?
Hi! I'm from Spain and I'd like to be understanded if possible. I've got Dreamweaver MX 7. I'd like to know how to send directly the form: I design a form, I put a text box with "name", "e-mail" and "comment" for example and the button "send". If I t
-
Sharing libraries: two computers: wireless setting
I'm having trouble sharing music between the libraries on my Mini and laptop. I am connected to the interest via a router, then both machines are connected wirelessly to the router. I can see and play the music on the laptop from the mini, but when I
-
I grouped my crystal report based on my products. I got 33 groups. Now i need to pick only top 10 groups which has heighest sales. how can i code it? Edited by: Sandeep Pendyala on Nov 24, 2008 11:14 PM
-
Why do I a kernel panic if I connect my SCSI Iomega Zipdrive?
I have start up problems in 10.4.11 (kernel panic), if my SCSI Iomega Zipdrive is connected to its AC power transformer and the Adaptec SCSI expansion card in my G4 PowerPC. I can no longer start the computer even after multiple restarts, the restart