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
Tags: Database
Similar Questions
-
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...
-
help the collection and immediate execution
Hello
I have problems with this code:
The error isDECLARE l_stmt VARCHAR2 (11111); TYPE strarrt IS TABLE OF VARCHAR2 (500) INDEX BY BINARY_INTEGER; strarr strarrt; BEGIN l_stmt := 'SELECT ''UPDATE '' || cols.table_name || '' SET '' || cols.column_name || '' = '' '' WHERE '' || cols.column_name || '' IS NULL;'' FROM (SELECT column_name, uc.table_name FROM USER_CONSTRAINTS UC, USER_CONS_COLUMNS UCC WHERE UCC.TABLE_NAME = UC.TABLE_NAME AND UCC.CONSTRAINT_NAME = UC.CONSTRAINT_NAME AND UC.CONSTRAINT_TYPE = ''P'' AND UCC.TABLE_NAME LIKE ''B136T%'' AND ucc.table_name NOT IN (''B136T017'', ''B136T018'', ''B136T0029'', ''B136T034'', ''B136T450'')) keyz, (SELECT column_name, table_name FROM user_tab_cols WHERE TABLE_NAME LIKE ''B136T%'' AND data_type LIKE ''%CHAR%'') cols WHERE keyz.column_name = cols.column_name AND keyz.table_name = cols.table_name;'; EXECUTE IMMEDIATE l_stmt INTO strarr; FOR i IN strarr.FIRST .. strarr.LAST LOOP DBMS_OUTPUT.put_line (strarr (i)); END LOOP; END;
Error on line 2
ORA-06550: Line 37, column 36:
PLS-00597: expression 'STRARR' in the list IS of the wrong type
ORA-06550: Line 37, column 4:
PL/SQL: Statement ignored
Any ideas?
Published by: user10962462 on 27.03.2010 19:24Try
DECLARE l_stmt VARCHAR2 (11111); TYPE strarrt IS TABLE OF VARCHAR2 (500) INDEX BY BINARY_INTEGER; strarr strarrt; BEGIN l_stmt := 'SELECT ''UPDATE '' || cols.table_name || '' SET '' || cols.column_name || '' = '''' '''' WHERE '' || cols.column_name || '' IS NULL;'' FROM (SELECT column_name, uc.table_name FROM USER_CONSTRAINTS UC, USER_CONS_COLUMNS UCC WHERE UCC.TABLE_NAME = UC.TABLE_NAME AND UCC.CONSTRAINT_NAME = UC.CONSTRAINT_NAME AND UC.CONSTRAINT_TYPE = ''P'' AND UCC.TABLE_NAME LIKE ''B136T%'' AND ucc.table_name NOT IN (''B136T017'', ''B136T018'', ''B136T0029'', ''B136T034'', ''B136T450'')) keyz, (SELECT column_name, table_name FROM user_tab_cols WHERE TABLE_NAME LIKE ''B136T%'' AND data_type LIKE ''%CHAR%'') cols WHERE keyz.column_name = cols.column_name AND keyz.table_name = cols.table_name'; EXECUTE IMMEDIATE l_stmt BULK COLLECT INTO strarr; FOR i IN strarr.FIRST .. strarr.LAST LOOP DBMS_OUTPUT.put_line (strarr (i)); END LOOP; END;
-
SQL dynamic immediate execution
Hi all
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.
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
-
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>
-
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!
-
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.
-
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;
/ -
Can I create multiple dynamic entries during execution?
Can I create multiple dynamic entries during execution?
Oracle 11g
Request Express 4.0.2.00.06
Here's my problem:
We have a table that contains metadata about the files (paper or electronic).
We hope that we can need more columns in the table at some point and do not want to change the table or the application.
So to do this I would like to create:
A table called TBL_FILE with columns:
NUMBER of TBL_FILE_ID (this will be the primary key)
TBL_FILE_NAME VARCHAR2 (1000) (this will be the name of the file)
A second table will be called TBL_FILE_META with columns:
NUMBER of TBL_META_ID (this will be the primary key)
NUMBER of TBL_FILE_ID (this will be the key to forign to the files table)
TBL_META_COLUMN VARCHAR2 (30) (this is what would be the name of the column if it existed in TBL_FILE)
TBL_META_VALUE VARCHAR2 (1000) (this is the value of this record and the column "would be")
If a person can have as many meta data in the file to add columns to the table.
The problem is how can I allow users to add as much data as they wish with them having to re develop page.
Other things to note is that we would like this be on one page.
I know how to add we can create inserts several rows using a SQL (editable report).
However the TBL_META_VALUE in the TBL_FILE_META column will sometimes be a selection list and other times one text box or a numeric field.
So I don't see now a SQL (editable report) would work for this and I can't create a table of elements on the page running I can?
No idea how I might accomplish this? Is there a better way to do this?
It is a term or a name for what I'm doing by creating these "virtual" columns in another table?
I found this method looking at the workflow tables Oracles.Welcome to the Oracle Forums!
>
Can I create multiple dynamic entries during execution?Oracle 11g
Request Express 4.0.2.00.06Here's my problem:
We have a table that contains metadata about the files (paper or electronic).
We hope that we can need more columns in the table at some point and do not want to change the table or the application.So to do this I would like to create:
A table called TBL_FILE with columns:
NUMBER of TBL_FILE_ID (this will be the primary key)
TBL_FILE_NAME VARCHAR2 (1000) (this will be the name of the file)A second table will be called TBL_FILE_META with columns:
NUMBER of TBL_META_ID (this will be the primary key)
NUMBER of TBL_FILE_ID (this will be the key to forign to the files table)
TBL_META_COLUMN VARCHAR2 (30) (this is what would be the name of the column if it existed in TBL_FILE)
TBL_META_VALUE VARCHAR2 (1000) (this is the value of this record and the column "would be")If a person can have as many meta data in the file to add columns to the table.
The problem is how can I allow users to add as much data as they wish with them having to re develop page.
>
Creation of dynamic Page elements is not available. You must create surplus items and hide/show, etc. But you cannot change the element Type. Overall, too many restrictions in this approach.
>
Other things to note is that we would like this be on one page.
>
The limit of 100 points will hit you if you go with extra page element. With the form of tables that should not be a limitation, unless you're exceeding the limit of 50 APEX_APPLICATION point. G_Fnn points and limitation of the 60 column of the region report with "(analyze the query executing only) use generic name column" in the dynamic region.
>
I know how to add we can create inserts several rows using a SQL (editable report).
However the TBL_META_VALUE in the TBL_FILE_META column will sometimes be a selection list and other times one text box or a numeric field.
>
If the type if the item is variable, that doesn't mean that you have to type a way to store the item. Metadata of meta data.
>
So I don't see now a SQL (editable report) would work for this and I can't create a table of elements on the page running I can?
>
Yes, you can do it. Update report / query in a table can be built from the metadata using the SQL query that returns the PL/SQL function . It will be a bit of coding in PL/SQL where you use metadata and metadata of Meta data to restore your SELECTION with the right APEX_ITEMs. He might have a decrease in performance associated with it, but it will not be a serious degradation.
>
No idea how I might accomplish this? Is there a better way to do this?It is a term or a name for what I'm doing by creating these "virtual" columns in another table?
I found this method looking at the workflow tables Oracles.
>
I guess it's just a good TNF. This is the master model / retail Design, sounding more modern? ;)Kind regards
-
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
-
I'm testing a huge application with a database middleware sophisticated against the Oracle 12 database. So far, it works well with Pervasive SQL, MSSQL and Oracle 8 database... 11.
There are many questions about execution plans changed from Version 11 to 12. Oracle will tell what all of the improvements are (or at least have to improvements), and I can't really denied him. It's not the subject.
But I met some SQLs with horrible execution time, especially connected to Crystal Reports. Whenever I tried to check them, the queries have been executed quickly. As far as I can see, this has to do with the schema/user who executes the SQL statement in a first time. Here are the details:
• = ADMIN, DML = GUI DDL: all data are stored in a scheme of the ADMIN. The ADMIN user creates the tables, views and so on. It grants access to and creates synonyms for users, but it cannot modify the data using DML, given that triggers prevent him. End users, named 'GUI' users, are allowed to use the DML, but they have no privileges DDL.
• Database of reports (on paper) are generally quite complex. They have only a limited data set (must fit on sheets of paper!), and the Oracle optimizer optimizes often against this goal. Because Crystal Reports generates no advice, most of the reports are based on a view ADMIN. < xyz > (with the necessary information) and are accessible via a GUI. < xyz > synonym of user GUI.
• Crystal Reports running slowly (~ 5 minutes) connected as a GUI. I export it, get the SQL export, log in as an ADMINISTRATORand run the query, it works quickly (~ 2 seconds). Well, I've probably changed some white space, the line endings and others then copy / paste, I don't?
• I tried many things, connecting both GUI and ADMIN, simplifying the application, execution and so on. Then I got confused version of who this query: when the ADMIN has added an additional space character it was fast, removed again and it was slow again. Whitespace have an influence on the execution plan? Probably not, and if yes, my vision of the world would collapse.
• Later, I found out: in Version 12, depends on the user executing this query. Nail down us the source of the query for the synonym GUI. < xyz >. Let us make a new version of it (coded by white space - smile), and if run you it like GUI first, it's slow. Even if you re - log on as ADMINISTRATOR, the same version of this query is still slow. (This is a bug!)
Question 1: When I prepend the SQL with 'EXPLAIN PLAN FOR', I seem to be changing. As GUI has no plan_table and no privilege to explain a plan, I can't spy on the implementation plan of 'bad '. I don't want to give too much GUI, and I fear it could alter the execution plan. Is there a trick to get the plan of execution of a SQL statement in v$ sql or v$ sqlarea? (Means: execute the query: GUI and explain it as an ADMINISTRATOR)
Question 2: Is this on the privileges of the GUI? What you think, what direction will further investigate?
Oracle database generates an execution plan based on SQL, the dictionary, the statistics, the privileges of the user of the analysis and database and session settings. One of the privileges is GRANT MERGE [ALL] DISPLAY, which was responsible for the difference in this particular example and in this particular version.
When you log on as another user and run the same code in SQL, Oracle database verifies all required components before she reuses the old execution plan. This is a minor bug, but it is.
In the example, the privilege GRANT MERGE ANY NOTICE was given implicitly by the DBA privilege. Because MERGE ANY VIEW disables security controls, the privilege of s/n, default, also disables security controls. This is not a desired behavior from the DBA privilege.
-
Several SQL statements with zero executions in the region of SQL
Hello
one of my databases has a large number of statements in the SQL box with zero executions. Some of them analyzed several times without a single run. Why the database stores these statements and how to avoid or reduce them?
My problem is that the only time or zero time sql statements take the largest part of the area of sql:
-sql statement and only once and without executing sql statements
Select
Count (1) num_sql_total,.
sum (decode (executions, 1, 1, 0)) num_one_use_sql,.
sum (decode (executions, 0, 1, 0)) num_no_use_sql,.
Sum (RUNTIME_MEM) / 1024/1024 mb_used,.
sum (decode (executions, sharable_mem, 1, 0)) / 1024/1024 mb_for_one_use_sql,.
sum (decode (executions, 0, sharable_mem, 0)) / 1024/1024 mb_for_no_use_sql
Of
GV$ sqlarea
where
RUNTIME_MEM > 0;
NUM_SQL_TOTAL NUM_ONE_USE_SQL NUM_NO_USE_SQL MB_USED MB_FOR_ONE_USE_SQL MB_FOR_NO_USE_SQL 23318
8739
8027
1420,95619106293
381,41183757782
530,999855041504
Concerning
Thomas
This is not unusual. Another app could analyze for example hard the most often used SQLs in upstairs app - making benefit of further processing (in theory) of sweet analysis when you use these SQLs.
In fact, I remember reading something to this effect as a performance for some factor or another Oracle document or note?
So unless you have serious questions of shared pool, why bother with these sliders? What would be the problem?
Maybe you are looking for
-
When signing up, I get a security popup alert that says, "this page is encrypted, the information you entered are to be sent over an unencrypted connection and could easily be read by a third party. "Are you sure you want to continue sending this inf
-
Why Firefox has disabled the only version of Flash (10.1.53.64) my computer can use? (Mac PowerPC G5) Help!
-
I will be able to run the program on the integrated map y50-70
Hello world I want to buy a y50 - 70 for purposes of development, I want to be able to easily run a specific program on the intel integrated HD4600 card, or on the nvidia gtx 560 m-I will be able to do? Thank you!
-
A screen is constantly on Full Tilt Poker. It says I can allow or block. I keep clicking on allow, but nothing happens. I played Full Tilt for a long time...
-
I am currently migrating from a CVI 9.0 project that worked well with the latest version (was 8.5?) Now, I am facing problems with the safe thread queues. I'm a reminder TSQ Installationavecuneracine CmtInstallTSQCallback, where I spend my CVICALLBAC