REF cursor in sql dynamic help to run immediately
HelloHow can we get the Ref cursor out a dynamic sql statement by executing immediate proceedings
for example, immediately run ' open CROR for select * from dynamicTable'
in this area, CROR is a dynamic cursor and table name is dynamic (known at run time), and we can't write static sql statement.
Thank you
I don't know what exactly you are after but here is a sample of what can be done.
SQL> VAR r REFCURSOR;
SQL> BEGIN
2 OPEN :r FOR 'SELECT * FROM DUAL';
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> PRINT r
D
-
X
You can use the OPEN... FOR education with a dynamic string.
Tags: Database
Similar Questions
-
Oracle 11G copy a table to help to run immediately
I want to copy the contents of a table in another aid to run immediately.
It keeps fails with the following error
ORA-00903: invalid table name
ORA-06512: at "TABLE_COPY", line 6
ORA-06512: at line 8 level
create or replace
procedure TABLE_COPY)
Table1 varchar2,
Varchar2 TABLE2)
is
Start
run immediately 'insert'. TABLE2. "(select * from ' |) TABLE1 |') ' ;
end;
Published by: user9213000 on January 24, 2013 07:38user9213000 wrote:
I want to copy the contents of a table in another aid to run immediately.It keeps fails with the following error
ORA-00903: invalid table name
ORA-06512: at "TABLE_COPY", line 6
ORA-06512: at line 8 levelcreate or replace
procedure TABLE_COPY)
Table1 varchar2,
Varchar2 TABLE2)
is
Start
run immediately 'insert'. TABLE2. "(select * from ' |) TABLE1 |') ' ;
end;Published by: user9213000 on January 24, 2013 07:38
Standard when boards (ab) use of EXECUTE IMMEDIATE is to compose the SQL statement in a single VARCHAR2 variable
Then print the variable before passing to EXECUTE IMMEDIATE.
COPY the statement & PASTE in sqlplus to validate its correctness. -
How to pass a sequence of 'help' to run immediately
How can I place a sequence.nxlval as a parameter using, here is my code looks like
sql_string is-> insert into some_tab (col1, col2,...) (select col1: passed_seq,...) where the... (I want to insert values in sequence for col2)
and I call it like
passed_seq: = "seq_". some_dynamic_num |'. nextval' (in my PB will be sequences with the string formed as seq_10.nextval)
EXECUTE IMMEDIATE sql_string using passes_seq;
If I do like this I get
Error: - 1722:ORA - 01722: invalid number seq_10.nextval
Published by: DIVI on 8 January 2013 07:40>
So is there another way to solve my problem, where queries are already formed and stored in a table in the form of data in the column, and I need to run those from different sequences in different scenarios.
>
Yes - you change applications to use a placeholder for the sequence you need (e.g. [SEQ_GOES_HERE]) when you need to run it you create a PL/SQL block into a VARCHAR2 variable, then use run immediately on the variable.If your stored query would look like this
sql_string is -> insert into some_tab (col1,col2, ....) (select col1,[SEQ_GOES_HERE],......) where ....
Load the good seq.nextval in a variable, and then replace "[SEQ_GOES_HERE]" in the query with this value.
-
ORA-02289 delete and create sequences to help to run immediately
Hi all
I copied a schema of one database to another.
In the former base my package works very well and the procedure inside the PKG_REFRESH package below works like a charm.
PROCEDURE FILL_TABLE(P_PROC_ID INTEGER) AS
BEGIN
EXECUTE IMMEDIATE (' drop sequence sq1' ");
EXECUTE IMMEDIATE (' create the sequence sq1 with cache of 1 500 ft);
v_kenmerk: = 'dim_1 ';
INSERT INTO dim_1)
Field1
Field2
... < snip >
END;
Now that I have imported the package in a different database, and while footage used by inside the packaging procedures exist, it gives the errors below:
Start
*
LAUGHING at regel 1:
. ORA-02289: sequence does not exist
ORA-06512: at "OWNER. PKG_REFRESH", line 117
ORA-06512: at "OWNER. PKG_REFRESH', line 80
ORA-01031: insufficient privileges
ORA-06512: at "OWNER. PKG_REFRESH', line 20
ORA-06512: at line 3 level
I checked and the sequence is abandoned, but has not been recreated. However, it only gives me an error message when you try to create the sequence.
I'm puzzled... search this forum, google and metalink did not help me either.
Does anyone have an idea why this is happening? Strange is that it does not work on the old database.
Robin
Published by: RobbieNerve on June 30, 2011 10:27 I forgot to say that the procedure is inside the packageTry to give an explicit subsidy to the user:
GRANT CREATE SEQUENCE TO OWNER;
Max
-
Need information about SQL dynamic
Hello
I have an obligation to pass the table name and the column names in the dynamic SQL statements to retrieve data from the table passed and columns. But here we cannot use the binding variable because we can link the names of tables and columns in dynamic SQL. Only, we can link the values in dynamic SQL and use the same query several times with different values for which we binded in dynamic SQL. I know that using dynamic SQL link variable will be more effective because the SQL engine will parse the query string, optimize, and then bind the value to obtain the correct data.
But my question is even if if we concatenate the table name and the column name in the dynamic SQL statements SQL if engine will reuse the same query string and optimize it and then bind the value of what we had him. Otherwise, every time it will generate for this query optimizer. Suppose that I'm passing always five name of the table with the same column names. Thus, each performance will be there still and always the same SQL string or it will create the optimizer again to each plate?
Please clarify my doubt.
Thank youuser212310 wrote:
But my question is even if if we concatenate the table name and the column name in the dynamic SQL statements SQL if engine will reuse the same query string and optimize it and then bind the value of what we had him. Otherwise, every time it will generate for this query optimizer. Suppose that I'm passing always five name of the table with the same column names. Thus, each performance will be there still and always the same SQL string or it will create the optimizer again to each plate?
Create how unique SQLs code? This is the fundamental question of ito SQL shareable.
Names of object (table, column, the function names and so on) can be variable in a SQL cursor. You cannot use bind variables for that. This following is - this not supported:
UPDATE :tablename SET col2 = :newvalue WHERE col1 = :pkey
So if your code produces the above code 5 different tables SQL, SQL statements unique 5 will be created. For example
UPDATE tab1 SET col2 = :newvalue WHERE col1 = :pkey .. UPDATE tab5 SET col2 = :newvalue WHERE col1 = :pkey
Creating dynamic SQL statements at best means a hard analysis first, followed by a soft analysis for each subsequent run.
It is still not enough good for performance - only the best is no soft analysis either. The code of the application creating the cursor once (via a soft or hard analysis) and then re-use the handle of the cursor again and again, each time with the bind variable, is approach the most optimal.
Noises of it - your reasons and dynamic SQL approach are not valid. Dynamic SQL is an exception. Always. And the vast majority of the time it is used, it is used correctly and for the wrong reasons.
The interface most appropriate between a client and a server is a formal application programming interface. In Oracle, this means the creation of the server API (for customers to call) using PL/SQL packages and ref Cursor.
Dynamic SQL is at the opposite extreme is end of what an API. How robust systems and applications would be if the core API does not exist and a "dynamic code" approach has been used? Performance and robustness would have been non-existent.
Now, you want to use this approach for Oracle? How does make any sense?
-
Hi guys,.
You can develop the ref cursor please. I'm new to the plsql...
Hi Alex,
Introduction to the REF CURSOR
With the help of
REF
CURSOR
s is one of the most powerful, flexible and scalable means to return the query results to an Oracle database to a client application.A
REF
CURSOR
is a PL/SQL data type whose value is the memory address of a workspace of query on the database. In essence, aREF
CURSOR
is a pointer or a handle to a game on the database of results.REF
CURSOR
s are represented by theOracleRefCursor
class ODP.NET.REF
CURSOR
s have the following characteristics:- A
REF
CURSOR
refers to a memory on the database address. Therefore, the client must be connected to the database during the lifetime of theREF
CURSOR
to access. - A
REF
CURSOR
involves a round-trip of additional database. While theREF
CURSOR
is returned to the customer, the actual data is not returned until the customer open itREF
CURSOR
and requests the data. Note that the data are not be retrieved until the user tries to read it. - A
REF
CURSOR
is not editable. The result set represented by theREF
CURSOR
is read-only. You cannot update the database using aREF
CURSOR
. - A
REF
CURSOR
is not back with scroll. The data represented by theREF
CURSOR
is only accessible in a way before and in series. You cannot position a record pointer inside theREF
CURSOR
to point to records in the result set. - A
REF
CURSOR
is a PL/SQL data type. You create and return aREF
CURSOR
within a block of PL/SQL code.
See this link which explained on cursor by Zerathul ref: PL/SQL 101: understanding Ref Cursor
Kind regards
Jitendra
- A
-
I get the following error when you try to select using a ref cursor
SQL error: ORA-06504: PL/SQL: return variables of the game results or the query types do not match
Here are the steps that I have followeed. Can someone tell me what I did wrong.
-CREATE AN OBJECT
create or replace type rsn_rec as object ( rsn_cd varchar2(10), rsn_desc varchar2(30) );
-CREATES A TABLE OF OBJECT
create or replace type rsn_tbl as table of rsn_rec;
PACKAGE/CREATED FUNCTION
create or replace package pkg_rept_test as type refcur is ref cursor; function get_rsn return rsn_tbl pipelined; end pkg_rept_test; create or replace package body pkg_rept_test as function get_rsn return rsn_tbl pipelined is o_cursor refcur; rec rsn%ROWTYPE; begin open o_cursor for select rsn_cd, rsn_desc from rsn; loop fetch o_cursor into rec; exit when (o_cursor%notfound); pipe row(rsn_rec(rec.rsn_cd, rec.rsn_desc) ); end loop; return; end; end pkg_rept_test;
-THE TABLE SELECTION
select * from table(pkg_rept_test.get_rsn)
-ERROR
SQL error: ORA-06504: PL/SQL: return variables of the game results or the query types do not matchYour code works for me just as you posted.
Connected to:
Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production
With partitioning, OLAP, Data Mining and Real Application Testing options
SQL > create table rsn (rsn_cd varchar2 (10), rsn_desc varchar2 (30));
Table created.
SQL > insert into rsn values ("cd test", "test description");
1 line of creation.
SQL > commit;
Validation complete.
SQL > create or replace the rsn_rec as an object type
2 (
3 rsn_cd varchar2 (10),
rsn_desc 4 varchar2 (30)
5 );
6.
Type of creation.
SQL > create or replace type rsn_tbl in the rsn_rec table;
2.
Type of creation.
SQL > create or replace package pkg_rept_test as
2 type refcur is ref cursor;
3 function get_rsn return rsn_tbl in pipeline;
4 end pkg_rept_test;
5.
Package created.
SQL > create or replace package body pkg_rept_test as
2 function get_rsn
3 return rsn_tbl pipeline is
4 o_cursor refcur.
5 rec rsn % ROWTYPE;
6 start
7. open o_cursor for
8. Select
rsn_cd 9,.
10 rsn_desc
11 of rsn;
12 loop
13 extract o_cursor in rec;
When exit 14 (o_cursor % notfound);
line 15 pipe (rsn_rec (rec.rsn_cd, rec.rsn_desc)
16 );
17 end of loop;
18 return;
end 19;
20 end pkg_rept_test;
21.
Package body created.
SQL > select * from table (pkg_rept_test.get_rsn);
RSN_CD RSN_DESC
---------- ------------------------------
tests of cd test description
-
differnce between Ref Cursor strong and weak Ref Cursor
differnce between Ref Cursor strong and weak Ref Cursor-------------
Sybrand Bakker
Senior Oracle DBA -
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 -
Apply the function of a column of a Ref Cursor
Hello
I have a stored procedure that returns a refcursor. Is it possible to apply a function as for example SUBSTR LPAD and a custom on a column in the refcursor function returned? The problem is that I can't change the existing stored procedure and I was wondering if it would be possible to write somewhat a wrapper procedure (or an anonymous PL/SQL block) that would actually apply a scalar function and only then return the result to the client?
Thanks and regards,
Sweardo you want after retrieve you records of Ref Cursor
SQL> create or replace 2 procedure rc_test (p_rc out sys_refcursor) 3 is 4 l_rc sys_refcursor; 5 begin 6 open l_rc for 7 select dummy from dual; 8 p_rc := l_rc; 9 end; 10 / Procedure created. SQL> declare 2 r sys_refcursor; 3 v varchar2(1); 4 begin 5 rc_test (r); 6 fetch r into v; 7 dbms_output.put_line (lower (v)); 8 close r; 9 end; 10 / x PL/SQL procedure successfully completed.
-
What is the purpose of USE in "run immediately".
Hi all
I just want to understand what is the purpose of USING in "EXECUTE IMMEDIATE".
Say for example, the procedure below, what is the difference? Both will end up in the same result.
DECLARE
FIXED_STAT VARCHAR2 (4000);
VAL_STAT VARCHAR2 (4): = "ijkl";
BEGIN
EXECUTE IMMEDIATE ' INSERT INTO abcd (efgh) VALUES (: text_string)' USING "ijkl";
FIXED_STAT: = "INSERT INTO abcd (efgh) VALUES ('|)" VAL_STAT |') ' ;
IMMEDIATELY RUN FIXED_STAT;
END;
/
Two major assets to HELP to run immediately, i.e. using parameters
(1) SQL statement should not hard analysed by the database every time [performance gain].
(2) more important, to avoid the "SQL injection" and ensure that the variable data will be properly includes as part of the statement
-
run immediately fails with the error
The following code generates the error and I can not understand what the problem is:
SET SERVEROUTPUT ON;
declare
Val number (21);
s_sql varchar2 (2000);
Start
s_sql: = q '{select last_number in the Vale of all_sequences where sequence_owner = 'SST' and sequence_name = "ADDRESS_SEQ"}';
Dbms_output.put_line ('sql 1 ' | s_sql);
run immediately s_sql;
end;
Error report:
ORA-00905: lack of keyword
ORA-06512: at line 7
00905 00000 - 'lack the key word'
* Cause:
* Action:
select last_number SQL 1 in val all_sequences where sequence_owner = 'SST' and sequence_name = "ADDRESS_SEQ."
The error is strange since
Select last_number in the all_sequences where sequence_owner = 'SST' and sequence_name = "ADDRESS_SEQ."
is a valid instruction
Although I see no need for SQL dynamic in this case, in general, you must provide the vatiable to receive the value of the select statement outside the immediate execution. More like:
s_sql: = q '{select last_number in the all_sequences where sequence_owner = 'SST' and sequence_name = "ADDRESS_SEQ"}';
execute immediate s_sql in val
John
-
2 running, 1 inside and 1 sql external, dynamic queries using loops and ref Cursor
Hi all
I'm under Oracle 10.2.0.2. I'm currently building dynamic sql using external and internal queries ref Cursor. Queries make use of user selected lists of ID, which is stored in a table in memory at run time. My example is as follows. From a list of book_publication_id on a web page, the user selects several books. The same user selects then several maps from a list of map_publication_id on the same web page. A book contains several maps and some of these cards will appear in more than one book (aka many many relationships).
This should then ask the following output to a new web page:
In the example above, the outer loop displays the details of the book for book_publication_id 230 and the inner loop displays all the users selected the maps in book_publication_id 230. Then, it moves on the details of the book for book_publication_id 240 and done the same thing again. Similar in some ways to how break would work in SQL * Plus, even if the table is built like an HTML, the book details must be on a separate line for the card details. However I don't know how I would want it to work.Outer loop: Display book details for book_publication_id 230 Inner loop: Display 1st map details for map_publication_id 340 Inner loop: Display 2nd map details for map_publication_id 346 Inner loop: Display 3rd map details for map_publication_id 350 Outer loop: Display book details for book_publication_id 240 Inner loop: Display 1st map details for map_publication_id 346 Inner loop: Display 2nd map details for map_publication_id 375 Outer loop: Display book details for id 255 and so on.
So far, using loops, I could not show all the details of card for each different book_publication_id, after the details of the book for only 1 book_publication_id are displayed each time. If a loop in a loop. I hope that makes sense. I think this is my internal request that it be built differently. My code for this part of the program, which is currently wrong, is as follows:
If anyone has any ideas or suggestions, I would be grateful. It is an extension of my previous code, I posted a question recently. As I am working and learning a step at a time, I left this idea in my previous question, that I had to make sure I knew that first.--Global variable section contains: var_user_chosen_map_list_ids VARCHAR2(32767); var_details VARCHAR2(32767); ...... PROCEDURE PROCMAPSEARCH (par_user_chosen_map_list_ids PKG_ARR_MAPS.ARR_MAP_LIST) IS BEGIN FOR rec_user_chosen_map_list_ids IN 1 .. par_user_chosen_map_list_ids.count LOOP var_user_chosen_map_list_ids := var_user_chosen_map_list_ids || '''' || par_user_chosen_map_list_ids(rec_user_chosen_map_list_ids) || ''',' ; END LOOP; var_user_chosen_map_list_ids := substr(var_user_chosen_map_list_ids, 1, length(var_user_chosen_map_list_ids)-1); var_details := FUNCMAPDATAFIND (var_user_chosen_map_list_ids); htp.print(var_details); END PROCMAPSEARCH; FUNCTION FUNCMAPDETAILS (par_user_chosen_map_list_ids IN VARCHAR2(32767) RETURN VARCHAR2 AS TYPE cur_type_map IS REF CURSOR; cur_book_search cur_type_map; var_book_date NUMBER(4); var_book_title VARCHAR2(32767); cur_map_search cur_type_map; var_map_date NUMBER(4); var_map_title VARCHAR2(32767); begin: OPEN cur_book_search FOR 'SELECT BI.book_date, BT.book_title FROM BOOK_INFO BI, BOOK_TITLE BT, TABLE (sys.dbms_debug_vc2coll(' || par_user_chosen_book_list_ids || ')) BL_1 WHERE BI.book_title_id = BT.book_title_id AND BI.book_publication_id = BL_1.column_value'; OPEN cur_map_search FOR 'SELECT MI.map_date, MT.map_title FROM map_info MI, map_title MT, TABLE (sys.dbms_debug_vc2coll(' || par_user_chosen_book_list_ids || ')) BL_2 TABLE (sys.dbms_debug_vc2coll(' || par_user_chosen_map_list_ids || ')) ML WHERE MI.map_title_id = MT.map_title_id AND BI.book_publication_id = BL_2.column_value AND BI.book_publication_id = MI.pub_publication_id AND MI.map_publication_id = ML.column_value'; LOOP LOOP FETCH cur_map_compare INTO var_book_date, var_book_title; var_details var_details := var_details || 'Book date: '|| var_book_date || 'Book title: ' || var_book_title; FETCH cur_map_compare INTO var_map_date, var_map_title; var_details := var_details || 'Map date: '|| var_map_date || 'Map title: ' || var_map_title EXIT WHEN cur_book_compare%NOTFOUND; END LOOP; EXIT WHEN cur_map_compare%NOTFOUND; END LOOP; RETURN var_details; END FUNCMAPDETAILS;
Kind regards
TimUsing dynamic SQL
declare dd sys_refcursor; ee sys_refcursor; d dept%rowtype ; e emp%rowtype ; begin open dd for 'select * from dept' ; loop fetch dd into d ; exit when dd%notfound ; dbms_output.put_line('Department:'||d.dname); open ee for 'select * from emp where deptno='||d.deptno ; loop fetch ee into e ; exit when ee%notfound ; dbms_output.put_line('..Employee:'||e.empno||':'||e.ename); end loop; end loop ; end ; / Department:ACCOUNTING ..Employee:7782:CLARK ..Employee:7839:KING ..Employee:7934:MILLER Department:RESEARCH ..Employee:7369:SMITH ..Employee:7566:JONES ..Employee:7788:SCOTT ..Employee:7876:ADAMS ..Employee:7902:FORD Department:SALES ..Employee:7499:ALLEN ..Employee:7521:WARD ..Employee:7654:MARTIN ..Employee:7698:BLAKE ..Employee:7844:TURNER ..Employee:7900:JAMES Department:OPERATIONS PL/SQL procedure successfully completed.
HTH
SS
-
Sql dynamic cursor or ref or what
I look on the internet for solutions to this problem and cannot say whether or not I need is dynamic sql, a Ref Cursor or what. I see pieces of what I'm trying to do on various web pages, but nothing which is close.
In a PL/SQL program, I read a file and perform operations based on these data. Here's my problem:
I have a select in my PL/SQL program based on something I read from the file and data, I put in a temporary variable earlier in the program.
Select fielda, fieldb
in tempvara, tempvarb
FROM table1
where fielda = fieldfromdatafile
and fieldb = tempvar1;
The problem is that I have more than one record that matches this criterion in the table. If I get a is not a single group group function error (ORA-00937).
When I think about doing a cursor, the cursor will have different or no data in this document, based on the results of the query.
If I try to build a select statement to run, then it will bomb because I have several recordings to come back.
I've never done ref Cursor and do not seem to understand how it would be useful for this. I looked at the ref Cursor definitions by searching on google, but maybe I should have done something else?
Any ideas? Thank you
VICYour condition is not exactly clear that you have not provided and the example data and the expected results (or given the version of your database).
Here is a link that will help you understand what the ref Cursor:
[PL/SQL 101: understanding Ref Cursor | http://forums.oracle.com/forums/thread.jspa?threadID=886365&tstart=0]
-
Hello
I have a package that returns a Ref Cursor, in this procedure, I have a dynamic sql code that is built according to certain values, and the query is a select query, is it possible that I can put that dynamic sql in the ref cursor and return of the procedure.
Or y at - it no alternative better workaround.
Thanks in advance.
NaveenYes you can.
Try this...
create or replace package test_pack is type ref_cur is ref cursor; procedure just_print(ref_var ref_cur); end; / create or replace package body test_pack is procedure just_print(ref_var ref_cur) is l_var emp%rowtype; begin loop fetch ref_var into l_var; exit when ref_var%notfound; dbms_output.put_line(l_var.ename); end loop; end; end; / declare cur_var test_pack.ref_cur; dsql varchar2(100); begin dsql := 'select * from emp where deptno=10'; open cur_var for dsql; test_pack.just_print(cur_var); end; / CLARK KING MILLER PL/SQL procedure successfully completed. Elapsed: 00:00:00.00
Kind regards
Prazy
Maybe you are looking for
-
Who is wrong
-
HP Pavilion all-in-one 23xt: CPU Upgrade
HP Pavilion q160xt CTO 23xt all in one I currently have an i3 - 4160 T Cpu, can I change CPU to i5-4590 t without license o/s issues? The i3 processor heat sink will be ok with the i5 processor. (both CPU state 35 Watt). What would be the best time
-
MY iPad came with Sprint cell data. What do I have to sign with Sprint? I'm trying to get rid of them. Thank you
-
What is your opinion on Windows Vista?
New technologies are important, but vista in the notebooks of Toshiba (all laptops) with 1024 or 2048 MB memory RAM is a mistake. All of my colleagues, I know that criticize vista and install xp instead of this system. XP works better - faster and mo
-
Urgent need for Windows 7 (64 bit) wlan driver for HP R249TU - product # L2Z88PA. I tried to install both of the pilots by (http://support.hp.com/us-en/drivers/selfservice/HP-15-r200-Notebook-PC-series/7486447/model/7748033#...).But both atheros and