BULK SELECT EXECUTE IMMEDIATE
I want to update 3 lakhs record in EXP__REQUEST_SK table
How to use bulk select "execute immedaite" in PLSQL
Update EXP__Request_sk set R_CUSTOMERNAME = cust_name where BFG_CUSTOMERID=cust_id;
Note: this code is not tested.
create or replace procedure generic_update (
tab_name varchar2, -- table to update
field_to_update varchar2, -- column to update
field_value varchar2, -- new column value
id_field_name varchar2, -- id column name
id_value number) -- id value to identify the record
as
--local variables section
stmt varchar2(4000) := '';
begin
stmt := 'update ' || tab_name || ' set ' || field_to_update || ' = :f_value where '
|| id_field_name || ' = :i_value';
savepoint txn1;
execute immediate stmt using field_value, id_value;
execute immediate ('commit');
dbms_output.put_line('Update is done.');
exception
when others then
rollback to txn1;
dbms_output.put_line('Update has been rolled back.');
end;
/Even though I provided this, static SQL is always faster than PL/SQL because there is no context switch.
An alternative to the above is to write all static SQLs into a script and run it.
However, if you don't know which tables and column to update before hand, and the update depends on user inputs (e.g. from a JSP), then Java can call a stored procedure to perform the update.
Edited by: pl/sql novice on Nov 20, 2008 11:24 PM
Similar Messages
-
Returning (bulk collect) clause with execute immediate
db version 11.1.0.7
trying to do a returning bulk collect but it is not working:
-- my test table
create table t as
with f as (select rownum rn from dual connect by rownum <= 10)
select
rn,
lpad('x',10,'x') pad
from f;
-- works as expected
declare
type aat is table of t%rowtype;
aay aat;
begin
delete from t returning rn,pad bulk collect into aay;
rollback;
end;
-- but the table I really want to do has many columns so I want to dynamically build list of columns for the
-- returning clause. This way if the table changes the stored proc will not have to be modified
-- fails PLS-00429: unsupported feature with RETURNING clause
declare
type aat is table of t%rowtype;
aay aat;
s varchar2(4000);
begin
s := 'delete from t returning rn,pad into :1';
execute immediate s returning bulk collect into aay;
rollback;
end;
-- tried a few other things:
create or replace type t_obj as object (rn number,pad varchar2(10));
-- PLS-00497: cannot mix between single row and multi-row (BULK) in INTO list
declare
nt t_obj;
s varchar2(4000);
begin
s := 'delete from t returning t_obj(rn,pad) into :1';
execute immediate s returning bulk collect into nt;
rollback;
end;
-- works, but would require store proc changes if the table changes
declare
type t is table of number;
type v is table of varchar2(10);
vt v;
nt t;
s varchar2(4000);
begin
s := 'update t set rn = 10 returning rn,pad into :1,:2';
execute immediate s returning bulk collect into nt,vt;
rollback;
end;
/basically I want to dynamically build the list of columns with all_tab_cols and put the list into the returning clause
but seems like I will have to hard code the column lists. This means whenever the table changes I will have to
modify the store proc .. Any way around this?
ThanksAnd with object type you were almost there. You forgot to create table of objects type:
SQL> create or replace type t_obj as object (rn number,pad varchar2(10));
2 /
Type created.
SQL> declare
2 type aat is table of nt;
3 aay aat;
4 s varchar2(4000);
5 begin
6 s := 'delete from t returning
SQL> declare
2 type aat is table of t_obj;
3 aay aat;
4 s varchar2(4000);
5 begin
6 s := 'delete from t returning t_obj(rn,pad) into :1';
7 execute immediate s returning bulk collect into aay;
8 rollback;
9 end;
10 /
PL/SQL procedure successfully completed.
SQL> SY. -
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> / -
Using execute immediate for select
Hi all,
i am writing a procedure as follows:
create or replace procedure ( run_yr_mo in number) as
v_count number := 0;
begin
execute immediate 'select count(*)
from all_tables
where owner = ''MGR''
and table_name = ''FC_PR_'''||run_yr_mo||''
into v_count;
dbms_output.put_line(v_count);
end;
this is returning me an error at the execute immediate.
run_yr_mo = 200912
Can someone help me out with this.
ThanksJust to show it working:
SQL> create or replace procedure myproc ( run_yr_mo in number) as
2 v_count number := 0;
3 begin
4 select count(*)
5 into v_count
6 from all_tables
7 where owner = 'MGR'
8 and table_name = 'FC_PR_'||to_char(run_yr_mo,'fm999999');
9
10 dbms_output.put_line(v_count);
11 end;
12 /
Procedure created.
SQL> set serverout on
SQL> exec myproc('200910');
0
PL/SQL procedure successfully completed.
SQL> -
How to take output of execute immediate 'select * from table' into a file ?
hi all,
below is my code .....
declare
var_column_name varchar2(2000);
main_string varchar2(12000);
var_table_name varchar2(30);
base_string varchar2(2000);
final_string varchar2(2000);
cursor c1 is
select
object_name
from user_objects
where object_type in ('TABLE','VIEW') and rownum < 2 ;
cursor c2 is
select
column_name
from user_tab_columns
where upper(table_name) = upper(var_table_name);
begin
--var_column_name := null;
-- main_string :=null;
-- table_name :=null ;
-- base_string :=null;
-- final_string := null;
open c1;
fetch c1 into var_table_name;
close c1;
for c2_var in c2
loop
main_string := c2_var.column_name||','||main_string;
end loop;
select rtrim(main_string,',') into final_string from dual;
dbms_output.put_line(final_string);
base_string := 'Select '||final_string ||' from '||var_table_name||'' ;
dbms_output.put_line(base_string);
spool tete.lst;
execute immediate base_string;
spool off;
end;
i want to take the output of the execute immediate in a file on unix server ....
please suggest
rgds
sWere you looking for something like this?
SELECT
CASE WHEN Columns.Column_Id = 1 THEN 'SELECT ' || CHR(10) END
|| CHR(09) || Columns.Column_Name
|| CASE
WHEN Columns.Column_Id = Info.Total_Columns THEN
CHR(10) || 'FROM'
|| CHR(10) || CHR(09) || Columns.Table_Name || ';'
|| CHR(10)
ELSE ','
END Statement
FROM
User_Tab_Columns Columns,
SELECT
Table_Name,
MAX(Column_Id) Total_Columns
FROM
User_Tab_Columns
GROUP BY
Table_Name
) Info
WHERE
Columns.Table_Name = Info.Table_Name
ORDER BY
Columns.Table_Name,
Columns.Column_Id; -
EXECUTE IMMEDIATE AND BULK INSERT
Hi,
I have a code
EXECUTE IMMEDIATE 'INSERT INTO PRC_ExcelDocumentStore_T(object_id,seg_index,segment,value,seg_length) VALUES (:object_id,:seg_index,:segment,:value,:seg_length)'
USING SELF.object_id,p_index,p_segment,p_value,lengthc(v_value)+2;
But due to some performance issue ,I can't use like this.Can you please suggest can i use Bulk Inserts instead of 'EXECUTE IMMEDIATE' statement.
If I can do,please give me an example.
Thanks in advanceuser10619377 wrote:
Hi,
I have a code
EXECUTE IMMEDIATE 'INSERT INTO PRC_ExcelDocumentStore_T(object_id,seg_index,segment,value,seg_length) VALUES (:object_id,:seg_index,:segment,:value,:seg_length)'
USING SELF.object_id,p_index,p_segment,p_value,lengthc(v_value)+2;
But due to some performance issue ,I can't use like this.Can you please suggest can i use Bulk Inserts instead of 'EXECUTE IMMEDIATE' statement.
If I can do,please give me an example.How can we give example when we don't know what you have now or what exactly needs to be done.? -
How to use using clause in execute immediate statement??
Hi ALL,
Can u help me ....
This is the code which I have written...
declare
type rec_typ is table of forall_test%rowtype;
v_rectype rec_typ:=rec_typ();
begin
--poputating records
for i in 1..10000 loop
v_rectype.extend;
v_rectype(v_rectype.last).id:=i;
v_rectype(v_rectype.last).code:=to_char(i);
v_rectype(v_rectype.last).description:='Description :'||to_char(i);
end loop;
execute immediate 'truncate table forall_test';
forall i in v_rectype.first..v_rectype.last
execute immediate 'insert into forall_test values :1' using v_rectype(i);
commit;
end;
But I am getting this ERROR....
execute immediate 'insert into forall_test values :1' using v_rectype(i);
ERROR at line 14:
ORA-06550: line 14, column 61:
PLS-00457: expressions have to be of SQL types
ORA-06550: line 14, column 1:
PL/SQL: Statement ignored
Thanks & Regards,
T.HalderThatmeans using statement cannot be a non sql type.True: You need an sql type for this:
e.g. with
create or replace type emp_typ
as
object (empno number (4),
ename varchar2 (10 byte),
job varchar2 (9 byte),
mgr number (4),
hiredate date,
sal number (7, 2),
comm number (7, 2),
deptno number (2))
create or replace type emp_tab as table of emp_typ
/you can do
--- an empty test table
SQL> create table emp2
as
select *
from emp
where 1 = 2
Table created.
SQL> declare
emp2_tab emp_tab;
begin
/* fill the collection */
select emp_typ (empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno)
bulk collect into emp2_tab
from emp
where empno like '77%';
-- forall loop
forall c in 1 .. emp2_tab.count
execute immediate 'begin
insert into emp2 select * from table(cast(emp_tab(:1) as emp_tab)) t;
update emp2 set sal = null where empno = (:1).empno and empno = 7788;
end;' using emp2_tab (c);
end;
PL/SQL procedure successfully completed.
SQL> select empno, ename, sal from emp2
EMPNO ENAME SAL
7782 CLARK 2450
7788 SCOTT
2 rows selected. -
How how to escape double quation in execute immediate in trigger
Hi all,
please inform me what is the mistake in this procedure.
i think the problem in how to escape double quation.
SQL> create or replace procedure P2
2 is
3 begin
4 execute immediate ' create or replace trigger t2 '
5 ||' before insert '
6 ||' on tb_test'
7 ||' for each row '
8 ||' declare '
9 ||' begin'
*10 ||' execute immediate ''create table t1 as select distinct(NVL(soundex(namess),'''NONE''')) from test';'*
11 ||' end;'
12 end;
13
14 /
Warning: Procedure created with compilation errors.
SQL> show error
Errors for PROCEDURE P2:
LINE/COL ERROR
10/83 PLS-00103: Encountered the symbol "NONE" when expecting one of
the following:
* & = - + ; < / > at in is mod remainder not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like like2 like4 likec between into using || bulk member
submultiset
SQL>See 'Text Literals' in the SQL Language doc for how to use alternative quoting
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements003.htm
>
Here are some valid text literals:
'Hello'
'ORACLE.dbs'
'Jackie''s raincoat'
'09-MAR-98'
N'nchar literal'
Here are some valid text literals using the alternative quoting mechanism:
q'!name LIKE '%DBMS_%%'!'
q'<'So,' she said, 'It's finished.'>'
q'{SELECT * FROM employees WHERE last_name = 'Smith';}'
nq'ï Ÿ1234 ï'
q'"name like '['"' -
How to use cursor in EXECUTE IMMEDIATE command.
i.e. I have a dynamic SELECT query where I will get huge records while using EXECUTE IMEDIATE command, which I want that to be in a cursor.
Can I do this ? If yes how ?Hi, it works perferctly fine with me. Oracle version 10.2.0.4
SQL> --create table t as select * from user_objects; ----->>> My mistake, there is no owner column in the table user_objects
SQL> create table t as select * from all_objects;
Table created.
SQL> CREATE OR REPLACE PROCEDURE dynamic_table (
2 table_name IN VARCHAR2,
condition IN VARCHAR2 DEFAULT NULL) AS
3 4 where_clause VARCHAR2(100) := ' WHERE ' || condition;
5 v_table varchar2(30);
6 type v_typet is table of t%rowtype index by pls_integer;
7 v_tableT v_typeT;
8 BEGIN
-- first make sure that the table actually exists; if not, raise an exception
SELECT OBJECT_NAME INTO v_table FROM USER_OBJECTS
9 10 11 where object_name = upper(table_name) and object_type = 'TABLE';
if condition is null then where_clause := ' order by object_type'; end if;
12 13 execute immediate 'SELECT * from ' || v_table || where_clause bulk collect into v_tablet;
14 for indx in 1..v_tablet.count loop
dbms_output.put_line(v_tablet(indx).owner || ' ' || v_tablet(indx).status);
15 16 end loop;
17 EXCEPTION
18 when no_data_found then
19 dbms_output.put_line ('Invalid table: ' || table_name);
20 end;
21 /
Procedure created.
begin
dynamic_table('T', '');
--Enter the table name you want for the 1st parameter and the condition for the second parameter.
--I think you just want to switch from tables where you want to select.
end;
/Edited by: Spongebob on May 25, 2010 1:33 PM -
Unable to INSERT PL/SQL record with EXECUTE IMMEDIATE
Hi All,
I am selecting data from a source table and after some modification inserting into a target table. Source and target table name are available at run time. You can say only source table structure is fixed.
I have crated a pl/sql table of type source record and inserting record by record in target table using execute immediate. But I am not able to write
EXECUTE IMMEDIATE string USING pl_sql_table(index); and getting error as
PLS-00457: expressions have to be of SQL types
Please see the part of code below. Is it possible to use FORALL with dynamic sql like
FORALL pl_sql_table.FIRST .. pl_sql_table.COUNT
EXECUTE IMMEDIATE .... pl_sql_table(j); -- Like this.
Please suggest why I am not able to write record here. I also want to replace 'INSERT in a loop' with a single INSERT statement out of the loop, to upload whole pl_sql table into target table in one go.
Thanks,
Ravi
DECLARE
TYPE rec_tab_CMP IS RECORD
model_id NUMBER(38),
absolute_rank NUMBER(5)
v_rec_tab_CMP rec_tab_CMP;
TYPE t_rec_tab_CMP IS TABLE OF v_rec_tab_CMP%TYPE INDEX BY BINARY_INTEGER;
v_records_CMP t_rec_tab_CMP;
rc SYS_REFCURSOR;
v_old_table_name VARCHAR2(30); -- passed from parameter
v_new_table_name VARCHAR2(30); -- passed from parameter
dyn_str VARCHAR2(500);
v_columns_str VARCHAR2(200) := ' model_id, absolute_rank ';
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE '|| v_new_table_name || ' AS SELECT * FROM ' || v_old_table_name ||' WHERE 1 = 2 ' ;
OPEN rc FOR 'SELECT '|| v_columns_str ||' FROM '|| v_old_table_name;
FETCH rc BULK COLLECT INTO v_records_CMP;
FOR j IN 1..v_records_CMP.COUNT
LOOP
v_records_CMP(j).model_id := 1; -- Do someting here, This thing can be performed in SQL stmt directly.
dyn_str := 'INSERT INTO '|| v_new_table_name ||' ( '|| v_columns_str || ' ) VALUES (:1, :2) ';
EXECUTE IMMEDIATE dyn_str USING v_records_CMP(j).model_id ,
v_records_CMP(j).absolute_rank ;
-- Here in place of two columns I want to use one record like
-- EXECUTE IMMEDIATE dyn_str USING v_records_CMP(j);
-- But it is giving me error like
-- EXECUTE IMMEDIATE dyn_str USING v_records_st(j);
-- PLS-00457: expressions have to be of SQL types
END LOOP;
CLOSE rc;
END;
/You cannot bind PL/SQL record types to dynamic SQL.
Possibly you could work around this by declaring the INDEX-BY table of records at package specification level, e.g.
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
SQL> CREATE PACKAGE package_name
2 AS
3 TYPE tt_emp IS TABLE OF emp%ROWTYPE;
4 t_emp tt_emp;
5 END package_name;
6 /
Package created.
SQL> CREATE TABLE new_emp
2 AS
3 SELECT *
4 FROM emp
5 WHERE 1 = 0;
Table created.
SQL> DECLARE
2 v_table_name user_tables.table_name%TYPE := 'NEW_EMP';
3 BEGIN
4 SELECT *
5 BULK COLLECT INTO package_name.t_emp
6 FROM emp;
7
8 EXECUTE IMMEDIATE
9 'BEGIN ' ||
10 ' FORALL i IN 1 ..package_name.t_emp.COUNT ' ||
11 ' INSERT INTO ' || v_table_name ||
12 ' VALUES package_name.t_emp (i); ' ||
13 'END;';
14 END;
15 /
PL/SQL procedure successfully completed.
SQL> SELECT empno, ename
2 FROM new_emp;
EMPNO ENAME
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
SQL> -
Execute immediate problem...
Hi, who knows, what is wrong, please?
PROCEDURE VYKONEJ (var_from IN NUMBER, var_to IN NUMBER) IS
TYPE tab_of_commands IS TABLE OF PRIKAZY.PRIKAZ%TYPE;
prikazy_array tab_of_commands; --this is table with sql commands (strings like 'select * from x;')
BEGIN
SELECT PRIKAZ
BULK COLLECT INTO prikazy
FROM PRIKAZY
WHERE ID >= var_from and ID <= var_to;
FORALL i IN prikazy.FIRST .. prikazy.LAST
-- EXECUTE IMMEDIATE 'EXECUTE IMMEDIATE '':prikazy_array'';' USING prikazy_array(i);
EXECUTE IMMEDIATE 'BEGIN '||prikazy_array(i)||' END;';
--what is wrong with excecute command pls?
END VYKONEJ;
Can enyone help me, please?--what is wrong with excecute command pls?Check this:
SQL> declare
type tab_of_commands is table of emp.ename%type;
prikazy tab_of_commands;
begin
select ename
bulk collect into prikazy
from emp;
forall i in prikazy.first .. prikazy.last
execute immediate 'BEGIN dbms_output.put_line(:1); END;'
using prikazy (i);
end;
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
PL/SQL procedure successfully completed. -
Dynamic BULK SELECT/MODIFY
Hi,
When using EXECUTE IMMEDIATE with a dynamically
built SELECT/INSERT statement with varying
tablename, fieldnames, fieldtypes and fieldcounts
how do I make the BULK COLLECT INTO/USING parts
dynamic too?
Can I use RECTYPE arrays if the type depends on
the dynamic statement? Kind of like (pseudo-code)
procedure FETCH(p_Stmt VARCHAR2)
valueType TABLE OF p_Stmt%REC_TYPE INDEXED BY
BINARY_INTEGER;
values valueType;
LOOP
EXECUTE IMMEDIATE p_Stmt
BULK COLLECT INTO values LIMIT 1000;
FOR values LOOP
-- use values.a and values.b
END LOOP
END LOOP;
FETCH('SELECT a,b FROM t');
If I'm forced to use DBMS_SQL and bind OUT/IN arrays
of different types do I need to pre-allocate arrays
of each type against the chance that up to (say)
a 100 DATE columns end up in need of beeing bound?
Or can I reuse one array per type because the methods
copy the arrays? Or is there a generic type that can
be used to bind whatever type the column has?
regards,
SchenkeHi Justin,
So I gathered (my question mentioned this option).
I only had gotten the impression from some comments
that DBMS_SQL may be slower than EXECUTE IMMEDIATE
because of the extra layer of flexibility. Maybe
there will be more parsing too.
There seems to be no way to define the arrays needed
for the DBMS_SQL-IN/OUT-binds dynamically. I don't
think reusing the same array for different columns
will work either. That means arrays must be defined
against all possible columns or at least enough of
each type with a dynamic allocation process (i.e.
count the number of columns for each type and assign
arrays with names like date1, date2, date3, ... to
the 1., 2., 3... DATE column in the statement.)
regards,
Schenke -
ORA-01006 in execute immediate
Hi Everyone!
Please help!
What's wrong with this code?
create or replace procedure rgo_test_forall
as
v_actie varchar2( 200 ) := 'BEGIN';
cursor c_sjv
is
select 1 netb_totaal
, 'prf' profiel
, 'naam' netbeheerder
from dual;
type tt_sjv is table of c_sjv%rowtype
index by binary_integer;
t_sjv_netb tt_sjv;
l_idx_netb pls_integer;
cursor c_sjv_ean(
b_profiel in varchar2
, b_netbeheerder in varchar2
, b_netb_totaal in number )
is
select 1 ean_totaal
, 1 / b_netb_totaal fractie
, 'loc' locatie
, 'prf' profiel
, 'ptf' portfolio
, b_netbeheerder netbeheerder
from dual;
type tt_sjv_ean is table of c_sjv_ean%rowtype
index by binary_integer;
t_sjv_ean tt_sjv_ean;
l_idx_ean pls_integer;
type tt_fractie is table of number
index by binary_integer;
type tt_netbeheerder is table of varchar2( 400 )
index by binary_integer;
type tt_profiel is table of varchar2( 400 )
index by binary_integer;
t_fractie tt_fractie;
t_netbeheerder tt_netbeheerder;
t_profiel tt_profiel;
v_maand date := to_date( '200711', 'yyyymm' );
v_aantal_toegevoegd pls_integer := 0;
v_fractie number;
procedure splits_tabel(
origtable_in in tt_sjv_ean
, fractie_tab_out out tt_fractie
, netb_tab_out out tt_netbeheerder
, profiel_tab_out out tt_profiel )
is
l_idx pls_integer := origtable_in.first;
begin
while l_idx is not null
loop
fractie_tab_out( l_idx ) := origtable_in( l_idx ).fractie;
netb_tab_out( l_idx ) := origtable_in( l_idx ).netbeheerder;
profiel_tab_out( l_idx ) := origtable_in( l_idx ).profiel;
l_idx := origtable_in.next( l_idx );
end loop;
end splits_tabel;
begin
open c_sjv;
loop
v_actie := 'bulk collect';
fetch c_sjv
bulk collect into t_sjv_netb limit 1000;
-- only 200 rows are expected
exit when t_sjv_netb.count = 0;
l_idx_netb := t_sjv_netb.first;
while l_idx_netb is not null
loop
open c_sjv_ean(
t_sjv_netb( l_idx_netb ).profiel
, t_sjv_netb( l_idx_netb ).netbeheerder
, t_sjv_netb( l_idx_netb ).netb_totaal );
loop
v_actie := 'bulk collect EAN';
fetch c_sjv_ean
bulk collect into t_sjv_ean limit 10000;
exit when t_sjv_ean.count = 0;
v_actie := 'splits tabel';
splits_tabel(
origtable_in => t_sjv_ean
, fractie_tab_out => t_fractie
, netb_tab_out => t_netbeheerder
, profiel_tab_out => t_profiel );
v_actie := 'forall insert';
forall i in t_sjv_ean.first .. t_sjv_ean.last
execute immediate
'insert into rgot( ptf, volume_plat ) values( ' ||
' substr( '':netbeheerder'' || '' '' || '':profiel'', 1, 30 ) ' ||
' , '':fractie'' )'
using t_netbeheerder( i ), t_profiel( i ), t_fractie( i );
end loop;
l_idx_netb := t_sjv_netb.next( l_idx_netb );
end loop;
end loop;
exception
when others
then
dbms_output.put_line( 'OTHERS: ' || sqlcode || ': ' || substr( sqlerrm, 1, 100 ) );
end rgo_test_forall;
sho errI get ORA-01006 when I run it. I have tried replacing the execute immediate statement with the code below, but that way I cannot compile the code.
forall i in t_sjv_ean.first .. t_sjv_ean.last
execute immediate
'insert into rgot( ptf, volume_plat ) values( ' ||
' substr( '''||:netbeheerder||''' || '' '' || '''||:profiel||''', 1, 30 ) ' ||
' , '''||:fractie||''' )'
using t_netbeheerder( i ), t_profiel( i ), t_fractie( i );Next, I tried with the code below, but then no row was inserted.
forall i in t_sjv_ean.first .. t_sjv_ean.last
execute immediate
'insert into rgot( ptf, volume_plat ) values( ' ||
' substr( ''''||:netbeheerder||'''' || '' '' || ''''||:profiel||'''', 1, 30 ) ' ||
' , ''''||:fractie||'''' )'
using t_netbeheerder( i ), t_profiel( i ), t_fractie( i );Any ideas?
By the way: the cursor definitions have been simplified for readability, of course.
Message was edited by:
RemcoGorisI am sorry, I am but a simple silly little developer who doesn't have good ideas.
Since you are someone who has good ideas, I would consider myself a very very happy man if you were to tell me what the result of the 'debugging' should be.
This?
INSERT INTO rgot(
ptf
, volume_plat )
VALUES(
substr( :netbeheerder || ' ' || :profiel, 1, 30 )
, :fractie );Humble thanks! -
How to see the data from the execute immediate o/p
Hello
i tried the following code
SQL> declare
2 vSql varchar2(3000);
3 begin
4 vSql:='SELECT';
5 for i in (select column_name from user_tab_columns where table_name='EMP' order by column_name
6 loop
7 vSql:=vSql||i.column_name||',';
8 end loop;
9 execute immediate rtrim(vSql)||' '||'from emp';
10 end;
11 /
declare
ERROR at line 1:
ORA-00900: invalid SQL statement
ORA-06512: at line 9Please guide me to solve this error
Edited by: josh1612 on Jun 22, 2010 3:58 AMjosh1612 wrote:
I need to execute that printed statment select ... from emp
You are already executing it by using:
execute immediate rtrim(vSql,',')||' from emp';What you probably want is to display results, right? Then you can either use bulk collect into clause in execute immediate or use cursor variable. Bulk collect example:
set serveroutput on
declare
vSql varchar2(3000);
type emp_rec_tbl_type is table of emp%rowtype;
emp_rec_tbl emp_rec_tbl_type;
begin
vSql:='SELECT ';
for i in (select column_name from user_tab_columns where table_name='EMP' order by column_id)
loop
vSql:=vSql||i.column_name||',';
end loop;
dbms_output.put_line(rtrim(vSql,',')||' from emp');
execute immediate rtrim(vSql,',')||' from emp' bulk collect into emp_rec_tbl;
for i in 1..emp_rec_tbl.count loop
dbms_output.put_line(rpad(emp_rec_tbl(i).ename,10) || emp_rec_tbl(i).sal);
end loop;
end;
SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from emp
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
PL/SQL procedure successfully completed.
SQL> Cursor variable example:
set serveroutput on
declare
vSql varchar2(3000);
emp_rec emp%rowtype;
cv sys_refcursor;
begin
vSql:='SELECT ';
for i in (select column_name from user_tab_columns where table_name='EMP' order by column_id)
loop
vSql:=vSql||i.column_name||',';
end loop;
dbms_output.put_line(rtrim(vSql,',')||' from emp');
open cv for rtrim(vSql,',')||' from emp';
loop
fetch cv into emp_rec;
exit when cv%notfound;
dbms_output.put_line(rpad(emp_rec.ename,10) || emp_rec.sal);
end loop;
close cv;
end;
SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from emp
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
PL/SQL procedure successfully completed.
SQL> SY. -
Hello all,
I am having a query using Execute Immediate where I want to read values in an array, here is my query:
declare
type emp_name is varray(25) of varchar2(30);
emp_n emp_name;
begin
Execute immediate 'Select emp_name bulk collect from emp ' into emp_n;
end;
but it is giving me an error: "experession "emp_n" in the INTO list is of wrong type."
please tell me what is the issue.
ThanksTry this:
create synonym emp for scott.emp;
declare
type emp_name is varray(25) of varchar2(30);
emp_n emp_name;
begin
Execute immediate 'Select ename from emp ' bulk collect into emp_n;
FOR i IN 1 .. emp_n.LAST loop
dbms_output.put_line( emp_n(i) );
end loop;
END;
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
KING
TURNER
JAMES
FORD
MILLER
SCOTT
ADAMS
Maybe you are looking for
-
Please contact me and explain me the reason of that, asap.
-
Similar code to n=n+1 (n=n+x) with conditional variable to stop
This may be an simple algorithm, but I can't do it. I need to do a VI with an algorithm so similar to n=n+1 , but I need 2 variables, something like n=n+x, and other conditional variable (numerical control). I need to enter the values n and x with a
-
At the top of the Firefox window, click on the Firefox button (File menu in Windows XP) and then click ExitOn the menu bar, click on the Firefox menu and select Quit FirefoxAt the top of the Firefox window, click on the File menu and select QuitAt th
-
I cannot add shortcuts to my desktop using your suggested way!
When I drag the icon to the desktop, it looks fine, but when I release the mouse button. there is no shortcut there
-
Defining Same JNDI Locations for Basic-XA-EJB
I'm working on a legacy system running on Oracle Internet Application Server, for performance improvements. When I look at data-sources.xml file I see that; <data-source...... location="jdbc/ProjectNameDS" xa-location="jdbc/ProjectNameDS" ejb-locatio