EXECUTE IMMEDIATE question [message #7443] |
Fri, 13 June 2003 08:03 |
Christophe MAILHE
Messages: 2 Registered: June 2003
|
Junior Member |
|
|
Hi all.
I am quite new in PL/SQL and I need to run the following statment in a PL/SQL procedure :
EXECUTE IMMEDIATE "INSERT INTO H21ADMIN.H21_RC_WATCH_DAT ( SELECT '"|| mnew_seq ||"', '"|| mytable ||"', COUNT(*) FROM "|| mytable || ")";
mnew_seq and mytable are PL/SQL variables and not columns. These columns don't exist in the table.
I need to find the equivalent to :
INSERT INTO H21ADMIN.H21_RC_WATCH_DAT ( SELECT '1', 'H21ADMIN.THISISATABLE', COUNT(*) FROM H21ADMIN.THISISATABLE);
Could someone help ?
Cheers,
Christophe.
|
|
|
|
Re: EXECUTE IMMEDIATE question [message #7459 is a reply to message #7447] |
Mon, 16 June 2003 04:39 |
Christophe MAILHE
Messages: 2 Registered: June 2003
|
Junior Member |
|
|
Hi Barry,
I have already tried something similar, but without any success. Oracle returns an Error message : Columns do not exist because the Select statment interprets :mnew_seq and :mytable as columns name instead of values.
To solve the issue I have had to add the record using 2 statements (1 insert and 1 update) :
INSERT INTO H21ADMIN.H21_RC_WATCH_DAT
VALUES (mnew_seq, mytable, NULL);
sql_stmt := 'UPDATE H21ADMIN.H21_RC_WATCH_DAT SET ROW_COUNT = '
|| ' (SELECT COUNT(*) FROM '|| mytable ||' ) WHERE SEQ_NUM = '
|| mnew_seq || ' AND TABLE_NAME = ''' || mytable || ''' ';
EXECUTE IMMEDIATE sql_stmt;
If someone have a better solution.... Ideas are welcome :)
Christophe.
|
|
|
Re: EXECUTE IMMEDIATE question [message #7465 is a reply to message #7459] |
Mon, 16 June 2003 10:48 |
Todd Barry
Messages: 4819 Registered: August 2001
|
Senior Member |
|
|
Christophe, you must have done something differently. Note how the bind variable is not interpreted as a column name - it is treated correctly as a literal value:
sql>create table t (table_name varchar2(30), num_rows int);
Table created.
sql>declare
2 v_table varchar2(30) := 'ALL_OBJECTS';
3 begin
4 execute immediate 'insert into t select :tn, count(*) from ' || v_table using v_table;
5 end;
6 /
PL/SQL procedure successfully completed.
sql>select * from t;
TABLE_NAME NUM_ROWS
------------------------------ ---------
ALL_OBJECTS 26060
1 row selected.
|
|
|