SQL dynamic immediate execution
Hi allWe have a table where we keep the query that will be executed and using bind variables.
But im facing some problems when I try to use it in immediate execution.
guess
function validate is
varaux varchar2 (200);
Start
for r1 in (select query,
using_bind
from table_1) loop
run immediately r1.query in varaux using r1.using_bind;
Return varaux;
end loop;
Returns a null value.
end;
I know that's not the way to do it, but can you give me instructions in the topic...
Thank you
Bruna
Published by: Bruna on January 4, 2013 03:07
>
We have a table where we keep the query that will be executed and using bind variables.
But im facing some problems when I try to use it in immediate execution.
>
No - the problems you are experiencing are because your function uses invalid syntax. The problems have NOTHING to do with 'run immediately '. This works for me and no changes have been made to the dynamic part.
create or replace function fn_validate return varchar2 is
varaux varchar2(200);
begin
for r1 in (select 'select ename from emp where empno = :1' query,
7369 using_bind from dual) loop
execute immediate r1.query into varaux using r1.using_bind;
return varaux;
end loop;
return null;
end;
declare
myVar varchar2(200);
begin
myVar := fn_validate();
dbms_output.put_line(myVar);
end;
SMITH
Tags: Database
Similar Questions
-
sql dynamic and immediate execution
Hello all;
Just curious... Why developers are still using dynamic sql... and run immediately, because I always thought that dynamic sql were negative and the use of immediate execution as well...
or did I miss something...I like this white paper on the topic: SQL in PL/SQL
http://www.Oracle.com/technetwork/database/features/PLSQL/overview/doing-SQL-from-PLSQL-129775.PDF -
HELP-immediate execution in PL/SQL has received the error ORA-00904: invalid identifier
What is the problem with the following codes from PL/SQL (actually it comes to Oracle Metalink Note: 313175.1):
===========
declare
cursor c1 is select * from $ semantic;
v_statement VARCHAR2 (255);
v_nc number (10);
v_nt number (10);
Start
immediate execution
"select count (*) from $ semantics" in v_nc;
immediate execution
' select count (distinct s_table_name) of semantics$ "in v_nt;
dbms_output.put_line
('Edit' | v_nc |) 'columns ' | v_nt | "tables");
to r1 c1 loop
v_statement: = 'ALTER TABLE ' | R1.s_owner | '.' || R1.s_table_name;
v_statement: = v_statement | «change (' |)» R1.s_column_name | ' ';
v_statement: = v_statement | R1.s_data_type | ' (' | r1.s_char_length;)
v_statement: = v_statement | ' CHAR))';
immediately run v_statement;
end loop;
dbms_output.put_line ('Done');
end;
/
=====
Executed once the codes as sysdba against 10gr 2 database, I got this error:
From build to select columns to change
Editing columns 4428 35249
declare
*
ERROR on line 1:
ORA-00904: invalid identifier
ORA-06512: at line 22
I see nothing wrong with the line of "immediate execution". I appreciate your help!
Thank you.Hello
Try to print the offending instruction using exception, I used small test cases by changing the pl/sql block, you may need to change to respond to all other types of data in this table.CREATE TABLE semantics$ AS SELECT USER AS owner, table_name, data_type AS s_data_type, column_name, data_length AS s_char_length FROM cols WHERE table_name = 'MY_OBJECTS'; DECLARE CURSOR c1 IS SELECT * FROM semantics$; v_statement VARCHAR2 (255); v_nc NUMBER (10); v_nt NUMBER (10); BEGIN EXECUTE IMMEDIATE 'select count(*) from semantics$' INTO v_nc; EXECUTE IMMEDIATE 'select count(distinct table_name) from semantics$' INTO v_nt; DBMS_OUTPUT.put_line( 'ALTERing ' || v_nc || ' columns in ' || v_nt || ' tables'); FOR r1 IN c1 LOOP v_statement := 'ALTER TABLE ' || r1.owner || '.' || r1.table_name; v_statement := v_statement || ' modify (' || r1.column_name || ' '; v_statement := v_statement || r1.s_data_type || '(' || r1.s_char_length; IF (r1.s_data_type = 'NUMBER') THEN v_statement := v_statement || '))'; ELSE v_statement := v_statement || ' CHAR))'; END IF; DBMS_OUTPUT.put_line (v_statement); -- EXECUTE IMMEDIATE v_statement; END LOOP; DBMS_OUTPUT.put_line ('Done'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line ('Statement = ' || v_statement); DBMS_OUTPUT.put_line (SUBSTR (SQLERRM, 1, 200)); RAISE; END;
_ Output
ALTERing 13 columns in 1 tables ALTER TABLE MY_OBJECTS modify (OWNER VARCHAR2(30 CHAR)) ALTER TABLE MY_OBJECTS modify (OBJECT_NAME VARCHAR2(30 CHAR)) ALTER TABLE MY_OBJECTS modify (SUBOBJECT_NAME VARCHAR2(30 CHAR)) ALTER TABLE MY_OBJECTS modify (OBJECT_ID NUMBER(22)) ALTER TABLE MY_OBJECTS modify (DATA_OBJECT_ID NUMBER(22)) ALTER TABLE MY_OBJECTS modify (OBJECT_TYPE VARCHAR2(19 CHAR)) ALTER TABLE MY_OBJECTS modify (CREATED DATE(7 CHAR)) ALTER TABLE MY_OBJECTS modify (LAST_DDL_TIME DATE(7 CHAR)) ALTER TABLE MY_OBJECTS modify (TIMESTAMP VARCHAR2(19 CHAR)) ALTER TABLE MY_OBJECTS modify (STATUS VARCHAR2(7 CHAR)) ALTER TABLE MY_OBJECTS modify (TEMPORARY VARCHAR2(1 CHAR)) ALTER TABLE MY_OBJECTS modify (GENERATED VARCHAR2(1 CHAR)) ALTER TABLE MY_OBJECTS modify (SECONDARY VARCHAR2(1 CHAR)) Done
Concerning
Published by: OrionNet on January 5, 2009 23:53
Published by: OrionNet on January 5, 2009 23:55
-
The use of bind variables (in &; out) with sql dynamic
I have a table that contains code snippets to make postings on a set of pl/sql database. what the code does is basically receives an ID and returns a number of errors found.
To run the code, I use dynamic sql with two bind variables.
When codes consists of a simpel query, it works like a charm, for example with this code:
However when I get to post more complexes that must perform calculations or run several queries I run into trouble.BEGIN SELECT COUNT (1) INTO :1 FROM articles atl WHERE ATL.CSE_ID = :2 AND cgp_id IS NULL; END;
I have boiled down the problem into that:
This code is not really make sense, but it's just to show you what is the problem. When I run this code, I get the errorDECLARE counter NUMBER; my_id NUMBER := 61; BEGIN EXECUTE IMMEDIATE (' declare some_var number; begin select 1 into some_var from dual where :2 = 61; :1 := :2; end; ') USING OUT counter, IN my_id; DBMS_OUTPUT.put_line (counter || '-' || my_id); END;
ORA-6537 ON bind variable linked to a position IN
The error doesn't seem wise,: 2 is the only one IN bind variable and it is only used in a where clause clause.
As soon as I remove this where clause, the code works again (giving me 61-61, in case you want to know).
Any idea what goes wrong? I just use bind variables in a way that you're not supposed to use it?
I'm using Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bitCorrection. With immediate execution , the binding is in position, but binds do not need to be repeated. My statement above is incorrect...
You must link only once - but bind by position. And the connection must correspond to the use of the variable binding.
If the connection never variable assigns a value in the code, link by in.
If the binding variable assigns a value in the code, link as OUTPUT.
If the binding variable assigns a value and is used a variable in another statement in the code, link as IN OUT.
For example
SQL> create or replace procedure FooProc is 2 cnt number; 3 id number := 61; 4 begin 5 execute immediate 6 'declare 7 n number; 8 begin 9 select 10 1 into n 11 from dual 12 where :var1 = 61; --// var1 is used as IN 13 14 :var2 := n * :var1; --// var2 is used as OUT and var1 as IN 15 :var2 := -1 * :var2; --// var2 is used as OUT and IN 16 end; 17 ' 18 using 19 in out id, in out cnt; --// must reflect usage above 20 21 DBMS_OUTPUT.put_line ( 'cnt='||cnt || ' id=' || id); 22 end; 23 / Procedure created. SQL> SQL> exec FooProc cnt=-61 id=61 PL/SQL procedure successfully completed. SQL>
-
PL/SQL dynamic dynamic SQL SELECT block vs
Hello
I have a question about the best way to encode a dynamic SELECT INTO statement. Here are the 2 posiibilities I know:
_1. Dynamically, the execution of the SELECT statement and making use of the INTO clause the EXECUTE IMMEDIATE statement_
FUNCTION to CREATE or REPLACE get_num_of_employees (p_loc VARCHAR2, VARCHAR2 p_job)
RETURN NUMBER
IS
v_query_str VARCHAR2 (1000);
v_num_of_employees NUMBER;
BEGIN
v_query_str: = 'SELECT COUNT (*) FROM emp_.
|| p_loc
|| ' WHERE employment =: bind_job';
V_query_str EXECUTE IMMEDIATE
IN v_num_of_employees
Using p_job;
RETURN v_num_of_employees;
END;
/
_2. Encapsulation of the SELECT INTO statement in a block and dynamically exectuting the gabarit@sur
FUNCTION to CREATE or REPLACE get_num_of_employees (p_loc VARCHAR2, VARCHAR2 p_job)
RETURN NUMBER
IS
v_query_str VARCHAR2 (1000);
v_num_of_employees NUMBER;
BEGIN
v_query_str: = ' start
"SELECT INTO COUNT (*): into_bind FROM emp_"
|| p_loc
|| ' WHERE employment =: bind_job;
end;';
V_query_str EXECUTE IMMEDIATE
With the HELP of v_num_of_employees, p_job;
RETURN v_num_of_employees;
END;
/
I was wondering which way would be best? I know that the second method uses a variable binding for the INTO clause, but one also uses link varialbes (No. semicolon)? Differences in terms of efficiency or speed?
Thank you very much
Edited by: BYS2 October 19, 2011 01:23SELECT Dynamics also links the output - it no worries.
The dynamic PL/SQL block would result in a change in additional context of PL/SQL for SQL inside the block. It would be a very slight overhead.
I'd go for the SELECT dynamic - I see no reason to associate a PL/SQL block.
-
difference btw dbms_sql.execution and immediate execution
Hello
IAM using 10g, difference btw immediate.please dbms_sql.execution and execution to tell me.797525 wrote:
IAM using 10g, difference btw immediate.please dbms_sql.execution and execution to tell me.
SQL all (and all the anonymous PL/SQL blocks) are analyzed by Oracle as a SQL cursors. These cursor structures are stored in the Shared Pool in the SGA (shared memory area which are essentially the "brain" of the database instance).
Once a cursor was created for a statement SQL, how to use and interface with this SQL cursor on the server (from the point of view of the customer)?
We use on the client side, a calling interface (or API) that allows our code to interact with this SQL cursor on the server. This interface can be ODBC. Or BDE. Or ADO. Or many other 3-4 letters for database interfaces.
PL/SQL is also a language of 'customer' from a SQL perspective. Such as Java and c#, the code PL/SQL will be calls to the SQL engine to analyze and create SQL cursors.
Of course, PL/SQL differs in 2 areas. It runs in the process of the Oracle server. It is the neighbour of the SQL engine.
For this reason, PL/SQL is much more perfectly integrated with the SQL than Java or c#.
But even in spite of this integration, you still need an interface in the client (PL/SQL) to interact with the SQL cursor, that you have created. In PL/SQL, there are a number of such interfaces customer.
There are explicit and implicit cursors. And there are sliders DBMS_SQL and reference. And there are implicit cursors created through immediate enforcement.
The difference between DBMS_SQL and immediate execution is therefore a difference in customer language interface. The SQL cursor on the server (in the shared Pool) doesn't know what customer interface is used. He cares not.
Client side, each of these client interfaces to a SQL cursor has a different function. And determines which one to use.
run immediately , it's fast and easy for dynamic SQL, but it cannot be used to determine the structure (called projection SQL) returned by a cursor to Select . So despite the dynamic SQL code, we must always know the encoding of time, just what this slider will return to consume its production.
DBMS_SQL to 'describe' the cursor SQL projection. So, we can create a dynamic Select SQL cursor. We can question its interface describe in order to determine the number, names and the data types of the columns returned. It can be removed these column values dynamically and consume the cursor.
There are therefore a major difference in the two feature sets. Tools for interfacing with the same SQL cursor.
So the main points to remember are:
-all SQL liquidation as SQL cursors on the server
-you have different interfaces in the language of the client to interact with a SQL cursor+
+ Your mission as a programmer, if you choose to accept it, is to determine which of these client interfaces is better suited to meet the requirements in your code.
This message will self-destruct in... 5.4.3 2...
-
Changing table via the package with immediate execution (problem)
Hello
I have a unusual problem. I have a package that contains the procedure that via execute immediate statement, creates a table on which he performs different actions for example:
-alter table some_owner.dummy_table noparallel
-create index some_owner.idx_name on some_owner.dummy_table (column)...
But I only managed to run move and create synonym table/public via execute immediate statement. Actions as alter table and create index fails with error ORA-01031: insufficient privileges.
Note If call these commands outside the package (a simple script) this all done OK.
I found a way, where I set the AUTHID CURRENT_USER command create package statement. In this way all the actions executed OK.
I wonder why I can't change the table via the package with immediate execution on a table that is in the tablespace "SOME_TABLESPACE" and the title of owner "SOME_OWNER", without putting the AUTHID command on the package.
There must be a problem why a package cannot change the table which is owned by the user 'SOME_OWNER '.
All useful responses would be appreciated.
I have a unusual problem.
No - you don't have. This question has been answered SEVERAL times on this forum and others.
But I only managed to run move and create synonym table/public via execute immediate statement. Actions as alter table and create index fails with error ORA-01031: insufficient privileges.
OK - your username doesn't have privileges to do these operations or only received privileges through roles.
Roles are DISABLED in named PL/SQL blocks that use of the AUTHOR's rights.
Note If call these commands outside the package (a simple script) this all done OK.
I found a way, where I set the AUTHID CURRENT_USER command create package statement. In this way all the actions executed OK.
Of course - the roles are NOT disabled in named PL/SQL blocks that use the rights of the APPELLANT or in anonymous blocks.
I wonder why I can't change the table via the package with immediate execution on a table that is in the tablespace "SOME_TABLESPACE" and the title of owner "SOME_OWNER", without putting the AUTHID command on the package.
Well now you know!
-
How how to escape double match in immediate execution in the trigger
Hi all
Please inform me what is the error in this procedure.
I think that the problem in the way to escape double match.
SQL > create or replace procedure P2
2 is
3. start
4 run immediately 'create or replace trigger t2.
5 |' before insertion '
6: ' on tb_test'
7: ' for each row '
8: ' declare '
9: ' start '
* 10 |' immediate execution "create table t1 as select distinct (NVL (soundex (namess),"'NONE "')) test ' *;
11: ' end; »
12 end;
13
14.
CAUTION: Procedure created with compilation errors.
SQL > show error
Errors of PROCEDURE P2:
LINE/COL ERROR
-------- -----------------------------------------------------------------
10/83 PLS-00103: encountered the symbol "NONE" when expecting one of
What follows:
* & = - + ; <>/ is mod remains not return rem
return < an exponent (*) > <>or! = or ~ = > = < = <>and or
as like2 like4 likec between using | Member in bulk
submultiset
SQL >Hello
Try the code below:
create or replace procedure P2
is
Start
run immediately "create or replace trigger t2 before insertion
on tb_test
for each line
declare
Start
run immediately "create table t1 as select distinct (NVL (soundex (account_number),"NONE")) of the test";
end;';
end;
/ -
How to declare variables in PL/SQL (dynamically)
Hello
Please inform me how can I declare variables in PL/SQL dynamically (I want used in other projects). I tried the following code, but this error
SQL > /.
declare
*
ERROR on line 1:
ORA-06550: line 1, column 74:
PL/SQL: ORA-00933: SQL not correctly completed command
ORA-06550: line 1, column 35:
PL/SQL: SQL statement ignored
ORA-06512: at line 10
-----------------------------------------
This is the code.
declare
v_temp varchar2 (300);
x varchar2 (20);
y varchar2 (20);
Start
BBB: = "X varchar2 (20);"
y : = '1' ;
run immediately 'declare '.
|| v_temp
|| "start."
|| 'Select name '.
|| "Ali where id =: yy '.
|| ' x using the ' | There
|| ' ; '
|| ' end; ';
end;
concerning
WAeluser3098640 wrote:
Hi, thanks a lot for all. OK I'll show you that I want to achieve. So please help me
refer to the following table it consist of four columns and three rows.
I only want to show the column that has a value of ZERO : like the following querySelect REC1 IMAGE_VALUE where corr = 'x' and rec1 = '1';
Do you want that ZERO or '1 '? Contradict you yourself.
Good to see you're requirements are clear.
in this case are easy, but in my case the columns may be more than 100 columns (this is will automatically create - already made the number of columns is not fixed but it start by REC1 REC2, REC3, REC4... etc)
the question is How can I DISPLAY the columns containing only '1' for specfic "corr" is X, Y or Z in PL - SQLWhy you have a table that automatically creates with an unknown number of columns?
It of very bad database design and get the basics of the design by the window completely.When you say 'Show' to what it means? SQL and PL/SQL "shows" nothing, because it has no user interface. It simply processes the data, and if these data are not in a known structure at the time where the application has been designed, and all code must be dynamic to try to deal with it. It is simply false in many ways.
A better structure would be something like this...
SQL> create table image_value (corr varchar2(20), rec varchar2(10), rec_val varchar2(20)); Table created. SQL> SQL> insert into image_value (corr, rec, rec_val) values ('X','REC1','1'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('X','REC2','0'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('X','REC3','0'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('Y','REC1','0'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('Y','REC2','1'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('Y','REC3','1'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('Z','REC1','1'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('Z','REC2','0'); 1 row created. SQL> insert into image_value (corr, rec, rec_val) values ('Z','REC3','1'); 1 row created. SQL> SQL> commit; Commit complete. SQL> SQL> with r as (select '&Required_Corr' as req_corr from dual) 2 -- 3 -- end of input 4 -- 5 select corr 6 ,max(decode(rn,1,rec)) as c1 7 ,max(decode(rn,2,rec)) as c2 8 ,max(decode(rn,3,rec)) as c3 9 ,max(decode(rn,4,rec)) as c4 10 ,max(decode(rn,5,rec)) as c5 11 ,max(decode(rn,6,rec)) as c6 12 ,max(decode(rn,7,rec)) as c7 13 ,max(decode(rn,8,rec)) as c8 14 ,max(decode(rn,9,rec)) as c9 15 ,max(decode(rn,10,rec)) as c10 16 from ( 17 select corr, rec, row_number() over (partition by corr order by rec) as rn 18 from image_value, r 19 where corr = req_corr 20 and rec_val = '1' 21 ) 22 group by corr; Enter value for required_corr: X old 1: with r as (select '&Required_Corr' as req_corr from dual) new 1: with r as (select 'X' as req_corr from dual) CORR C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 -------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- X REC1 SQL> / Enter value for required_corr: Y old 1: with r as (select '&Required_Corr' as req_corr from dual) new 1: with r as (select 'Y' as req_corr from dual) CORR C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 -------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- Y REC2 REC3 SQL> / Enter value for required_corr: Z old 1: with r as (select '&Required_Corr' as req_corr from dual) new 1: with r as (select 'Z' as req_corr from dual) CORR C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 -------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- Z REC1 REC3 SQL>
And you support the maximum number of values REC you expect.
Other than that, what you're really talking is a reporting requirement that requires that the data to be read once before, it is then processed and only the required data is queried with a second query. This can be done in PL/SQL using DBMS_SQL (or versions using EXECUTE IMMEDIATE), but really, that should be done by tools that are designed to query data then format and place based on the content of the reporting data.
Yet, you have not really explained why you are trying to do. It's alright saying you are trying to achieve, but which does not justify as being the right way to do things. What you have shown us so far, is not how to design a database or application code.
-
Appeal procedure set up by IMMEDIATE EXECUTION
Package: my_packageSQL> Create table bipul_test (a number(3), b number(2), c number(5), d varchar2(10), status varchar2(10)); Table Created. SQL> desc bipul_test Name Null? Type ----------------------------------------- -------- ---------------------------- A NUMBER(3) B NUMBER(2) C NUMBER(5) D VARCHAR2(10) STATUS VARCHAR2(10) SQL> insert into bipul_test values(1,1,1,'a', null); SQL> insert into bipul_test values(1,1,1,'b', null); SQL> insert into bipul_test values(2,1,2,'c', null); SQL> insert into bipul_test values(2,2,2,'d', null); SQL> insert into bipul_test values(null, null, null, 'x',null); SQL> commit; SQL> select * from bipul_test; A B C D STATUS ---------- ---------- ---------- ---------- ---------- 1 1 1 a 1 1 1 b 2 1 2 c 2 2 2 d x
Procedure: proc2, proc3
The Code of the appellant:CREATE OR REPLACE PACKAGE SRGUPTA.my_package AS -- PROCEDURE proc2; PROCEDURE proc2 (status OUT varchar2); PROCEDURE proc3 (Proc_name IN VARCHAR2, status IN OUT varchar2); END my_package; / CREATE OR REPLACE PACKAGE BODY SRGUPTA.my_package IS -- PROCEDURE proc2 PROCEDURE proc2(status OUT varchar2) AS n number(2); BEGIN update bipul_test set status ='SUCCESS' where A=1; commit; status:='FAILURE'; end proc2; PROCEDURE proc3 (proc_name varchar2, status IN OUT varchar2) AS x varchar2(100):=null; BEGIN --EXECUTE IMMEDIATE 'BEGIN '||proc_name||'; END;'; EXECUTE IMMEDIATE 'BEGIN '||proc_name||'(status); END;'; if status='FAILURE' then update bipul_test set status ='FAILURE' where A=2; commit; end if; end proc3; END my_package; /
Without THE normal execution mode param works very well. Above code is an effort through which I tried to capture the value of OUT to the appellant PROC3 param.set feedback on set serveroutput on DECLARE status VARCHAR2(100):=''; begin my_package.proc3('my_package.proc2', status); end; / EXIT;
PROC3 call PROC2 with immediate execution. Now if PROC2 have a parameter OUT and I want to capture in PROC3, is it possible?
Out of desire to my code:
Published by: handsome June 3, 2011 06:29SQL> select * from bipul_test; A B C D STATUS ---------- ---------- ---------- ---------- ---------- 1 1 1 a SUCCESS 1 1 1 b SUCCESS 2 1 2 c FAILURE 2 2 2 d FAILURE x
SQL> Create table bipul_test (a number(3), b number(2), c number(5), d varc (10)); Table created. SQL> insert into bipul_test values(1,1,1,'a', null); 1 row created. SQL> SQL> insert into bipul_test values(1,1,1,'b', null); 1 row created. SQL> insert into bipul_test values(2,1,2,'c', null); 1 row created. SQL> insert into bipul_test values(2,2,2,'d', null); 1 row created. SQL> insert into bipul_test values(null, null, null, 'x',null); 1 row created. SQL> commit; Commit complete. SQL> select * from bipul_test; A B C D STATUS ---------- ---------- ---------- ---------- ---------- 1 1 1 a 1 1 1 b 2 1 2 c 2 2 2 d x
SQL> CREATE OR REPLACE PACKAGE my_package AS 2 -- PROCEDURE proc2; 3 PROCEDURE proc2(status OUT varchar2); 4 PROCEDURE proc3(Proc_name IN VARCHAR2, status IN OUT varchar2); 5 END my_package; 6 / Package created. SQL> CREATE OR REPLACE PACKAGE BODY my_package 2 IS 3 -- PROCEDURE proc2 4 PROCEDURE proc2(status OUT varchar2) 5 AS 6 n number(2); 7 BEGIN 8 update bipul_test set status ='SUCCESS' where A=1; 9 commit; 10 status:='FAILURE'; 11 end proc2; 12 13 PROCEDURE proc3 (proc_name varchar2, status IN OUT varchar2) 14 AS 15 x varchar2(100):=null; 16 BEGIN 17 --EXECUTE IMMEDIATE 'BEGIN '||proc_name||'; END;'; 18 EXECUTE IMMEDIATE 'BEGIN '||proc_name||'(:1); END;' USING IN OUT status; 19 20 if status='FAILURE' then 21 update bipul_test set status ='FAILURE' where A=2; 22 commit; 23 end if; 24 end proc3; 25 END my_package; 26 / Package body created. SQL> set serverout on SQL> DECLARE 2 status VARCHAR2(100):=''; 3 begin 4 my_package.proc3('my_package.proc2', status); 5 dbms_output.put_line(status); 6 end; 7 / FAILURE PL/SQL procedure successfully completed. SQL>
-
Hello
DECLARE
V_CMP_PREFIX VARCHAR2 (15 BYTE): = 'cmp2. "
SQL_STMT VARCHAR2 (32760 BYTE);
CURSOR SIMPLE_VIEW_CUR IS
SELECT REPLACE (DBMS_METADATA. GET_DDL ('VIEW', object_name, owner), 'cmp1', V_CMP_PREFIX) SVIEW_SCRIPT
OF OBJECT
WHERE OWNER = "cmp1_schema."
AND OBJECT_TYPE = 'VIEW.
AND STATUS = "VALID".
AND OBJECT_NAME IN (SELECT NAME FROM ALL_DEPENDENCIES
OWNER of WHERE LIKE "cmp1_schema";
BEGIN
FOR SVIEW_REC IN SIMPLE_VIEW_CUR
LOOP
RUN IMMEDIATE SVIEW_REC. SVIEW_SCRIPT;
END LOOP;
END;
friends, I use this block to create a diagram to another diagram views. It contains simple but also complex views. views complexes are those who use
other views such as base tables.
the script of each view contains the clause "create or replace view force." When I'm compiling manually these views scripts all points of view are created once, because it contains the clause of STRENGTH.
but when I'm compiling the block above it gives the following error
Error report:
ORA-24344: success with compilation error
ORA-06512: at line 24
24344 00000 - 'success with compilation error'
* Cause: A compilation of sql/plsql error has occurred.
* Action: Return with the error code OCI_SUCCESS_WITH_INFO
can anyone suggest me why and what can be the solution to this.
Thank you
NeerajYou get the error as creating the view has caused an error in your immediate execution and is transmitted upward to the appellant. You should treat this exception inside the loop. Something like:
DECLARE V_CMP_PREFIX VARCHAR2(15 BYTE) := 'cmp2'; SQL_STMT VARCHAR2(32760 BYTE) ; view_error EXCEPTION; PRAGMA EXCEPTION_INIT (view_error, -24344); CURSOR SIMPLE_VIEW_CUR IS SELECT REPLACE(DBMS_METADATA.GET_DDL('VIEW',OBJECT_NAME,OWNER),'cmp1',V_CMP_PREFIX) SVIEW_SCRIPT FROM ALL_OBJECTS WHERE OWNER = 'cmp1_schema' AND OBJECT_TYPE = 'VIEW' AND STATUS = 'VALID' AND OBJECT_NAME IN (SELECT DISTINCT NAME FROM ALL_DEPENDENCIES WHERE OWNER LIKE 'cmp1_schema') ; BEGIN FOR SVIEW_REC IN SIMPLE_VIEW_CUR LOOP BEGIN EXECUTE IMMEDIATE SVIEW_REC.SVIEW_SCRIPT; EXCEPTION WHEN view_error THEN NULL; END; END LOOP; END;
You can save the error somewhere, or at least check for invalid in the new schema views once you are finished.
John
-
Update in immediate execution with rownum
Can I put in an UPDATE by immediate execution that only maximum 10 lines will be updated?
It is:
Thanks in advanceEXECUTE IMMEDIATE 'UPDATE A_PAZIENTI_ESAMI SET '|| ' NREFERTO = :1, '|| ' DATA_REFERTO = :2, '|| ' ANNOREFERTO = :3, '|| ' DATA_EROGAZ = :4, '|| ' STATO_REFERTO = :5, '|| ' MEDICO_E = :6 '|| ' WHERE '|| WHERE_CLAUSE_ESA -- || ' and rownum <= 10' -- this is my doubt -- USING AP_REFERTOB.NREFERTO, AP_REFERTOB.DATARF, EXTRACT (YEAR FROM AP_REFERTOB.DATARF) , AP_REFERTOB.DATAEROGA, STATOREFERTO, AP_REFERTOB.K_MED; COMMIT;
Hello
your query works very well. It updates only the first 10 rows based on the place where the condition.Thank you.
-
Error in PL/SQL - immediate execution
Hello
I get this error when executing the procedure. Could you help me what wrong in it?
SQL > create or replace procedure delete_emp_copy (tNom in varchar2) is
sql_stmt varchar2 (200);
Start
sql_stmt: =' remove from: 1';
execute immediate sql_stmt using tNom;
end;
/
Procedure created
. SQL > execute delete_emp_copy ('EMPLOYEES_COPY');
BEGIN delete_emp_copy ('EMPLOYEES_COPY'); END;
*
ERROR on line 1:
ORA-00903: invalid table name
ORA-06512: at "HR. DELETE_EMP_COPY', line 5
ORA-06512: at line 1
EMPLOYEES_COPY are in my schema? What could be wrong?Only the column values can be used for binding. You must concatenate the name of the table in the dynamic statement:
create or replace procedure delete_emp_copy (tname in varchar2) is sql_stmt varchar2(200); begin sql_stmt :='delete from ' || tname; execute immediate sql_stmt; end; /
SY.
-
How to get 2 out of procedure with sql dynamic param?
Hello
following my other question on the treatment in the ranks, so that I did a procedure defined ranges for the table I want to deal with;
I did another procedure to obtain a particular range for treatment and that's where I have a problem;
My data are:
{
CREATE THE TABLE PRECUBE. TEST_STG_TMO_RANGES
(
NUMBER OF GLASS FIBER,
MIN_RID VARCHAR2 (18 BYTE),
MAX_RID VARCHAR2 (18 BYTE)
)
tablespace UTI_DAT;
Insert into TEST_STG_TMO_RANGES (GRP, MIN_RID, MAX_RID) values (0, 'AABKSxAAEAACW3pAAA', 'AABKSxAAEAADqCICcQ');
Insert into TEST_STG_TMO_RANGES (GRP, MIN_RID, MAX_RID) values (1, "AABKSxAAEAADqCJAAA", "AABKSxAAEAAD + wICcQ");
Insert into TEST_STG_TMO_RANGES (GRP, MIN_RID, MAX_RID) values (2, ' AABKSxAAEAAD/QJAAA', 'AABKSxAAEAAECyICcQ');
Insert into TEST_STG_TMO_RANGES (GRP, MIN_RID, MAX_RID) values (3, 'AABKSxAAEAAEFQJAAA', 'AABKSxAAEAAEL8ICcQ');
Insert into TEST_STG_TMO_RANGES (GRP, MIN_RID, MAX_RID) values (4, 'AABKSxAAEAAEMSJAAA', 'AABKSxAAEAAEPwICcQ');
}
my package:
{
create or replace
PACKAGE C_UTI_BASIC AS
procedure P_GET_RANGE (table_owner_in in varchar2
TABLE_NAME_IN in varchar2
range_no number
Min_RANGE_OUT OUT varchar2
max_range_OUT OUT varchar2
);END C_UTI_BASIC;
}
My procedure:
{
procedure P_GET_RANGE (TABLE_OWNER_IN in varchar2
TABLE_NAME_IN in varchar2
RANGE_NO number
MIN_RANGE_OUT OUT varchar2
MAX_RANGE_OUT OUT varchar2
)
isDDL_STATEMENT varchar2 (4000);
Start
DDL_STATEMENT: ='select MIN_RID, MAX_RID in MIN_RANGE_OUT, MAX_RANGE_OUT in '. TABLE_OWNER_IN |'. ' || TABLE_NAME_IN | "_RANGES where PRV =' | RANGE_NO;immediately run DDL_STATEMENT;
DBMS_OUTPUT. PUT_LINE (DDL_STATEMENT);
DBMS_OUTPUT. Put_line ('Min_RANGE_OUT :'||) Min_RANGE_OUT);
DBMS_OUTPUT. Put_line ('max_range_OUT :'|| max_range_OUT);
end P_GET_RANGE;}
I tried to call the procedure with:
{
declare
V_MIN_RANGE_OUT varchar2 (30);
v_Max_RANGE_OUT varchar2 (30);
Start
C_UTI_BASIC. P_GET_RANGE (TABLE_OWNER_IN = > 'PRECUBE')
, TABLE_NAME_IN = > 'TEST_STG_TMO '.
, RANGE_NO = > '4'
, MIN_RANGE_OUT = > V_MIN_RANGE_OUT
, MAX_RANGE_OUT = > v_Max_RANGE_OUT
);
end;
}
but it ends with the error:
Error report:
ORA-06510: PL/SQL: not supported by the user-defined exception
ORA-06512: at "DWH_ADMIN. C_UTI_BASIC', line 2331
ORA-00905: lack of keyword
ORA-06512: at line 5
06510 00000 - "PL/SQL: not supported by the user-defined exception.
* Cause: A user-defined exception has been raised by PL/SQL code, but
not been processed.
* Action: Fix the problem that causes the exception or write an exception
Manager for this condition. Or you may have to contact your
Director of application or DBA.
When I jump the "run immediately" just to see what is output through DBMS output here it is:
{
Select MIN_RID, MAX_RID in MIN_RANGE_OUT, PRECUBE MAX_RANGE_OUT. TEST_STG_TMO_RANGES where PRV = 4
Min_RANGE_OUT:
max_range_OUT:
}
the select statmetent seems OK for me, but the parameters are not met and have the value null;
I would appreicate advice on where I went wrong here and how achieve a correct output
THS
Rgds
Rgds
Outside the use of SQL statements cr@p Dynamics (i.e. DML and not the DDL as implied by your code) and your total lack of variable bind...
And that is your problem. Your dynamic instruction seeks to select VARIABLES that are out of reach of the dynamic statement itself. The IN such statements should be part of the EXECUTE IMMEDIATE for example
EXECUTE IMMEDIATE ddl_statement INTO min_range_out, max_range_out;
-
Procedure failed with ORA-01403, associated with immediate execution, captured
This procedure compiles without error. Its purpose is to copy the sequence of one schema to another object. After it failed with ORA-01403, I added exception code of capture (see the comments in the code).
A few notes:
1. I started the proceedings with exec copy_sequence ('ADDRESS_SEQ', 'SST', 'TEST1');
2. the first sql string is built correctly since I can see in the console output:
Select last_number in the all_sequences where sequence_owner = upper('TSS') and upper('ADDRESS_SEQ') = sequence_name
I can run this SQL command, and returns a real value;
Here is the procedure
SET SERVEROUTPUT ON;
create or replace procedure copy_sequence (seq VARCHAR2, prod_schema VARCHAR2, VARCHAR2 test_schema)
as
Val number (21);
s_sql varchar2 (200);
Start
s_sql: = ' select last_number in the all_sequences where sequence_owner = upper('''|| prod_schema ||'') ') and sequence_name = upper('''||) Seq | " ')';
-immediate' select last_number in the Vale of all_sequences where sequence_owner = upper('''|| prod_schema ||'') ') and sequence_name = upper('''||) Seq | " ')';
Dbms_output.put_line ('sql 1 ' | s_sql);
execute immediate s_sql in val; ---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The code does not work here!
Dbms_output.put_line ('val' |) Val);
s_sql: = 'delete the sequence'. test_schema |'. ' || FF.;
Dbms_output.put_line ('sql 2 ' | s_sql);
-execution immediate s_sql;
s_sql: = 'create the sequence'. test_schema |'. ' || Seq |' minvalue maxvalue 1 999999999999999999999 begin by ' | Val | 'increment of 1';
Dbms_output.put_line ('sql 3 ' | s_sql);
-execution immediate s_sql;
-Add the following after procedure thown ORA-01403: no data found
exception when TOO_MANY_ROWS then DBMS_OUTPUT.put_line ('TOO_MANY_ROWS error');
When NO_DATA_FOUND then DBMS_OUTPUT.put_line ('error NO_DATA_FOUND');
While some other then raise_application_error (-20011, "Unknown Exception in this procedure");
end;
/
Hoek wrote:
The user (no SYSTEM, SYS, and SYSTEM are special, you should not use them unless you have TO) execution of the procedure must have the necessary privileges granted directly, bypassing a role...
??? There is nothing special about SYS or SYSTEM in what concerns them stored procedures. SYS by default is granted directly SELECT ANY SEQUENCE, so it does not work for the owner of MS by SYS. SYSTEM is therefore not:
Scott@pdborcl12 > create sequence s;
Order of creation.
Scott@pdborcl12 > create or replace
2 procedure system.p1
3 is
number of v_last_value 4;
5. start
6. Select last_number
7 in v_last_value
8 of all_sequences
9 where sequence_owner = 'SCOTT '.
10 and s = sequence_name ';
11 end;
12.Created procedure.
Scott@pdborcl12 > system.p1 exec.
BEGIN system.p1; END;*
ERROR on line 1:
ORA-01403: no data found
ORA-06512: at "SYSTEM. "P1", line 5
ORA-06512: at line 1Scott@pdborcl12 > grant select on s to the system;
Grant succeeded.
Scott@pdborcl12 > system.p1 exec.
PL/SQL procedure successfully completed.
SY.
Maybe you are looking for
-
Paint or Windows Photo Gallery I try to combine a picture of my sister with a picture of a poem beside it, how to combine the two into a single photo?
-
The other day hptoolbox fail to start at the top. Wasn't worried. Now, it starts, but I have no way to access. If I start start menu, another process starts, but some windows don't open. Impossible to find all the logs, so have no idea how to stu
-
Model Smartphones New BlackBerry?
Does anyone know if there is any plans for a new BB Torch model (or other) with a sliding keyboard and a larger screen 3.2 inches?
-
Form of Muse sends do not?
I have problems with the form of muse to a customer, I did a lot of them in the past without problem, but this one when done direct is formatted fone, can be filled in, but when you press on send, nothing happens, and the forms are filled inIt works
-
Servlet not showing do not image BLOB Oracle in JSF
Hi forum. I have a table in oracle in which i m storage photos in a BLOB on the field and extension (mime type) of the file in a field of varchar2. I can store image successfully. but I want the stored image to display in a graphicimage BLOB tag. I s