Using cursor on table of records
I'm trying to use cursors on a passed in table of records and I
get a compilation error on the cursor definition that states
that the table must be defined (but I'm passing the table in).
Here are the types:
create type input_record_t as object (
field1 varchar2(10),
field2 varchar2(20));
create type input_table_t is table of input_record_t;
procedure process_data (
input_data in input_table_t) is
cursor data_cursor is select * from input_data; /* error */
begin
/* processin here */
end;
I've also tried:
cursor data_cursor (data_table in input_table_t) is select
* from data_table; /* also error */
How do I define a cursor on a passed in table of records? Any
help would be appreciated.
Same thing I am doing
Its working finr in applications(If i call from VB or ASP I am
getting result set)
I want to see the result set in SQL plus window,How can i call it
SQL > execute packperson.oneperson(1,?);
SQL > execute packperson.oneperson(1);
SQL > execute packperson.oneperson(1,NULL);
Nothing is working
Need help please
Thanks
are you trying to assign records to a plsql table type?
CREATE OR REPLACE PACKAGE packperson
AS
TYPE tfname is TABLE of VARCHAR2(15)
INDEX BY BINARY_INTEGER;
PROCEDURE oneperson
(onessn IN integer,
fname OUT tfname);
END packperson;
CREATE OR REPLACE PACKAGE BODY packperson
AS
PROCEDURE oneperson
( onessn IN integer,
fname OUT tfname)
IS
CURSOR person_cur
IS
SELECT ssn, fname, lname
FROM person
where ssn = onessn;
percount NUMBER DEFAULT 1;
BEGIN
FOR singleperson IN person_cur
LOOP
fname(percount) := singleperson.fname;
percount := percount + 1;
END LOOP;
END;
END;
Similar Messages
-
To Use Cursor or TYPE table Index by PLS_integer
Hi All,
Let's see if I have table with no. of records 19,26,20,000.
If I want to loop through all the records which will be a optimized way To Use Cursor or TYPE table Index by PLS_integer.
Please guide.
Thanks.What is it you want to do to/with the rows you're looping through?
Ideally you want to avoid looping, as that's row by row (aka slow by slow) processing and it's expensive time-wise.
If you're doing DML (insert/update/delete) then you're best off doing it in one sql statement, rather than looping. -
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. -
Can we create a cursor that can hold records of any table
DECLARE
CURSOR cr_tablenames IS
SELECT tname
FROM tab;
v_tablename tab.tname%TYPE;
BEGIN
OPEN cr_tablenames;
LOOP
FETCH cr_tablenames INTO v_tablename;
EXIT WHEN cr_tablenames%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Table: '||v_tablename);
-- HERE, I WANT TO PERFORM SELECT * OPERATION ON
-- EACH TABLE WHOSE NAME COMES INTO v_tablename
-- AND DISPLAY THE RECORDS
END LOOP;
END;
I want to perform SELECT * operation on each table whose name comes into the variable and display the records. How to achieve it? I am clueless.
Can this be achieved by something like... using cursors which can be defined dynamically and which can hold records of any table?
Or, do I need to have some different approach to this problem?
Edited by: kartins on Nov 5, 2009 11:41 PMsomething like
SQL> create or replace
2 function getContent (p_tn in varchar2)
3 return sys_refcursor
4 is
5 retval sys_refcursor;
6 begin
7 open retval for
8 'select * from '||p_tn;
9 return retval;
10 end;
11 /
Function created.
SQL>
SQL> var rc refcursor
SQL>
SQL> begin
2 :rc := getContent ('emp');
3 end;
4 /
PL/SQL procedure successfully completed.
SQL>
SQL> print rc
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 900 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1700 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1350 500 30
7566 JONES MANAGER 7839 02-APR-81 3075 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1350 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2950 30
7782 CLARK MANAGER 7934 09-JUN-81 2551 10
7788 SCOTT ANALYST 7566 19-APR-87 3100 20
7839 KING PRESIDENT 17-NOV-81 5100 10
7844 TURNER SALESMAN 7698 08-SEP-81 1600 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1200 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7900 JAMES CLERK 7698 03-DEC-81 1050 30
7902 FORD ANALYST 7566 03-DEC-81 3100 20
7934 MILLER CLERK 7782 23-JAN-82 1400 10
14 rows selected.
SQL>
SQL> begin
2 :rc := getContent ('dual');
3 end;
4 /
PL/SQL procedure successfully completed.
SQL>
SQL> print rc
D
X -
How to use temporarly table wid record type
Hi,
how to use temporarly table wid the record type wid this example
declare
type empcurtyp is ref cursor;
type rectype is record (veid t.emp_id%type, vename t.ename%type);
TYPE tabtype IS TABLE OF rectype;
empcv empcurtyp;
vtab tabtype;
begin
open empcv for select emp_id,ename from t;
loop
fetch empcv into vtab;
exit when empcv%notfound;
dbms_output.put_line(vtab.vename||vtab.veid);
end loop;
close empcv;
end;
here we hav table t and i m taking only two fields of the table t which r emp_id and ename.Try this
declare
type rectype is record (veid t.emp_id%type, vename t.ename%type);
type empcurtyp is ref cursor return rectype;
TYPE tabtype IS TABLE OF rectype;
empcv empcurtyp;
vtab empcv%rowtype;
begin
open empcv for select emp_id,ename from t;
loop
fetch empcv into vtab;
exit when empcv%notfound;
dbms_output.put_line(vtab.vename||vtab.veid);
end loop;
close empcv;
end; -
How to pick max value from a column of a table using cursor and iteration
Hello Everybody
I have a table loan_detail
and a column in it loan_amount
now i want to pick values from this table using cursor and then by using iteration i want to pick max value from it using that cursor
here is my table
LOAN_AMOUNT
100
200
300
400
500
5600
700i was able to do it using simple loop concepts but when i was trying to do this by using cursor i was not able to do it .
Regards
PeeyushSQL> SELECT MAX(sal) Highest_Sal,MIN(sal) Lowest_Sal FROM emp;
HIGHEST_SAL LOWEST_SAL
5000 800
SQL> set serverout on
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := counter;
10 counter := counter + 1;
11 END LOOP;
12 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
13 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
14 END;
15 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Even smaller
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := 1;
10 END LOOP;
11 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
12 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
13 END;
14 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Edited by: Saubhik on Jan 5, 2011 4:41 PM -
Loading data from one table to another using cursor
Hi,
I have given the below command to load the data from 1 table to another using cursor.
declare
cursor mycursor IS
SELECT extract_name,from_date,to_date,BETA from temp_table where EXTRACT_NAME='GIFTCARD_DETAILS';
Begin
for mycursor_1 IN mycursor loop
insert into tmp_tab columns(col1,col2,col3,col5) values(mycursor_1.EXTRACT_NAME,mycursor_1.from_date,mycursor_1.to_date,mycursor_1.BETA);
End loop;
commit;
end;
It is working fine.
But I want to hard code some of the columns ( like flags ) which are not there in 1st table and load them into 2nd table.
In db2 we will give commands like
varSqlStatus=`db2 "declare mycurs cursor for select extract_name,from_date,to_date,BETA,'N','Y' from temp_table"`
varSqlStatus=`db2 "load from mycurs of cursor modified by identityignore insert into tmp_tab(col1,col2,col3,col5,col6,col7) nonrecoverable"`
But I want it in oracle 10g, Can any one help me in this.Have you tried either of the two options :
1. Modify the CURSOR itself :
cursor mycursor IS
SELECT extract_name,from_date,to_date,BETA,'N','Y' from temp_table where EXTRACT_NAME='GIFTCARD_DETAILS';2. Modify the INSERT statement itself :
insert into tmp_tab columns(col1,col2,col3,col5,col6,col7) values(mycursor_1.EXTRACT_NAME,mycursor_1.from_date,mycursor_1.to_date,mycursor_1.BETA,'N','Y'); -
I HAVE A SOURCE TABLE WITH 10 RECORDS AND TARGET TABLE 15 RECORDS. MY QUESTION IS USING WITH THE TABLE COMPARISON TRANSFORM .I WANT TO DELETE UNMATCHED RECORDS FROM THE TARGET TABLE ?? HOW IT IS ??
Hi Kishore,
First identify deleted records by selecting "Detect deleted rows from comparison table" feature in Table Comparison
Then Use Map Operation with Input row type as "delete" and output row type as "delete" to delete records from target table. -
Facing low Performance when iterating records of database using cursor
Hi ,
i inserted nearly 80,000,000 records into a database, by reading a file whose size is nealry 800MB, in 10 minutes.
when i am iterating the records using Cursor with Default lock mode , it is taking nearly 1 hour.
My Bdb details are as follows
Environment : Non transactional , non locking
Database : Deferred write.
Cache : 80% of JVM ( -Xms=1000M -Xmx=1200m )
Could you please explain why it is taking such a long time ? did i make any mistakes on settings ?
Thanks
nvseenu
Edited by: nvseenu on Jan 15, 2009 5:47 AMHello Gary,
StoredMap is a convenience API wrapper for a Database. It has the same performance and multi-threading characteristics as a Database. You don't need to synchronize a StoredMap, or use Database to get better performance.
The lock conflicts are the thing to focus on here. This is unrelated to the topic discussed earlier in this thread.
How many threads are inserting and how many performing queries?
What other work, other than inserting and reading, are these threads performing?
Does any thread keep an iterator (which is a cursor) open?
How large are the data items in the map?
What is the resolution of the timestamp? Milliseconds?
I don't think the exception you posted is complete. Please post the full exception including the cause exception.
I can't tell from the exception but it looks like multiple insertion threads are conflicting with each other, not with the query threads. If you test only the insertions (no queries), do the lock conflicts still occur?
One possibility is that multiple insertions threads are using the same timestamp as the key. Only one thread will be able to access that key at a time, the others will wait. Even so, I don't understand why it's taking so long to perform the inserts. But you can easily make the key unique by appending a sequence number -- use a two part key {timestamp, sequence}.
Please upgrade to JE 3.3 in order to take advantage of the improvements and so we can better support you. We're not actively working with JE 3.2, which is very outdated now.
--mark -
Can I use a cursor to display multipla records (just as an EXECUTE_QUERY)?
is it possible?
so I can display about 10 records at the same time in a block (in forms)
just like a tabular form?
using a cursor, fetch, etc.what I want is this...
do you see when in a tabular forms (multiple records, i.e 5 records at the same time).
if you press F8 (execute query), it will fetch all records.
ok, now instead of using the execute_query key, how can I get the same thing by using cursors? -
How to update multiple columns from different tables using cursor.
Hi,
i have two tables test1 and test2. i want to udpate the column(DEPT_DSCR) of both the tables TEST1 and TEST2 using select for update and current of...using cursor.
I have a code written as follows :
DECLARE
v_mydept1 TEST1.DEPT_CD%TYPE;
v_mydept2 TEST2.DEPT_CD%TYPE;
CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO v_mydept1,v_mydept2;
EXIT WHEN C1%NOTFOUND;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
END LOOP;
COMMIT;
END;
The above code when run says that it runs successfully. But it does not updates the desired columns[DEPT_DSCR].
It only works when we want to update single or multiple columns of same table...i.e. by providing these columns after "FOR UPDATE OF"
I am not sure what is the exact problem when we want to update multiple columns of different tables.
Can anyone help me on this ?oops my mistake.....typo mistake...it should have been as follows --
UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
Now here is the upated PL/SQL code where we are trying to update columns of different tables --
DECLARE
v_mydept1 TEST1.DEPT_CD%TYPE;
v_mydept2 TEST2.DEPT_CD%TYPE;
CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO v_mydept1,v_mydept2;
EXIT WHEN C1%NOTFOUND;
UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
END LOOP;
COMMIT;
END;
Please let us know why it is not updating by using using CURRENT OF -
How to manipulate data in multiple rows without using cursor??
Hi all,
I have a form in which there is a push button & 4 text fields.
Push button : Process
Text fields: Year, Month, Financial_To_Year, Financial_From_Date.
In database , there are tables like, CUSTOMER_MASTER, FD_ACCOUNT_MASTER, FD_ACCOUNT_DTL, CUSTOMER_YEARLY_INTEREST, etc.
In table FD_ACCOUNT_MASTER, there are columns, like CUST_CODE, FD_ACCT_NO, FD_AMOUNT, ACCT_OPEN_DT, ACCT_CLOSE_DATE, ACCT_TYPE, INTEREST_RATE, etc.
There are thousands of records in the table.
For Push button : Process , TRIGGER: When button pressed,
I have to do all the process of FD for all the FD_ACCOUNTS at once. Process means i have to calculate Interest for all the accounts, calculate interest monthly, quarterly, yearly and make the FD_Accounts disable after the date of ACCT_CLOSE_DATE is reached, make the accounts renewed , etc all the process.
But to do this process for multiple rows at once, we use cursor, but i don't want to use cursor as i feel its too tedious to do.
Is there any other way , where i do process of multiple records at once without using cursor??
Help me, Thank You.
Oracle Forms Builder 6i.
Oracle9i Enterprise Edition Release 9.2.0.1.0 - ProductionAn Update statement certainly can update sets of data instead single row updates:
UPDATE Statement
if you can't do it in a single statement you can use bulk processing as well:
PLSQL 101
cheers -
Hi,
Inside of a stored procedure I created:
- a record type:TYPE gr_rec IS RECORD (contact_id number)
- a table of records: TYPE gr_tb IS table of gr_rec INDEX BY BINARY_INTEGER
and then I populated the table in a loop: tb(i).contact_id := a.contact_id.
My question is:
Is it posible to perform a select statement on the table of records to get only distinct records?
If not, how can I filter these records, as I use the procedure as a block data source in a form and I need only distinct records.
Note: I can not obtain select rows from the query that populates the table.
Thank you, MonicaLook at this function:
create or replace type TYP_REC_EMP as object
EMPNO NUMBER(4),
ENAME VARCHAR2(10),
JOB VARCHAR2(10),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2)
create or replace type TYP_TAB_REC_EMP is table of TYP_REC_EMP
CREATE OR REPLACE FUNCTION Ret_Cur RETURN TYP_TAB_REC_EMP
IS
TAB TYP_TAB_REC_EMP := TYP_TAB_REC_EMP(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
CURSOR C_EMP IS
SELECT *
FROM EMP ;
i PLS_INTEGER := 0 ;
BEGIN
FOR CEMP IN C_EMP LOOP
TAB.extend ;
i := i + 1 ;
TAB(i) := TYP_REC_EMP
CEMP.EMPNO,
CEMP.ENAME,
CEMP.JOB,
CEMP.MGR,
CEMP.HIREDATE,
CEMP.SAL,
CEMP.COMM,
CEMP.DEPTNO
END LOOP ;
RETURN TAB ;
END;
/And the Sql*plus query :
SQL*Plus: Release 9.0.1.3.0 - Production on Ve Mar 24 09:18:37 2006
(c) Copyright 2001 Oracle Corporation. All rights reserved.
Connecté à :
Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.5.0 - Production
SQL>
SQL> SELECT DISTINCT(empno),deptno FROM TABLE(ret_cur()) WHERE deptno=20
2 /
EMPNO DEPTNO
7369 20
7566 20
7788 20
7876 20
7902 20
SQL> Francois -
Block on PL/SQL Table of Record
Hi,
I am using Developer 6 with Oracle 8i, I based my block on PL/SQL procedure returning table of record, the form is running fine, but when ever I try to get data set, more then 200 rows, first it gows for query, working for a moment and then closed the form with out giving any result. And the least possible queryable data is 1000 to 30000.
It is working fine with less then 200 rows.
The form is also working fine with Ref Cursor with any of rows, but what I am doing is only possible with Table of record type.
Please give me your good advise what should I do.
Please accept my thanks in advance.
FahimThis should not happen. If you are not sure that your stored procedure is bug-free, you could generate and use stored procedures using my SQLPlusPlus and give it a try.
You can email me directly if your problem still not gets solved.
regards,
M. Armaghan Saqib
+---------------------------------------------------------------
| 1. SQL PlusPlus => Add power to SQL Plus command line
| 2. SQL Link for XL => Integrate Oracle with XL
| 3. Oracle CBT with sample GL Accounting System
| Download free: http://www.geocities.com/armaghan/
+---------------------------------------------------------------
null -
System getting hanged whilst using Insert into table select * from table
I have a peculiar problem.
I am using the below statements:
Query 1:
insert into table ppms.erin_out@ppms_dblink select * from erin_out;
Query 2:
insert into table ppms.erin_out@ppms_dblink values(23,'dffgg',12',dfdfdgg,dfdfdg);
I am in 'interfaces' schema (testing server) and executing above statements. We have testing server and development server, both are identical, i.e one is clone of the other.
ppms_dblink is created in interfaces schema. ppms_dblink points to different database server which has two schemas 'clarity' and 'ppms'. ppms_dblink is create through authentication details of clarity schema.
erin_out table is created on ppms schema on the same dababase server pointed by ppms_dblink.
Question is :
TOAD hangs while running query 1.
Query 2 is working perfectly.
As I have pl/sql script which is using query 1. I want to know why query 1 is creating problem.
If I use query 2 in my pl/sql query then it may create performance issue as i have to use cursor then.
On clarity schema, I have insert, update, select, modify rights on ppms.erin_out.
I have tried same queries from another database server.
That is I tried queries from 'interfaces' schema of development server ( clone of the testing server ). Its working perfectly.
Message was edited by:
user484158Dhanchik:
The table from which I select rows, to insert into table on dblink, is having only one record. It may contatin maximum 100 rows at a time because I am scheduling the procedure through daemon process. Anyway transaction is not more than 100 records. I am trying with just 1 record for testing.
So 1) Problem is not about the cost, TOAD is getting hanged ( to insert 1 record, cost does not mean much)
2) there is no large amount of data, so no question of deteriorated performance
Aron Tunzi:
I think that should not be problem, because I am able to insert a record through query 2.
Warren Tolentino :
I am testing with 1 record only. Its not performance issue.
Message was edited by:
रचित
Maybe you are looking for
-
How to restrict S_ALR_87003642 - Open and Close Posting Periods entry/updat
How to restrict S_ALR_87003642 - Open and Close Posting Periods entry/update for certain group of users eg. for Subsidiaries to openr/close periods of their own company only. Refer to my screen, Company 1010 and 1060 is for Head Office while 1210 is
-
XE web service stopped working
I installed XE 10g rel 2 on a Ubuntu Linux server a few weeks ago (so that's how much experience I have). I've been using the web service (server:8080/apex from one Vista and one XP client) for a few days. After a few hours of light work today, a cou
-
No data found for region error on Workflow notification page
Hi ! For workflow, I have created a Non Form Function in Oracle Applications and linked the region to display in the notification. This region's type is StackLayout. I created an attribute of type Document and assigned it the value in the form JSP:/O
-
Whitespace removed in Content Conversion
Hey! After content converting a flat file and running it through an xslt mapping, the java mapping need to find substrings within some fields. It turns out the fields are suppressed, meaning the whitespace before and after values are missing. Can thi
-
Itunes won't install on my computer
Itunes won't download on my comnputer. It says errors ocuured during installation. Your system has not been modified.