Open cursor for a nested table
Hi,
I want to open a cursor like:
open c1 for select * from emp;
BUT
I what the cursor results to be populated with contents of a nested table or associative array..
How can this be done???
Thanks in advance,
teo
Well, given a variable YOUR_EMP of nested table type EMP_NT it could be as simple as
open c1 for select * from TABLE( CAST(your_emp AS emp_nt));Cheers, APC
Similar Messages
-
Open cursor for internal table
Hi folks,
I have a tricky question concerning the cursor function and internal tables. First of all, it is not possible to create a view. Instead I've to use a function module for extracting some data to the BI system.
Actually most of the time I work with SELECT (for outer joins) and internal tables. At the end I have a internal table and must open an cursor. As fact, I can't open a cursor for an internal table - only database tables are allowed.
OPEN CURSOR WITH HOLD s_cursor FOR SELECT * FROM lt_temp.
Does someone have a clue how to solve my problem? Obviously I have to use a db table for a open cursor statement but on the same way I have to use a internal table for all my previous SELECT-statements.
Thanks in advance for your help.
Regards,
MrclWhy don't you use EXEC and ENDEXEC
Check this link
http://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb3b8b358411d1829f0000e829fbfe/content.htm
a® -
Open cursor for ...using...
Hi all
1 general question- can someone explain me how 'using keyword in 'open cursor for ...' statement works.
Does it replace where clause and how?
open cur_xx for qry_xxx using text_xxx
where 'text_xxx' is going to be my codition in select statement
Tahnks everyonehttp://oraclesvca2.oracle.com/docs/cd/B10501_01/appdev.920/a96624/13_elems35.htm
-
Open a cursor containing a nested table?
I have created a table with several nested tables by following the examples in: http://otn.oracle.com/docs/products/oracle8/doc_index.htm
I am now trying to query one of these nested tables using the techniques that are described.
The two techniques are (page 31 of above reference):
(1) "Flattened" query.
(2) Nested cursor
Both of these techniques work by simple SQL queries in SQl*Plus.
However, I get compilation errors when using identical queries when trying to open and return a cursor in a stored procedure.
Can you please help? Is this supported in 8.1.7?
Is there a better way to query and return data using OO4O to a windows client program?
If necessary, I would be glad to provide code samples.
Thank you,
Casey CummingsHere is a copy of my test code for the question that I posted. Any help on how I can get nested table data back to a windows client would be greatly apreciated.
Thank you.
Casey Cummings
--DDL.
--Original table.
CREATE TABLE MY_TABLE(
MY_KEY INTEGER,
MY_DATA VARCHAR2(2000));
--Basic Object types.
CREATE TYPE MY_NUMERIC_OBJTYP AS OBJECT(
SEQUENCE INTEGER,
DATUM NUMBER);
CREATE TYPE MY_TEXT_OBJTYP AS OBJECT(
SEQUENCE INTEGER,
DATUM VARCHAR2(255));
--Table type. Table of basic objects.
CREATE TYPE MY_NUMERIC_TABTYP AS TABLE OF MY_NUMERIC_OBJTYP;
CREATE TYPE MY_TEXT_TABTYP AS TABLE OF MY_TEXT_OBJTYP;
--Add nested tables to original table.
ALTER TABLE MY_TABLE ADD(
MY_NUMERIC_NTAB MY_NUMERIC_TABTYP)
NESTED TABLE MY_NUMERIC_NTAB STORE AS MY_NUMERIC_TABLE;
ALTER TABLE MY_TABLE ADD(
MY_TEXT_NTAB MY_TEXT_TABTYP)
NESTED TABLE MY_TEXT_NTAB STORE AS MY_TEXT_TABLE;
--Insert test data in the main, unnested table.
INSERT INTO MY_TABLE(
MY_KEY, MY_DATA
)VALUES(
1001, 'RECORD-1001');
COMMIT;
--Create the actual nested tables.
UPDATE MY_TABLE SET
MY_NUMERIC_NTAB = MY_NUMERIC_TABTYP();
UPDATE MY_TABLE SET
MY_TEXT_NTAB = MY_TEXT_TABTYP();
COMMIT;
--Insert test data into the nested tables.
INSERT INTO TABLE(
SELECT X.MY_NUMERIC_NTAB
FROM MY_TABLE X
WHERE MY_KEY = 1001
)VALUES(
1,901);
INSERT INTO TABLE(
SELECT X.MY_NUMERIC_NTAB
FROM MY_TABLE X
WHERE MY_KEY = 1001
)VALUES(
2,902);
INSERT INTO TABLE(
SELECT X.MY_NUMERIC_NTAB
FROM MY_TABLE X
WHERE MY_KEY = 1001
)VALUES(
3,903);
INSERT INTO TABLE(
SELECT X.MY_TEXT_NTAB
FROM MY_TABLE X
WHERE MY_KEY = 1001
)VALUES(
1,'ONE');
COMMIT;
BOTH OF THESE QUERYS WORK WHEN ENTERED IN SQL*PLUS.
--"FLATTENED" QUERY
SELECT X.MY_DATA, N.SEQUENCE, N.DATUM, T.SEQUENCE, T.DATUM
FROM
MY_TABLE X,
TABLE(X.MY_NUMERIC_NTAB) N,
TABLE(X.MY_TEXT_NTAB) T
WHERE X.MY_KEY = 1001;
--"CURSOR" QUERY
SELECT X.MY_DATA,
CURSOR(
SELECT *
FROM TABLE (MY_NUMERIC_NTAB) ),
CURSOR(
SELECT *
FROM TABLE (MY_TEXT_NTAB) )
FROM MY_TABLE X
WHERE X.MY_KEY = 1001;
CREATE OR REPLACE PACKAGE MANAGE_TEST AS
TYPE THE_CURSOR IS REF CURSOR;
PROCEDURE QUERY_TEST(
MY_CURSOR IN OUT THE_CURSOR
END;
CREATE OR REPLACE PACKAGE BODY MANAGE_TEST AS
PROCEDURE QUERY_TEST(
MY_CURSOR IN OUT THE_CURSOR
AS
BEGIN
OPEN MY_CURSOR FOR
--"FLATTENED" QUERY
SELECT X.MY_DATA, N.SEQUENCE, N.DATUM, T.SEQUENCE, T.DATUM
FROM
MY_TABLE X,
TABLE(X.MY_NUMERIC_NTAB) N,
TABLE(X.MY_TEXT_NTAB) T
WHERE X.MY_KEY = 1001;
END;
END;
*****************Errors:
LINE/COL ERROR
8/5 PL/SQL: SQL Statement ignored
11/13 PLS-00201: identifier 'X.MY_NUMERIC_NTAB' must be declared
CREATE OR REPLACE PACKAGE BODY MANAGE_TEST AS
PROCEDURE QUERY_TEST(
MY_CURSOR IN OUT THE_CURSOR
AS
BEGIN
OPEN MY_CURSOR FOR
--"CURSOR" QUERY
SELECT X.MY_DATA,
CURSOR(
SELECT *
FROM TABLE (MY_NUMERIC_NTAB) ),
CURSOR(
SELECT *
FROM TABLE (MY_TEXT_NTAB) )
FROM MY_TABLE X
WHERE X.MY_KEY = 1001;
END;
END;
*****************Errors:
LINE/COL ERROR
11/11 PLS-00103: Encountered the symbol "SELECT" when expecting one of
the following:
( ) - + mod not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
table avg count current exists max min prior sql stddev sum
variance execute multiset the both leading trailing forall
year month DAY_ HOUR_ MINUTE_ second TIMEZONE_HOUR_
TIMEZONE_MINUTE_ time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL stri
12/41 PLS-00103: Encountered the symbol "," when expecting one of the
following:
; return returning and or
null -
Open cursor for PLSQL table of records
Is it possible to open a cursor for all data in a PLSQL table of records?
something like
cursor c (p1 number) is select * from <plsqltab>
where <plsqltab>.col = p1There is no such thing as a PL/SQL table. Yes, I know that many calls this structure in PL/SQL a table. And that is exactly where all this confusion stems from.. and trying to treat such a "table" as an Oracle table using SQL.
The correct terms are dynamic array (indexed by integer) or dynamic associative array (indexed by varchar). And an array is nothing like a table ito RDBMS processing.
Yes, you can run SQLs against arrays. But it is "expensive". Why? Because the data sits inside PL/SQL Engine. Not in the SQL Engine. The data is in a PL/SQL defined structure. Not a SQL defined structure.
So.. the data needs to be shipped from the PL/SQL Engine to the SQL Engine and converted into a format that the SQL Engine can understand and use.
Also, once shipped and converted the SQL structure is not indexed. Which means that the only option is a full table scan of that structure.
So you need to ask yourself why do you want to use SQL against a PL/SQL array? As soon as you do that, you are saying "Hey, this PL/SQL table ain't good enough and I need to process it using SQL".
So why then does that data sit inside a PL/SQL array and not in a SQL table?
Oracle provides you with the ability to create temporary session tables. These can be indexed. SQL can be run against them without all the "expenses" that are associated with running SQL against a PL/SQL array.
PL/SQL arrays is a great tool. But only when it is the right tool to use. When someone says he/she needs SQL to use this tool, then I question the choice of the tool. Make sure you use the right tool for the job. -
There's a way to open a cursor from a plsql table?
Hello
Have a look here:
http://asktom.oracle.com/pls/ask/f?p=4950:8:13876292179522624220::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:666224436920, -
Open sys_refcursor for select from table variable?
Hi,
I've got a challenge for you! :-)
I've got a procedure that has a lot of logic to determine what data should be loaded into a table variable. Because of various application constraints, i can not create a global temporary table. Instead, i'd like to create a table variable and populate it with stuff as i go through the procedure.
The end result of the procedure is that i must be able to pass the results back as a sys_refcursor. This is a requirement that is beyond my control as well.
Is there a way to make this sort of procedure work?
Create Or Replace Procedure Xtst
Mu_Cur In Out Sys_Refcursor
Is
Type Xdmlrectype Is Record (Col1 Varchar2(66));
Type Xdmltype Is Table Of Xdmlrectype;
Rtn Xdmltype;
Begin
Select Internal_Id Bulk Collect Into Rtn From Zc_State;
open mu_cur for select col1 from table(rtn);
end;
11/42 PLS-00642: local collection types not allowed in SQL statements
11/36 PL/SQL: ORA-22905: cannot access rows from a non-nested table item
11/19 PL/SQL: SQL Statement ignored
Show Errors;Not anything i'd want to personally implement.
But for educational purposes only of course....
create table this_will_be_gross
column1 number,
column2 varchar2(30)
insert into this_will_be_gross values (1, 'begin the ugliness');
insert into this_will_be_gross values (2, 'end the ugliness');
variable x refcursor;
ME_XE?
declare
Rtn sys.ODCIVARCHAR2LIST;
BEGIN
SELECT
column1 || '-' || column2 Bulk Collect
INTO
Rtn
FROM
this_will_be_gross;
OPEN :x FOR
SELECT
regexp_substr (column_value, '[^-]+', 1, 1) as column1,
regexp_substr (column_value, '[^-]+', 1, 2) as column2
FROM TABLE(CAST(rtn AS sys.ODCIVARCHAR2LIST));
end;
17 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.09
ME_XE?
ME_XE?print :x
COLUMN1 COLUMN2
1 begin the ugliness
2 end the ugliness
2 rows selected.
Elapsed: 00:00:00.11In the above example i 'knew' that a hypen was a safe character to use to break up my data elements (as it would not be found anywhere in the data itself).
I would strongly encourage you not to implement something like this. I realize it's tempting when you are working in strict environments where it can take a serious battle to get structures like temporary tables or SQL Types created, but that's really the proper approach to be taking. -
Open cursor for and bind variables
Hello all,
how can I assign values to a bind variable before opening a cursor?
example code:
DECLARE
TYPE ref_cur_t IS REF CURSOR;
l_ref_cur ref_cur_t;
l_query VARCHAR2 (100)
:= 'select * from table t1 where ndx= :var_index' ;
BEGIN
-- assign a value to :var_index
OPEN l_ref_cur FOR l_query;
END;Thanks!Something like this ->
scott@ORCL>
scott@ORCL>DECLARE
2 l_ref_cur sys_refcursor;
3 l_query VARCHAR2 (100) := 'select * from emp where empno = :var_index';
4 l_empno number(4);
5 BEGIN
6 l_empno := &emno;
7 -- assign a value to :var_index
8 OPEN l_ref_cur FOR l_query using l_empno;
9 END;
10 /
Enter value for emno: 7698
old 6: l_empno := &emno;
new 6: l_empno := 7698;
PL/SQL procedure successfully completed.
Elapsed: 00:00:19.78
scott@ORCL>Regards.
Satyaki De. -
Open Cursor for insert actually inserts
Have been using code similar to this to prepare an SQL insert
statement before entering a loop to perform the inserts. Find
that the open actually performs an insert when it is executed,
so the table always has an extra row of garbage.
Is there some way to stop this behaviour? ProC compiler option
or open parameter. Currently I am doing a ROLLBACK after the
open to clear it.
Using the prepare because the table name is not known at compile
time.
EXEC SQL AT DB_1 PREPARE insert_cust FROM :szSqlInsertCustomer;
EXEC SQL AT DB_1 DECLARE insert_cust_cursor CURSOR FOR insert_cust;
EXEC SQL AT DB_1 OPEN insert_cust_cursor USING :customerId;
for ( ....)
customerID = ...;
EXEC SQL AT DB_DCMS EXECUTE insert_cust;
Also tried passing in the tablename as a host variable
EXEC SQL INSERT INTO :szTableName (CUSTOMER_ID) VALUES (:customerId);
ProC compiler gives the following error:
INSERT INTO :szTableName (CUSTOMER_ID) VALUES (:customerId);
........................1
PCC-S-02201, Encountered the symbol ":" when expecting one of the following:
( an identifier, a quoted string, date, table, count,
extract, interval, multiset, the, time, timestamp, treat,
trim, avg, max, min, sum, stddev, variance,
ThanksYou don't DECLARE and OPEN an insert statement (just selects). Just use EXECUTE ... USING ...
-
Open cursor for a dynamic query
Hi,
I'm using forms6i and db 10g
I want to create a procedure like below
PROCEDURE pop_cursor (generic_cur IN OUT gencurtyp, querystring varchar2) IS
BEGIN
OPEN generic_cur FOR querystring;
END;where generic_cur is a Ref Cursor and querystring will contain a query statement like 'select col1... from table1'
The above way i'm not able to do that, i'm getting error
Encountered the symbol 'QUERYSTRING' when expecting on of the following:
select
Is there any alternative??
Please helpYou can only use that sort of dynamic sql in the database. maybe you can return the ref cursor to the form from a db-function, i don't know if it works in forms 6i.
But you could tell us your requirement, maybe you can implement it without these dynamic things. -
Open cursor for string and select from partition (cannot bind)
Hi,
i don't manage to use a bind variable for a select ... from ... PARTITION(...).
It doesn't work to use something like
open cur for 'select ... from ... PARTITION(:1) where ...' using 'NDAY_20120301';So i have to create the cursor string with string functions each time i change the partition.
But that means, that the string changes each time.
Doesn't that prevent from re-use in library_cache?
best regards,
Frank>
So i have to create the cursor string with string functions each time i change the partition.
But that means, that the string changes each time.
>
Yes it does.
Doesn't that prevent from re-use in library_cache?
>
Yes it does.
So why do you care? Unless you have large numbers of partitions what difference does it make? Bind variables are useful to keep Oracle from doing hard parses of queries that are basically the same but use different filter values. Such as an INSERT statement that uses different values FOR EACH ROW rather
You are just constructing the main (non-filter) part of the query one time and need a single value for the entire query regardless of how many rows - that isn't really a use case for bind variables and isn't going to provide any real benefit.
So the real question is why do you even care about something that wouldn't provide any benefit to you even if you could do it?
Looks like you just want to 'roll your own' parallel processing rather that use, and pay for, Oracle's parallel functionality.
If each partition uses its own tablespace you could filter on the FILE Id of the ROWIDs since the file number will be different for each tablespace and datafile.
1. Determine the list of file numbers for each partitions tablespace.
2. Use a WHERE DBMS_ROWID.ROWID_RELATIVE_FNO (ROWID) = :n filter (or use IN (:n1, :n2))to filter rows based on file number(s) for the partition you want
See DBMS_ROWID.ROWID_RELATIVE_FNO in the PL/SQL Packages and Types doc
http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_rowid.htm#i1004259 -
Open cursor for existing procedure
Is it possible to open a refcursor for an existing procedure as apposed to opening a refcursor for a standard select statement. For example:
Standard select:
OPEN refcursor FOR
select * from my_table;
RETURN the_cursor;
Based on an existing procedure:
OPEN refcursor FOR
my_package.my_procedure();
RETURN the_cursor;
Note: my_procedure returns a table of record type rows defined by myself in the package spec.
Any help is much appreciated
Regards
TonyHi Sven
Thank you for that information however I am still a little unsure as to how to call the procedure including the necessary parameters. The procedure in question has the following IN parameters with one OUT parameter.
my_procedure (p_context_id in number,
p_username in varchar2,
p_mdata out mtab)
As you stated, if it is a procedure, which it is, then use the following:
my_package.my_procedure(the_cursor);
However if I need to pass parameters in the call that kind of conflicts where 'the_cursor' is?
Any ideas?
Tony -
OPEN cursor
When you OPEN a cursor for a mult-row query, is there a straightforward way that you can have it only retrieve a limited number of rows at a time and then automatically delete those rows as you do the FETCH against them? I'm thinking of setting up multiple sequential cursors, and opening and closing them as the rows are processed. But I'm hoping there might be a better way.
The problem is that I'm running out of TEMPORARY during the OPEN cursor stage.
The application I am working on needs to work in Standard Edition and Personal Edition versions of Oracle.
Thank you.Thanks - I had read the documentation before, but interpreted it differently.
What I had read was in:
http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#i45288
The extract of interest was:
Opening a Cursor
Opening the cursor executes the query and identifies the result set, which consists of all rows that meet the query search criteria. For cursors declared using the FOR UPDATE clause, the OPEN statement also locks those rows. An example of the OPEN statement follows:
DECLARE
CURSOR c1 IS SELECT employee_id, last_name, job_id, salary FROM employees
WHERE salary > 2000;
BEGIN
OPEN C1;
Rows in the result set are retrieved by the FETCH statement, not when the OPEN statement is executed.
My interpretation was that the result of the query was put into the temporary tablespace and then retrieved into the program during the FETCH.
Assuming I was wrong, what I'm wondering now is how I can possibly be running out of temporary space during this OPEN cursor process. -
Hi
I am using oracle 8.1.7 on solaris.
I have created
SQL> CREATE OR REPLACE PACKAGE TEST_PKG AS
2 TYPE row_cursor IS REF CURSOR RETURN TEMP_TAB%ROWTYPE;
3 PROCEDURE Return_Columns_proc (c_return IN OUT row_cursor);
4 END TEST_PKG;
5 /
Package created.
now i am trying to create the procedure Return_Columns_proc by
CREATE OR REPLACE PACKAGE BODY TEST_PKG AS
PROCEDURE Return_Columns_proc (c_return IN OUT row_cursor) AS
QUERY_STR VARCHAR2(850);
x number ;
BEGIN
x:=1;
QUERY_STR :='SELECT * FROM temp_tab where order_line_id = '||x;
OPEN c_return FOR QUERY_STR;
END Return_Columns_proc;
END TEST_PKG;
I am getting following error.
SQL> show error
Errors for PACKAGE BODY TEST_PKG:
LINE/COL ERROR
8/4 PL/SQL: Statement ignored
8/9 PLS-00455: cursor 'C_RETURN' cannot be used in dynamic SQL OPEN
statement
any help for this error.The error says it all. You have defined a strong ref cursor and it cannot be used with dynamic sql. However, that does not mean you cannot use the query directly in the OPEN clause
OPEN c_Return FOR SELECT * FROM temp_tab where order_line_id = X ; -
SELECT DISTINCT With OPEN cursor FOR
Hello.
I have the following procedure. All it does is open a cursor for an SQL string passed into it, and return the open cursor.
PROCEDURE sp_execute_dynamic (hold_input_string IN CLOB,
hold_cursor OUT hold_cursor_type) IS
BEGIN
OPEN hold_cursor FOR TO_CHAR(hold_input_string);
END sp_execute_dynamic;
It works fine except when I perform SELECT DISTINCT. I get the following error.
SQL> declare
2 TYPE hold_cursor_type IS REF CURSOR;
3 hold_cursor hold_cursor_type;
4 hold_object_name VARCHAR2(1024);
5 hold_object_type VARCHAR2(1024);
6 begin
7 dynamic_sql_pkg.sp_execute_dynamic('select distinct object_name from user_objects where object_
name in (''PLAN_TABLE'',''DBA_OBJECTS'')',hold_cursor);
8 loop
9 fetch hold_cursor into hold_object_name, hold_object_type;
10 exit when hold_cursor%NOTFOUND;
11 dbms_output.put_line('Object Name = '||hold_object_name||' Object Type = '||hold_object_type);
12 end loop;
13 end;
14 /
declare
ERROR at line 1:
ORA-01007: variable not in select list
ORA-06512: at line 9
It does the same thing with SELECT UNIQUE or SELECT with a GROUP BY. Can anyone tell me why this happens and what I could to to work around it?
Thanks
Chrissee at line 7 you are selecting only one column and at line 9you are fetching into two variables
7 dynamic_sql_pkg.sp_execute_dynamic('select distinct object_name from user_objects where object_
name in (''PLAN_TABLE'',''DBA_OBJECTS'')',hold_cursor);
8 loop
9 fetch hold_cursor into hold_object_name, hold_object_type;
HTH
Maybe you are looking for
-
Dear all, I've created out BI system in our new Portal installation and tested the connection. Everything works fine. I added the certificate of the portal to STRUSTSSO2 in BI ABAP and assigned SAP_ALL and SAP_NEW to my Account.in BI. If I open the i
-
How to set buildID.xml and custom.properties in SDK
Hello, I just completed a new build deployment of SAP ME5.2, because after I deployed the new version, I don't think I have set a correct version number.Can you someone give me a sample how to set the buildID.xml and custom.properties? I am a new on
-
Airplay movie stops and start over again
I have problem with AirPlay while watching play-channels from my iPad. After a while the program just starts over again. What can I do to fix this?
-
SCORM bookmarked playback resumes video but not audio
Hi, We've published e-learning courses with Captivate 3 as SCORM 1.2 in a PIF (zipped Flash 9 + HTML). They play fine when delivered from our SABA 5.4 LMS, with the audio and video in sync. If we exit SABA's content player part way through the cour
-
IMovie exported file location?
Where does iMovie store the file it creates when exporting to iDVD? I want to find it and delete it, but I don't know where it is. I'd appreciate any help. Thanks.