Problem in Using of EXECUTE IMMEDIATE clause
HelloI tried using using the clause, but am getting an error. Any help is welcome.
Here is the code
declare
number of g_salary;
v_stm varchar2 (100);
Start
v_stm: = "insert into emp (empno, deptno, sal) values (: 1,: 2: 3)"; "
execute immediate v_stm using empno, sal, deptno;
end;
Here is the error message
ERROR on line 1:
ORA-06550: line 6, column 31:
PLS-00201: identifier 'EMPNO' must be declared.
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
You must provide values that must be inserted into the table.
something like
declare
g_salary number ;
v_stm varchar2(100);
begin
v_stm := 'insert into emp (empno, deptno, sal) values (:1, :2, :3)';
execute immediate v_stm using 10, 20, 30;
end;
not tested
Tags: Database
Similar Questions
-
Creating external Tables using the EXECUTE IMMEDIATE in PL/SQL
Hi guys,.
I am trying to create an external Table using the EXECUTE IMMEDIATE in a procedure and I managed to compile and no errors were generated. But when I try to run it from sql using the exec command I get the following error:
------------------------------------------------------------------------
ERROR on line 5:
ORA-00911: invalid character
ORA-06512: at "GEO. TEST_DDL', line 4
ORA-06512: at line 5
-------------------------------------------------------------------------
I tried to check the whole statement to create the external table, but I can't find where is the error. Surprisingly, if I simply run the command table create external on sqlplus it works, but not a procedure.
If anyone can help with ideas or experience?
Geoffrey KossamiThe error means that there is an identifier somewhere that starts with a nonalphanumeric. This is a typical mistake of editing. A procedure compiles correctly is not of course because the underlying dynamic sql running is OK. Which of course only be resolved when you try to run it.
There is certainly a problem with the text you provide to be run as a piece of dynamic sql code. You should try to watch it with dbms_output and run this code in sqlplus. But your problem is with the code you run as dynamic PL/SQL, it is not itself compilable.
Jack
-
use of execute immediate with variables
Hello
I want to update a table with immediate execution, but I need to use the variable to run immediately. I typed a code, but the code below does not work and I m getting the error ORA-00936. Something wrong with this code?
WHILE v_acu_payment_amount > 0
LOOP
BEGIN
SELECT emplid, amount_past_due
IN v_emplid, v_vade_tutari
OF vade_temp
WHERE the sira = v_sirano AND emplid IN (r1.emplid);
END;
v_acu_payment_amount: = v_acu_payment_amount - v_vade_tutari;
Dbms_output.put_line ('paymentamount2' | v_acu_payment_amount);
IF v_acu_payment_amount > 0
THEN
EXECUTE IMMEDIATE "update vade_temp set AMOUNT_PAST_DUE = to_number('||) 0
|| ') where sira = to_number (')
|| v_sirano
|| ')';
ON THE OTHER
Temp: = v_acu_payment_amount * (-1);
Dbms_output.put_line (temp);
EXECUTE IMMEDIATE "update vade_temp set AMOUNT_PAST_DUE = to_number (')"
|| Temp
|| ') where sira = to_number (')
|| v_sirano
|| ')';
END IF;
COMMIT;
v_sirano: = v_sirano + 1;
END LOOP;
Kind regards
Gunce
In addition, you have no dynamic SQL for this
[code]
WHILE v_acu_payment_amount > 0
LOOP
BEGIN
SELECT emplid, amount_past_due
IN v_emplid, v_vade_tutari
OF vade_temp
WHERE the sira = v_sirano AND emplid IN (r1.emplid);
END;
v_acu_payment_amount: = v_acu_payment_amount - v_vade_tutari;
Dbms_output.put_line ('paymentamount2' | v_acu_payment_amount);
IF v_acu_payment_amount > 0
THEN
UPDATE vade_temp
SET amount_past_due = 0
WHERE the sira = v_sirano;
ON THE OTHER
Temp: = v_acu_payment_amount * (-1);
Dbms_output.put_line (temp);
UPDATE vade_temp
SET AMOUNT_PAST_DUE = temp
where sira = v_sirano;
END IF;
COMMIT;
v_sirano: = v_sirano + 1;
END LOOP;
[/ code]
But if you need dynamic sql statements, then you should use bind variables as
...
EXECUTE IMMEDIATE "UPDATE vade_temp SET amount_past_due =: b1 WHERE sira =: b2' using 0, v_sirano;"
...
HTH
-
Use of EXECUTE IMMEDIATE with XML
Database version: 10.2.0.3.0 - 64bi
Hi all
I have an xml that is stored in a table, xmltype column.
I target insert tables including column names and xml nodes are the same.
using the table of all_tab_columns I will generate columns must be passed to xmltable.
all these data, I'll store them in variables and finally proceed to xmltable as below
I just want to know using execute immediate is good to use XML?
SQL_STMT: = 'insert'. table_name | ' ( '|| V_COLUMN_NAME |') ' ;
SQL_STMT: = SQL_STMT | ' SELECT '. V_XTAB_COLUMN_NAME |
"FOR TO_XML,
XMLTABLE(' || v_xpath ||)
"PASSAGE XML_VALUE
columns | V_COLUMNS_DATA_TYPE
||') XTAB
WHERE Seq_NO = ' | P_SEQUENCE_NO;
RUN IMMEDIATELY SQL_STMT;
Thank you and best regards,
RubyHi Ruby,
I just want to know using execute immediate is good to use XML?
Sorry, but this question does not make much sense.
Whether or not dynamic SQL means XML is not relevant, the statement will end up as a cursor in the shared pool independently and be executed or retrieved as any other.If I were you, I'd be worried on the statement do not use bind variables, or the need for dynamic SQL in the first place.
Why should you use dynamic SQL?
If it's laziness, because there are a lot of columns to write down, then it is a bad reason. -
Problem with bind in Execute Immediate
Hi all
Scenario 1:
It works very well.
Scenario 2:DECLARE V VARCHAR2(20) := ':X'; X VARCHAR2(20) := 'Hello World'; RES VARCHAR2(20); QRY VARCHAR2(100); BEGIN QRY := 'SELECT '||V||' FROM DUAL'; EXECUTE IMMEDIATE QRY INTO RES USING X; DBMS_OUTPUT.PUT_LINE(RES); END;
Necessary 'X' as the output.DECLARE V VARCHAR2(20) := 'X'; X VARCHAR2(20) := 'Hello World'; RES VARCHAR2(20); QRY VARCHAR2(100); BEGIN QRY := 'SELECT '||V||' FROM DUAL'; EXECUTE IMMEDIATE QRY INTO RES USING X; DBMS_OUTPUT.PUT_LINE(RES); END; Error: X invalid identifier.
I could do to make it works very well for both scenarios with small changes in the code?My code can't handle scenario 2.
You can change to sth as follows:
SQL> declare v varchar2 (20) := 'X'; x varchar2 (20) := 'Hello World'; res varchar2 (20); qry varchar2 (100); begin qry := 'SELECT ' || case when v not like ':%' then ':' end || v || ' FROM DUAL'; execute immediate qry into res using case when v not like ':%' then v else x end; dbms_output.put_line (res); end; / X PL/SQL procedure successfully completed. SQL> declare v varchar2 (20) := ':X'; x varchar2 (20) := 'Hello World'; res varchar2 (20); qry varchar2 (100); begin qry := 'SELECT ' || case when v not like ':%' then ':' end || v || ' FROM DUAL'; execute immediate qry into res using case when v not like ':%' then v else x end; dbms_output.put_line (res); end; / Hello World PL/SQL procedure successfully completed.
-
Hi all
I have a statement i.e. Select ename from EMP; I want to run it in execute immediate command.
He return several rows at a time.
How can I run this statement with oracle execute immediate command
Please review...dipuna wrote:
Hi all
I have a statement i.e. Select ename from EMP; I want to run it in execute immediate command.
He return several rows at a time.If you have this EXACT statement, then you need not dynamic nothing :)
dipuna wrote:
How can I run this statement with oracle execute immediate command
Please review...
Examples of both methods (as previously mentioned).
TUBBY_TUBBZ? declare type l_enames_tab is table of scott.emp.ename%type; l_enames_array l_enames_tab ; begin 5 execute immediate 'select ename from scott.emp' bulk collect into l_enames_array; for x in 1 .. l_enames_array.count loop dbms_output.put_line(l_enames_array(x)); end loop; end; / SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER PL/SQL procedure successfully completed. Elapsed: 00:00:00.00 TUBBY_TUBBZ?TUBBY_TUBBZ? declare type l_enames_tab is table of scott.emp.ename%type; l_enames_array l_enames_tab ; l_ref_cursor sys_refcursor; begin 6 open l_ref_cursor for 'select ename from scott.emp'; fetch l_ref_cursor bulk collect into l_enames_array; close l_ref_cursor; for x in 1 .. l_enames_array.count loop dbms_output.put_line(l_enames_array(x)); end loop; end; 19 / SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER PL/SQL procedure successfully completed. Elapsed: 00:00:00.00 TUBBY_TUBBZ?
-
With the help of the POLL with Execute Immediate clause
I wrote a query to update a table and return the column in a variable of type table nested using the return clause but its not working, I am getting error like
ORA-06550: line 66, column 22:
PLS-00306: wrong number or types of arguments in the call to ' |'
ORA-06550: line 66, column 4:
PL/SQL: Statement ignored
I get the error message in part of my request to the suite
|| "RETURN If_row_status in bulk collect INTO"
|| v_if_row_status;
Referred to as v_if_row_status-
TYPE v_count IS the TABLE OF varchar2 (50) INDEX directory.
v_if_row_status v_count;The original message listed error about concatenation, but the code in response to scott.wesley had no concatenation. Have you tried to revise the update process? What is a different issue, unrelated to the first?
The best thing to do would be to create a cursor, loop through it and use a simple update for each row. Updates complex as the one you are trying to use are very difficult to manage. By using a loop to insert/update, you can have control over the process and have access to all values, you must use more later in advance instead of having to use the RETURN clause.
Insert bulk can be more effective, but is difficult to manage for the great updates.
-
use of execute immediate to select
Hi all
I am writing a procedure as follows:
create or replace procedure (run_yr_mo in number) as
number of v_count: = 0;
Start
run immediately "select count (*)
from all_tables
where owner = "MGR"
and table_name = "FC_PR_" '. run_yr_mo | »
in v_count;
dbms_output.put_line (v_count);
end;
This returns me an error when immediate execution.
run_yr_mo = 200912
Can someone help me with this.
Thank youWhy use run immediately?
create or replace procedure myproc ( run_yr_mo in number) as v_count number := 0; begin select count(*) into v_count from all_tables where owner = 'MGR' and table_name = 'FC_PR_'||to_char(run_yr_mo,'fm999999'); dbms_output.put_line(v_count); end;
You missed to give a name to the procedure and you need to format numbers as strings explicitly to avoid possible extra spaces get added at the beginning of the implicit conversion.
-
EXECUTE IMMEDIATE in error >; >; ORA-00972: identifier is too long
Hello
I wrote a code using execute immediate command. This command essentially creates a cursor that selects user_tab_columns table names. These table names are then used inside the statement immediately execute in order to apply the same select statement on the table being selected within the cursor names. The problem is that the above error is thrown saying identifier is too long. I can't understand how to solve this problem.
Please find the below code:
Declare
Cursor C_1 is select distinct table_name
of user_tab_columns
where column_name = 'PROGRAM_UPDATE_DATE' AND table_name LIKE '% MISPA '.
intersect
Select unique table_name
of gsi_daily_count;
table_names varchar2 (100);
Begin
C_1 open;
Loop
Extract the C_1 in table_names;
WHEN THE EXIT C_1% NOTFOUND;
EXECUTE IMMEDIATE "select last_extract_date,
TO_CHAR (min (largest (nvl (last_update_date, "1 January 10"), nvl (program_update_date, "1 January 10"))), "MON-DD-YY HH24:MI:SS") mi.
TO_CHAR (max (greatest (nvl (last_update_date, "1 January 10"), nvl (program_update_date, "1 January 10"))), "MON-DD-YY HH24:MI:SS") my
of ' | table_names |
"Last_extract_date group".
order of last_extract_date desc;';
End loop;
Close C_1;
COMMIT;
End;
/
Help, please.
Kind regards
Hossamdeclare cursor C_1 is select distinct table_name from user_tab_columns where column_name = 'PROGRAM_UPDATE_DATE' and table_name LIKE 'MISPA%' intersect select unique table_name from gsi_daily_count; table_names varchar2(100); begin open C_1; loop fetch C_1 into table_names; exit when c_1%notfound; execute immediate 'select last_extract_date, to_char(min(greatest(nvl(last_update_date,''01-Jan-10''),nvl(program_update_date,''01-Jan-10''))),''DD-MON-YY HH24:MI:SS'') mi, to_char(max(greatest(nvl(last_update_date,''01-Jan-10''),nvl(program_update_date,''01-Jan-10''))),''DD-MON-YY HH24:MI:SS'') ma from'|| table_names || 'group by last_extract_date order by last_extract_date desc;'; end Loop; close C_1; commit; End;
Here is the list of question I see in this code.
1. in the SQL string, you must have a
after the keyword and before the GROUP BY keyword. 2. you should not use ';' in EXECUTE IMMEDIATE. You must remove it.
3. the result set returned by the EXECUTE IMMEDIATE should be stored in a Variable. You use the INTO LEDGES INTO clause or in BULK to this effect.
-
Execute Immediate for an IF statement
All,
I'm trying to run an IF statement dynamically at execution using the Execute Immediate statement, but I am gettign error... The error is PLS-00201: identifier 'V_BARG_UNIT' must be declared
The condition and the result for the case statement is stored in a test_dynamic_if_tbl table that has 2 columns.
condition = v_barg_unit = 'ABC' and result_val = v_bg_group: = "93"
Yet the code...
DECLARE
cmd VARCHAR2 (4000);
v_barg_unit VARCHAR2 (10): = 'ABC ';
v_bg_group VARCHAR2 (10): = ' ';
ABC of the CURSOR
IS
SELECT * FROM test_dynamic_if_tbl;
BEGIN
FOR c1 IN abc
LOOP
cmd: =.
"Start."
If ' | C1.condition
|| "THEN."
|| C1.result_val
|| ' ; End if;
End;';
Dbms_output.put_line ('cmd: ' | cmd);
Cmd EXECUTE IMMEDIATE.
Dbms_output.put_line (' status: ' | c1.condition);
Dbms_output.put_line ('result_value: ' | c1.result_val);
Dbms_output.put_line ('v_bg_group: ' | v_bg_group);
END LOOP;
END;
What I am doing wrong?
Thanks for your help in advance,
Vinay
Published by: user652279 on September 16, 2009 11:45
Published by: user652279 on September 16, 2009 11:47Well, dynamic sql is executed in a separate context and scope. V_barg_unit stated in your code is therefore not visible in the dynamic PL/SQL block you run. You can pass block dynamic PL/SQL using the variable binding and the USING clause:
DECLARE cmd VARCHAR2 (4000); v_barg_unit VARCHAR2 (10) := 'ABC'; v_bg_group VARCHAR2 (10) := ' '; CURSOR abc IS SELECT * FROM test_dynamic_if_tbl; BEGIN FOR c1 IN abc LOOP cmd := 'declare v_barg_unit VARCHAR2 (10); Begin v_barg_unit := :v_barg_unit; If ' || c1.condition || ' THEN ' || c1.result_val || ' ; End if; End;'; DBMS_OUTPUT.put_line ('cmd: ' || cmd); EXECUTE IMMEDIATE cmd USING v_barg_unit,OUT v_bg_group; DBMS_OUTPUT.put_line ('Condition: ' || c1.condition); DBMS_OUTPUT.put_line ('result_value: ' || c1.result_val); DBMS_OUTPUT.put_line ('v_bg_group: ' || v_bg_group); END LOOP; END;
However, since you do not know what name will be stored in test_dynamic_if_tbl my suggestion is not a generic solution.
SY.
Published by: Solomon Yakobson September 16, 2009 12:07
-
How to set multiple parameters in a sql EXECUTE IMMEDIATE
I want to set nls_language and nls_date_language and set them as different languages. fnd_global.set_nls_context () does not work. So I think I can use the EXECUTE IMMEDIATE and add them together into a single statement. But I don't know how to make that happen. Can someone help me? Thank you very much.
PS: It must be done in a single call.928091 wrote:
Hey, thanks for your reply, I forgot to mention that it should be in the begin-end block, do you know how?EXEC is a more SQL API which can be used to execute the PL/SQL code. What it does is put the PL/SQL code in a BEGIN block... END of block and run.
So, you can also manually put code in a BEGIN block... END of block and run like that.
SQL> begin 2 execute immediate q'[begin execute immediate 'alter session set nls_date_language = ''AMERICAN'''; execute immediate 'alter session set nls_language = ''AMERICAN'''; end;]'; 3 end; 4 / PL/SQL procedure successfully completed. SQL>
And I don't understand what possible benefit he made a single run in a BEGIN block... Terminate during execution of multiples.
Why this code is not in your interest, something like this
begin execute immediate 'alter session set nls_date_language = ''AMERICAN'''; execute immediate 'alter session set nls_language = ''AMERICAN'''; end; /
-
dml execution 2 statements at a time with the help of execute immediate
I would like to run 2 DML statements at a time with the help of execute immediate usefulness,.
ex:
update employee set ename = 'Chantal' where eno = 123;
Update dept set dname = 'IT' where dno = 345;
I want the two statement to execute at a time, but I don't want to use any loop as well.
Thanks in advanceYou can use:
execute immediate 'begin update employee set ename='jagadeesh' where eno=123; update dept set dname='IT' where dno=345; end;';
Although updates will always run one after the other.
If you want the simultaneous treatment, then you need to use DBMS_JOB and DBMS_SCHEDULER to plan both work to run to run each of the statements independently.
-
I have plsql-variable with CLOB datatype. It contains the source code of a package body. The size of the variable does not fit into 'varchar2' data type, it is larger.
Now, I want to run a "EXECUTE IMMEDIATE" statement to create my package body defined in my CLOB variable.
How can I use the "EXECUTE IMMEDIATE"?
Note that "EXECUTE IMMEDIATE" does not allow for CLOB-variable as an argument.
Note that "EXECUTE IMMEDIATE" allows "varchar2" - variables as arguments but "varchar2" contains maximum only some 32 KB of data.
--
Maybe it's possible to run something like
"EXECUTE IMMEDIATE v1 | v2 | v3»
where v1, v2, v3 are of type varchar2 and altogether they contain ca 100K of data and execute immediately would succeed?
I wil outside, a s test...I wil outside, a s test...
You may have searched for as well ;)
Re: immediate execution of a CLOB field.
Note: In 11 GR 2 run immediately accepts a clob as well...
-
Execute Immediate and by calling a function
I'm calling a function by using the Execute Immediate, but not sure what the correct syntax for the function's return value after the Execute Immediate runs.
I need the value for further processing in my PL/SQL package.
I tried several methods, but cannot get the right format for this work/compilation.
EExecute Immediate plsql_block back in ShName2;
Have been that plsql_block has the code to:
Declare shname2 varchar2 (4000); Begin shname2: = lurking. ShortNm ('STRUCTURE'); End;SQL> set serveroutput on SQL> SQL> create or replace 2 function fun 3 return varchar2 4 is 5 begin 6 return 'hello'; 7 end fun; 8 / Function created. SQL> SQL> SQL> declare 2 var varchar2(100); 3 begin 4 execute immediate 'begin :this := fun; end;' using in out var; 5 dbms_output.put_line(var); 6 end; 7 / hello PL/SQL procedure successfully completed. SQL>
-
Pass Pl/sql table in the USING clause in the EXECUTE IMMEDIATE statement
Getting error when I try to pass the PL/SQL table in the USING clause in the EXECUTE IMMEDIATE statement:
Declare
result NUMBER;
TYPE values_tab IS TABLE OF NUMBER INDEX OF directory;
lv_tab values_tab;
lv_exp varchar2 (300);
lv_exec varchar2 (300);
BEGIN
lv_tab (1): = 5;
lv_tab (2): = 48;
lv_tab (3): = 7;
lv_tab (4): = 6;
lv_exp: = ': + b1: b2 + (: b3 *: b4)';
lv_exec: = 'SELECT'. lv_exp | ' THE DOUBLE '.
IMMEDIATE EXECUTION
lv_exec
IN
result
USING
lv_tab;
DBMS_OUTPUT. Put_line (result);
END;
/
Error on line 1
ORA-06550: line 20, column 12:
PLS-00457: expressions must be SQL types
ORA-06550: line 15, column 8:
PL/SQL: Statement ignored
I am trying to evaluate the expression ': + b1: b2 + (: b3 *: b4) "which is stored in the table. This table has different expressions (expressions about 300). I want to use the bind variable in the expression because each expression evaluated thousands of time may be more in some cases. If I use bind variable can he fill pool.
Is there a way I can pass parameters with the HELP of (IN) dynamically instead of write "help lv_tab (1), lv_tab (2), lv_tab (3), lv_tab (4)? As number of change of the input parameters depend on the expression in the table.
If it is possible please suggest any other ideas/approaches
Help, please...
Published by: satnam on June 11, 2009 11:50Well, you keep changing faster reqs that I can follow. In any case, assuming that N-th variable bind (left to right) corresponds to n-th collection item:
Declare result NUMBER; lv_tab values_tab := values_tab(); lv_exp varchar2(300); lv_exec varchar2(300); lv_i number := 0; BEGIN lv_tab.extend(4); lv_tab(1) := 5; lv_tab(2) := 48; lv_tab(3) := 7; lv_tab(4) := 6; lv_exp := ':5000135+:5403456+(:5900111*:5200456)'; lv_exec := lv_exp; While regexp_like(lv_exec,':\d+') loop lv_i := lv_i + 1; lv_exec := REGEXP_REPLACE(lv_exec,':\d+',':b(' || lv_i || ')',1,1); end loop; lv_exec := 'BEGIN :a := ' || lv_exec || '; END;'; DBMS_OUTPUT.PUT_LINE(lv_exec); EXECUTE IMMEDIATE lv_exec USING OUT result,IN lv_tab; DBMS_OUTPUT.PUT_LINE(result); END; / BEGIN :a := :b(1)+:b(2)+(:b(3)*:b(4)); END; 95 PL/SQL procedure successfully completed. SQL>
SY.
Maybe you are looking for
-
No Favorites appear in the bar of boomarks
Hello. I decided to try Australis (again). So I updated my Firefox 28.0 to the most recent one (32.0.3). But the bookmarks bar show any of my favorites. When I type CTRL + B, the sidebar presents each of them: they are there, but the bookmarks bar sh
-
Text is so ugly! He is very good in Internet Explorer
Get is the second time that I installed Firefox and I have the same problem - text looks like all Gossamer and aliassy compared to IE. It's the same on all websites (including this one) and so uncomfortable on the eye. What's the point of a browser t
-
Photosmart Pro B9180 'Replace due to defective print head' WARNING
My B9180 gave me the msg "replace the defective print head" after a period of inactivity. I'm using Snow Leopard on a Mac Pro 10.6.8. The printer went through several periods of non-use, since I bought it used, but almost nine. Usually, I experien
-
analog and digital data synchronization
Hi all I would like to help with what I seek to accomplish, if you don't mind much. I'm trying to synchronize the acquisition of analog and digital modes using a common trigger that launches both types of data collection at the same time. What I've d
-
I installed a keyboard wrong driver and now my winxp support with virtually no feature mouse or the keyboard, both frozen. I can't even load windows in SafeMode to solve my problem of driver, because my keyboard lights up until windows is completely