Concept of unbound cursors.....
Can anybody explain to me the concept of unbound cursors in
PL/SQL?
null
Sayan,
What do you mean by unbound cursors?
I think you want a 'dynamic SQL cursor'?
Try this one:
Oracle8i Application Developer's Guide - Fundamentals
Release 8.1.5
A68003-01
Chapter 9 has some Dynamic SQL examples.
I hope it is what you want.
Teixeira.
Sayan (guest) wrote:
: Can anybody explain to me the concept of unbound cursors in
: PL/SQL?
null
Similar Messages
-
Can anybody explain in detail reference cursors?
I couldnt understand the concept of reference cursors in details
Regards
shwetaif we want use the cursor for more than one select statement then we will go for
ref cursor.
prior to oracle 9i it needs two statements to declare a refcursor
ex: type t1 is ref cursor
a t1;
here a is of type ref cursor and called as CURSOR VARIABLE.
in oracle 9i there is one statement is enough to declare ref cursor
ex a sys_refcursor
ex: declare
a sys_refcursor;
e emp%rowtype;
d dept%rowtype;
begin
open a for select * from emp; -- 1 select statement
loop
fetch a into e;
exit when a%notfound;
dbms_output.put_line(e.empno||' '||e.ename);
end loop;
close a;
open a for select * from dept; -- 2 select statement
loop
fetch a into d;
exit when a%notfound;
dbms_output.put_line(d.deptno||' '||d.dname);
end loop;
close a;
end;
regards
venu -
Does cursor hit database everytime ??
Hi seniors,
I am little confused with the concept of the cursor. I juts want to know is cursor has anything to deal with the database hit.
Explanation :
Say for example I have a cursor which is returning the 1000 rows from multiple table then that recordset will stored in some named sql area called cursor.
Now my question is when I loop the cursor will it get the actual data directly from the cursor OR It will just get an address location from the cursor to the actual database table or something.
If possible please help me to clear this doubt.
the reason being I have created one package which will move or drop the table with its all objects from all the schema available on the database server and it makes use of lot of cursor which are based on system views like all_table,all_trigger etc
Thanks in advance.Example is :
PROCEDURE move_table_pd
cTable in varchar2,
cFromSchema in varchar2 := 'STI_COUNTRY_USA',
cToSchema in varchar2 := 'STI_COMMON',
nVerbosity in number := 0,
nExecuteImmediate in number := 1
IS
BEGIN
if ((cTable is not null) AND (cFromSchema is not null) AND (cToSchema is not null)) then
if (nVerbosity <> 0) then
print_start_time_pd;
end if;
cTableName := upper(cTable);
cSourceSchema := upper(cFromSchema);
cDestinationSchema := upper(cToSchema);
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',false);
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',false);
-- Step 1 : Create the table at destination schema if needed.
create_table_pd(cTableName,cSourceSchema,cDestinationSchema);
-- Step 2 : Create Sequences and Triggers for the table at destination schema if needed.
create_trigger_and_sequence_pd(cTableName,cSourceSchema,cDestinationSchema);
-- Step 3 : Create Indexes for the table at destination schema if needed and then drop the rest all indexes if any.
create_index_pd(cTableName,cSourceSchema,cDestinationSchema);
-- step 4 : Insert the data at destination schema table
populateTable_pd(cTableName,cSourceSchema,cDestinationSchema);
-- The last step is to Drop the table and we need to really take care here.
-- step 5 : Drop the table from all other schema except destination schema.
drop_table_pd(cTableName,cDestinationSchema);
-- Again create the public synonyms on table
create_and_grant_synonym_pd(cTableName,cTableName);
-- Step 6 : Now execute all the statements from the statement array.
executeStatement_pd(nVerbosity,nExecuteImmediate,cDestinationSchema);
if (nVerbosity <> 0) then
print_end_time_pd;
end if;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END move_table_pd;
PROCEDURE create_table_pd
cTableName in ALL_TABLES.TABLE_NAME%TYPE,
cSourceSchema in ALL_TABLES.OWNER%TYPE,
cDestinationSchema in ALL_TABLES.OWNER%TYPE
IS
BEGIN
-- Step 1 : Create or drop the table depending on the the tables schema.
if ((cTableName is not null) AND (cSourceSchema is not null)) then
FOR REC_TABLE IN cur_get_create_table_detail(cTableName,cSourceSchema)
LOOP
BEGIN
if (REC_TABLE.OWNER = cSourceSchema) then
--Get the DDL of the table
cSqlStatement := getObjectDDL_fd('TABLE',cTableName,REC_TABLE.OWNER);
-- As This sql statement is with the source table schma name we need to replace that with the destination schema
-- And then we should create the table.
cSqlStatement := FindAndReplace_fd(cSqlStatement,cSourceSchema,cDestinationSchema);
-- Now first check whether the same table exist at destination schema or not if yes no need to create the same else create.
nObjectFound := isTableAlreadyExist_fd(cTableName,cDestinationSchema);
if (nObjectFound = 0) then
-- Now we are assured that the same table does not present at cDestinationSchema
-- So now we can push the statement to be executed in statements array.
pushStatement_pd(cSqlStatement);
cSqlStatement := null;
end if;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END;
END LOOP;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END create_table_pd;
PROCEDURE create_trigger_and_sequence_pd
cTableName in ALL_TABLES.TABLE_NAME%TYPE,
cSourceSchema in ALL_TRIGGERS.OWNER%TYPE,
cDestinationSchema in ALL_TRIGGERS.OWNER%TYPE
IS
-- Procedure local variables.
-- for triggers details
cTriggerSchema ALL_TRIGGERS.owner%TYPE;
cDescription ALL_TRIGGERS.description%TYPE;
cTriggerBody ALL_TRIGGERS.trigger_body%TYPE;
cTriggerName ALL_TRIGGERS.trigger_name%TYPE;
-- for sequence details
cSequenceOwner ALL_SEQUENCES.sequence_owner%TYPE ;
cSequenceName ALL_SEQUENCES.sequence_name%TYPE ;
-- Check Trigger count on table
cTriggerCount number :=0;
BEGIN
-- Step 2 : Create the sequences, triggers and there synonyms and grants on the the tables schema.
if ((cTableName is not null) AND (cSourceSchema is not null) and (cDestinationSchema is not null)) then
FOR REC_TRIGGER IN cur_get_create_trigger_detail(cTableName,cSourceSchema)
LOOP
BEGIN
cTriggerSchema := REC_TRIGGER.owner ;
cDescription := REC_TRIGGER.description ;
cTriggerBody := REC_TRIGGER.trigger_body;
cTriggerName := REC_TRIGGER.trigger_name;
if (cTriggerSchema = cSourceSchema) then
-- check the sequences for that trigger if any then create the same
FOR REC_SEQUENCE IN cur_get_create_sequence_detail(cTriggerName,cSourceSchema) LOOP
cSequenceOwner := REC_SEQUENCE.sequence_owner;
cSequenceName := REC_SEQUENCE.sequence_name;
BEGIN
if ((cSequenceName is not null) AND (cSequenceOwner = cSourceSchema)) then
--Get the DDL of the sequence
cSqlStatement := getObjectDDL_fd('SEQUENCE',cSequenceName,cSequenceOwner);
-- As This sql statement is with the source sequence schema name we need to replace that with the destination schema
-- And then we should create the sequence.
cSqlStatement := FindAndReplace_fd(cSqlStatement,cSourceSchema,cDestinationSchema);
-- Now first check whether the same sequence exist at destination schema or not if yes no need to create the same else create.
nObjectFound := isSequenceAlreadyExist_fd(cSequenceName,cDestinationSchema);
if (nObjectFound = 0) then
-- Now we are assured that the same sequence does not present at cDestinationSchema
-- So now we can push the statement to be executed in statements array.
pushStatement_pd(cSqlStatement);
cSqlStatement := null;
-- First drop synonym and then create
drop_synonym_pd(cSequenceName,cDestinationSchema);
-- Create the public synonym for sequence and give the grants to the sequence
-- As we know this sequence is the part of the trigger so we do not need
-- to create the synoyms and grants for the same
--create_and_grant_synonym_pd(cSequenceName,cSequenceName);
-- And now drop this existing sequences
drop_sequence_pd(cSequenceName,cSourceSchema,cDestinationSchema);
end if;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END;
END LOOP;
-- Now first check whether the same table exist at destination schema or not if yes no need to create the same else create.
nObjectFound := isTriggerAlreadyExist_fd(cTriggerName,cDestinationSchema);
if (nObjectFound = 0) then
-- Now we are assured that the same table does not present at cDestinationSchema
-- So now we can push the statement to be executed in statements array.
-- Rather we can create the trigger using some different way as show below
-- Create trigger using different way
cDescription := FindAndReplace_fd(UPPER(cDescription),cTableName,cDestinationSchema||'.'||cTableName);
cSqlStatement :='CREATE OR REPLACE TRIGGER '||cDescription||UPPER(cTriggerBody);
pushStatement_pd(cSqlStatement);
cSqlStatement := null;
end if;
-- Now drop the existing synonyms on triggers if any
-- As we do not create the synonyms for triggers then we dont have to drop the same
--drop_synonym_pd(cTriggerName,cDestinationSchema);
-- Now drop the existing triggers from other schema
-- We do not need to drop the triggers manually as it gets dropped along with the table.
--drop_trigger_pd(cTriggerName,cSourceSchema,cDestinationSchema);
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END;
END LOOP;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END create_trigger_and_sequence_pd;
PROCEDURE create_index_pd
cTableName in ALL_INDEXES.TABLE_NAME%TYPE,
cSourceSchema in ALL_INDEXES.OWNER%TYPE,
cDestinationSchema in ALL_INDEXES.OWNER%TYPE
IS
BEGIN
--cur_get_create_index_detail index_name
if((cTableName is not null) AND (cSourceSchema is not null) AND (cDestinationSchema is not null) ) then
FOR REC_CREATE_INDEX IN cur_get_create_index_detail(cTableName,cSourceSchema)
LOOP
BEGIN
if ((REC_CREATE_INDEX.index_name IS NOT NULL ) AND (REC_CREATE_INDEX.owner = cSourceSchema)) then
--Get the DDL of the Index
cSqlStatement := getObjectDDL_fd('INDEX',REC_CREATE_INDEX.index_name,REC_CREATE_INDEX.owner);
-- As This sql statement is with the source index schema name
-- we need to replace that with the destination schema
-- And then we should create the sequence.
cSqlStatement := FindAndReplace_fd(cSqlStatement,cSourceSchema,cDestinationSchema);
-- Now first check whether the same index exist at destination schema or not
-- if yes no need to create the same else create.
nObjectFound := isIndexAlreadyExist_fd(REC_CREATE_INDEX.index_name,cDestinationSchema);
if (nObjectFound = 0) then
-- Now we are assured that the same index does not present at cDestinationSchema
-- So now we can push the statement to be executed in statements array.
pushStatement_pd(cSqlStatement);
cSqlStatement := null;
-- Now as we have created a statement to create the index
-- So we need to check its existing Synonmyms and drop the same if exist
drop_synonym_pd(REC_CREATE_INDEX.index_name,cDestinationSchema);
-- Guess for Indexes we do not need to creat public synonym and no need to give grant to index
--create_and_grant_synonym_pd(REC_CREATE_INDEX.index_name,REC_CREATE_INDEX.index_name);
-- And now drop this existing indexes if any
-- We do not need to drop the indexes manually as it gets dropped along with the table.
-- drop_index_pd(REC_CREATE_INDEX.index_name,cSourceSchema,cDestinationSchema);
end if;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END;
END LOOP;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END create_index_pd;
PROCEDURE populateTable_pd
cTableName in ALL_TABLES.TABLE_NAME%TYPE,
cSourceSchema in ALL_TABLES.OWNER%TYPE,
cDestinationSchema in ALL_TABLES.OWNER%TYPE
IS
BEGIN
if((cTableName is not null) AND (cSourceSchema is not null) AND (cDestinationSchema is not null) ) then
nObjectFound := isTableAlreadyExist_fd(cTableName,cSourceSchema);
if (nObjectFound <> 0) then
cSqlStatement := 'INSERT INTO ' ||cDestinationSchema||'.'|| cTableName||
' SELECT * FROM '||cSourceSchema||'.'||cTableName;
pushStatement_pd(cSqlStatement);
cSqlStatement := null;
end if;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END populateTable_pd;
PROCEDURE executeStatement_pd
nVerbosity in number :=0,
nExecuteImmediate in number := 1,
cExecuteOn in varchar2:= 'STI_COMMON'
IS
nTotalRecords number :=0;
l_strsql LONG;
cStmt varchar2(200);
cError varchar2(300);
cCurrentSchema varchar2(50);
BEGIN
if (aAllStatement is not null) then
cCurrentSchema := getCurrentSchema_fd;
if (nExecuteImmediate <> 0) then
--altersession_pd;
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',false);
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',false);
end if;
nTotalRecords := aAllStatement.COUNT;
if (nVerbosity <> 0) then
DBMS_OUTPUT.PUT_LINE('TOTAL STATEMENTS TO BE EXECUTED :'|| nTotalRecords);
DBMS_OUTPUT.PUT_LINE('---------------------- EXECUTION BEGINS HERE -----------------');
end if;
--FOR cntr in 1..nTotalRecords
FOR cntr in aAllStatement.FIRST..aAllStatement.LAST
LOOP
BEGIN
if aAllStatement.EXISTS(cntr) then
cSqlStatement := aAllStatement(cntr);
l_strsql := dbms_lob.SUBSTR( cSqlStatement, 32765, 1 );
if (nVerbosity <> 0) then
DBMS_OUTPUT.PUT_LINE(cntr||' Now executing : '||cSqlStatement );
end if;
if (nExecuteImmediate <> 0) then
if (l_strsql is not null) then
BEGIN
--EXECUTE IMMEDIATE cSqlStatement;
EXECUTE IMMEDIATE l_strsql;
INSERT INTO gen_sql_log (t_sql_log_time,c_os_user,c_host,c_Server_Host,c_sql) VALUES (LOCALTIMESTAMP,sys_context('USERENV', 'OS_USER'),sys_context('USERENV', 'HOST'),sys_context('USERENV', 'SERVER_HOST'),l_strsql);
EXCEPTION
WHEN OTHERS THEN
cError:=substr(SQLERRM,1,300);
DBMS_OUTPUT.PUT_LINE('-------------<< ERROR >>-------------');
DBMS_OUTPUT.PUT_LINE('Error while running : '|| l_strsql);
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('Error Occured : '|| cError);
DBMS_OUTPUT.PUT_LINE('-------------<< END OF ERROR >>-------------');
END;
end if;
end if;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END;
END LOOP;
aAllStatement.DELETE(aAllStatement.FIRST,aAllStatement.LAST);
--aAllStatement.TRIM(nTotalRecords);
nStatementCounter :=0;
-- Move back to previous session
if (nExecuteImmediate <> 0) then
--altersession_pd(cCurrentSchema);
cCurrentSchema := getCurrentSchema_fd;
if (nVerbosity <> 0) then
DBMS_OUTPUT.PUT_LINE(' CURRENT SCHEMA : '|| cCurrentSchema);
end if;
end if;
commit;
end if;
EXCEPTION
WHEN OTHERS THEN
null;
END executeStatement_pd; -
Hi,
can any one give me good url for cursor bulk fetch concept. when i am using normal cursors it is taking 10 min to genarate report.
i need to improve the performance.
Thanks & Regards,
Sruthiuser12852882 wrote:
can any one give me good url for cursor bulk fetch concept. when i am using normal cursors it is taking 10 min to genarate report.
i need to improve the performance.How did you came to the conclusion that cursor fetching is the bottleneck and that it can be resolved using bulk fetching?
There are a lot of diverse factors that determine query and cursor performance. One of the more typical ones is simply bad PL/SQL design and coding, and poor use of the SQL language. And no amount of bulk fetching will ever fix that.
So before you jump to the conclusion that bulk fetching is the magic wand that will kill the performance monster your code needs to deal with, make sure you know WHAT the performance problem is.
Identify the problem. Then solve it. A so-called solution based on ignorance of the problem is not a solution at all - and merely increases the complexity of the problem by introducing more factors that play a role in the problem. -
Friends,
Please kindly help me to analyse this dump.
In BI end routine select * query has been written to fetch values from active dso . They are using non primary key in the where condition.
More than 2 crore 20 million records were avaialble in that DSo for that condtion. While executing this query its going to dump.
If i move ahead with Open cursor set Hold will this query work fine and using open cursor set hold can i fetch 2 crore 20 million records.
In dump its shwoing to check the parameters : ztta/roll_area ,ztta/roll_extension and abap/heap_area_total . Even i checked those parametrs in RZ11. The curent val is sufficient.
Please kindly advice me for this dump and will open cursor set hold avoid the dump for 2 crore 20 million records.
Thanks
Edited by: Suhas Saha on Sep 29, 2011 1:06 PMI am not completely convinced: the difference depends on the task which has to be done with the records of a package.
If the records are processed and the result must be written to the database into another table, then it will be necessary to COMMIT the changes, otherwise the 20.000.000 records will cause an overflow in the redo-logs of the database.
=> Only the OPEN CURSOR WITH HOLD will survive the DB-Commit.
The SELECT ... PACKAGE SIZE is simpler and can be used, if the result is further processed and reduced in size, which makes it possible to keep all data in memory (no intermediate DB-COMMITs are necessary) .
I would assume that case is the less frequent use case.
Siegfried -
Is any server side cursor concept in jdbc
hello friends,
i am new programmer in java, i am doing one project in which
i have to show the record in group (like user click one <1> link then
he can view first 10 record if <2> then he can view 11 -20 record list) on
same page.
tech - jsp/servlet
database - mysql
and if u know any tutorial jsp and mysql connectivity please tell me.
waiting 4 reply
thanx
nil4uYou will have to implement the caching of the results on the server side and return the results as the user requests them. You can use the CachedRowSet on the server side to help you do this. There may be some utilities and tools available to help with this, try googling.
-
T-SQL: Cursor is not advancing to next record
SQL Version: 2008 R2
Problem: I have the code below which uses a cursor. The cursor keeps returning the first record and does not advance to the next record in the cursor. It appears as if the Fetch Next is not being recognized. The Select
Statement in the cursor declaration returns two records which is the result set I expect to be contained in the cursor record set.
...bob sutor
SQL CODE:
DECLARE
@ProcessGroupID nchar(4)
, @RemoveAuditUser nchar(128)
DECLARE CertGroupCursor CURSOR FOR
SELECT DISTINCT CertGroups.GroupCode, CertGroups.RemoveAuditUser
--, UserControl.ProcessGroupID, UserControl.VPUserName
FROM udCertGroups AS CertGroups
LEFT JOIN udAuditUsers AS UserControl
ON CertGroups.GroupCode = UserControl.ProcessGroupID
WHERE CertGroups.GroupCode = UserControl.ProcessGroupID
AND CertGroups.RemoveAuditUser = UserControl.VPUserName
OPEN CertGroupCursor
FETCH NEXT FROM CertGroupCursor INTO @ProcessGroupID, @RemoveAuditUser
WHILE @@FETCH_STATUS = 0
Print @ProcessGroupID + '-' + @RemoveAuditUser
DELETE FROM udAuditUsers
WHERE ProcessGroupID = @ProcessGroupID
AND VPUserName = @RemoveAuditUser
FETCH NEXT FROM CertGroupCursor INTO @ProcessGroupID, @RemoveAuditUser
CLOSE CertGroupCursor
DEALLOCATE CertGroupCursor
Bob SutorThe real question is how to get rid of this mess. Think about the local “@remove_audit_user” as a variable; it's name is a verb, not a noun! and the NVARCHAR(n) lets you use Chinese Unicode. Why? In ISO-11179 rules , “remove_” is a called a role, and the
audit user would be the attribute with the attribute property “_user” in a valid data model. Where is the table that models “audit_users”? It has to be here by definition.
One of the first rules of data modeling is that a data element has one and only one name. This is a results of the Law of Identity in Logic (A is A: to be is to be something in particular, to be nothing in particular or many things in general is to be nothing
at all).
So how did “G.remove_audit_user = U.vp_user_name” occur?? ANSWER: lack of a design!
Your “G.group_code = U.process_group_id” is wrong. An identifier is not a code! TOTALLY DIFFERENT type of data elements! Do you have a postal code or a postal id? Blood_type or blood_id? Etc.? Have you ever read a book on basic data modeling?
The purpose of PRINT is debugging and not output. We had joke in the ANSI X3H2 Committee that SQL means “scarcely Qualified as a Language” because there is no I/O. PRINT will screw up performance in so many ways.
In a properly designed schema, we seldom use SELECT DISTINCT; we have keys and a valid schema that does not produce redundant duplicate rows. It might be valid, but after 30+ years of SQL, I would bet against it.
Your statement would use an EXISTS() predicate to handle multiple columns and conditions. But you did not bother with DDL, as required by basic Netiquette, so here is the skeleton I can give you.
DELETE FROM UD_Audit_Users
WHERE EXIST
(SELECT *
FROM UD_Cert_Groups AS G
WHERE G.process_group_id = ??
AND G.vp_user_name = ??;
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Select statement with if condition in cursors
Create procedure test (t1 in varchar2 ,t2 out varchar2)
IS
BEGIN
IF t1 = 'E'
select ENO INTO t2 from emp ;
elsif t1 = 'D'
select DNO INTO t2 from dept ;
end if;
end
how do i write this using cursorsi think i have to go for two cursors Intially i have written like the below codeThis requirement merely made me think of a pure SQL solution - something interesting...
As I mentioned, you likely should be using ref cursors as typically this requirement extends to a client that wants to make a call to Oracle without needing to know SQL or table names, pass parameters, and get a dynamic "+result set+" in response.
In Oracle. this is done using ref cursors. The code will look something like this"
{code}
create or replace procedure FooProc( someVar1 varchar2, someVar2 number, curResult OUT sys_refcursor ) is
begin
case
when someVar1 = someValueA then
open curResult for select * from emp;
when someVar1 = someValueB then
open curResult for select * from emp where empid = someVar2;
.. etc ..
end case;
end;
{code}
This is pretty straight forward stuff - and well explained with examples in the Oracle® Database PL/SQL User's Guide and Reference (http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#sthref139)
You procedure it a tad different in output parameters (and not what one typically would get in a production system), but the concept is the same. If the SQL projection is the same for each cursor, you can use a ref cursor as above - and then simply code a single fetch to get the row's columns, close the cursor, and then return that column values as output parameters.
Edited by: Billy Verreynne to fix the broken Jive s/w that does not understand how to parse a link and show it correctly.. (am getting more and more peeved at the crappiness factor of the new forum s/w) -
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 -
Retrieving cursor from a stored procedure
Hi,
Is there any means to retrieve a cursor from a stored procedure using java.sql.* package, without using database specific type code like OracleTypes.CURSOR?
Regards,
Shalin.Hi,
I had some across this problem some time ago. Although, there is no direct answer to this solution, there is a "kloog" that you can apply.
Please note that the signature for registerOutParameter(int parameterIndex, int sqlType), and note that where ever sqlType is mentioned it is an int.
Now JDBC is an interface and the implementation is given by Oracle. So to register an "out" parameter all you have to do is registerOutParameter(1, OracleTypes.CURSOR). It works!
Or otherwise try and find out what the int value of CURSOR is and replace. This is because not all databases can support returning a "cursor" type, since ORACLE and few other databases have a concept of "STORED PROCEDURE" and PLSQL is specific to ORACLE.
I hope this helps!
Cheers,
John. -
INVALID CURSOR - Anonymous Block calling Cursor in function
I am getting an error when trying to call my cursor.
CREATE OR REPLACE PACKAGE tax_update
AS
TYPE gencur IS ref cursor;
FUNCTION tax_sf
p_state IN bb_tax.state%type,
p_thecursor IN OUT gencur
RETURN NUMBER;
END;
CREATE OR REPLACE PACKAGE BODY tax_update
AS
FUNCTION tax_sf
p_state IN bb_tax.state%type,
p_thecursor IN OUT gencur
RETURN NUMBER
IS
lv_taxrate NUMBER;
BEGIN
OPEN p_thecursor FOR
SELECT taxrate
FROM bb_tax
WHERE state = p_state;
RETURN lv_taxrate;
END;
END;
DECLARE
tax_cur tax_update.gencur;
rec_tax bb_tax%rowtype;
BEGIN
LOOP
FETCH tax_cur INTO rec_tax;
EXIT WHEN tax_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(rec_tax.taxrate);
END LOOP;
END;
DECLARE
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 6Assignment is to create a package that will hold tax rates by state in a packaged cursor. The package will contain a function that can receive a 2 character state abbr. as an argument and find a match in the cursor and return the tax rate for tha tstate. An anonymous block will test the function with state of NC.
Can anyone assist?You would need to call the function to open the cursor before you try to fetch from the cursor
DECLARE
tax_cur tax_update.gencur;
rec_tax bb_tax%rowtype;
l_some_number number;
BEGIN
l_some_number := tax_update.tax_sf( <<some state parameter>>, tax_cur );
LOOP
FETCH tax_cur INTO rec_tax;
EXIT WHEN tax_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(rec_tax.taxrate);
END LOOP;
END;A couple of points, though.
1) Your function returns a NUMBER but that NUMBER will always be NULL. It seems rather unlikely that this is really what you want. It would seem to make more sense for the function to return the cursor rather than returning a superfluous number.
2) Your function requires a `bb_tax.state%type` parameter. But your anonymous block doesn't seem to have any concept of a state so I'm not sure what you want to pass in there.
3) Looking at the code, it seems a bit odd that your cursor returns a single column of data. If a state can have multiple rates, wouldn't you need to select some additional criteria in order to figure out which sort of tax each row represents or to otherwise differentiate different rows? If a state can only have a single tax rate, it makes no sense to open a cursor that is only going to ever return a single row.
4) There is no need to declare your own weak ref cursor type (tax_update.gencur). You can just use the Oracle built-in type SYS_REFCURSOR.
Justin -
Hi,
I am new to Ref Cursor concepts and I am trying a small block but its throwing error. Pls help me.
PACKAGE SPEC:
CREATE OR REPLACE PACKAGE PKG_JOBINFO AS
PROCEDURE JOBINFO ( v_job_id IN number, p_cursor OUT PKG_JOBINFO.RESULT_REF_CURSOR);
TYPE RESULT_REF_CURSOR IS REF CURSOR;
END PKG_JOBINFO;
PACKAGE BODY:
CREATE OR REPLACE package body PKG_JOBINFO
AS
PROCEDURE JOBINFO ( v_job_id IN number,
p_cursor OUT PKG_JOBINFO.RESULT_REF_CURSOR)
AS
BEGIN
OPEN p_cursor FOR
SELECT JOB_ID,
JOB_NAME,
TABLE_NAME
FROM JOB_INFO
WHERE JOB_ID=V_JOB_ID;
EXCEPTION
WHEN OTHERS THEN
raise;
END;
END;
While compiling the package i am not getting any errors. I am getting errors only while executing
SQL> exec PKG_JOBINFO.JOBINFO ('23');
BEGIN PKG_JOBINFO.JOBINFO ('23'); END;
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'JOBINFO'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Please help me to resolve this error.
Thanks.user497267 wrote:
Thanks its working. So if we are using ref cursor we have to execute like this only.To add...
The actual issue you were experiencing was not because you were using ref cursors specifically, but because you had an OUT parameter in your procedure.
When you have an OUT parameter, you need to ensure that you pass in a variable to that parameter of the procedure in order that the procedure can populate it. In your case you were only passing in the first parameter, but you weren't passing in a variable to capture the OUTput.
What Alex showed was that by declaring a variable of the same datatype (ref cursor in your case) and passing that in as the second parameter, that variable was populated with the ref cursor information from inside the procedure. Once that variable was populated, after the procedure call, the data from that ref cursor can be obtained (using SQL*Plus' print command in Alex's example). -
How to divide resultset of a query in different ref cursors in a package
Hi Oracle Gurus,
I need to create a package which counts the no of rows returned by a select query on multiple tables and according to the returned rowcount inputs the resultset in a ref cursor. Procedure will be called by a .NET program and output param refcursor will be assigned to a data reader which will redirect all the data to an Excel file.
All the above is done. Issue is due to Excel's limit of 64000 rows, if data returned by query is greater than 64K it wont be fit in 1 Excel sheet. So, in order to overcome this limit I need to do some looping in Oracle package which keeps on storing the query results (rows<64K) in different ref cursors so that these refcursors as OUT params can be redirected to separate Excel sheets in C# program.
NOTE : Earlier on I created 2 procedures in the package to fetch rows<64K and another one to fetch rows between 64K and rowcount of the query. My program was calling 2 different procedures to redirect data into 2 diff Excel sheets.
But this fails when query resultset is even greater than 128000 or more and demands 3-4 or even more Excel sheets to be created.
Please help.
Any idea how to do looping in Oracle to accomplish this?> So, in order to overcome this limit I need to do some looping in Oracle package which keeps on
storing the query results (rows<64K) in different ref cursors so that these refcursors as OUT params
can be redirected to separate Excel sheets in C# program.
Huh?
That is saying that "I need to change this road and build it straight through that lake as the road has a curve here".
It surely is a LOT easier to leave the road as is and simply turn the darn steering wheel in the car?
Have the .Net data reader keep a row count of rows read from the ref cursor - and when it reached a pre-set max, have the reader do a "control break"[1] and change to a new worksheet as the destination for writing the rows to.
[1] Google the term if you do not understand this basic concept that was among the very basic program control structures taught back in the 80's.. while I foam at the mouth how today's "wonder kids" turned programmers, that grew up with computers, do not even comprehend the most basic programming logic designs... -
Is there a way to restrict the cursor at the end of a line in the source code editor??
In the source code editor, the cursor will always follow where I click. But I wanna restrict it at the end of a line, just like other text editors do. Is there a option or sth? I can't put up with it any longer.
Solved!
Go to Solution.Hello morphe!
The source editor in the LabWindows/CVI environment is constructed under the concept of virtual space.
At the moment, in the current version of LabWindows/CVI, this is the default behavior, which cannot be changed from the editor preferences dialogs.
Best regards,
- Johannes -
How to edit text in design mode - i.e get an I cursor instead of arrow
DW CS6 This is a very basic unclear on concept question. I normally work in code view and have no problems. Sometimes there is a large block of text within a paragraph which is in a div.
I would like to correct a simple typo or insert some more text directly from the design mode display. However, almost all the time, when I click on the spot I want to modify, the cursor remains an arrow and the entire div gets a yellow border. I still have the select arrow, not the vertical bar I need to edit.
Sometimes I seem to stumble onto the right set of clicks and get the edit bar.
Thanks for any suggestions.Design View can get pretty cranky when your code has errors.
Check the validator at http://validator.w3.org and make sure you're working with clean code before resorting to wiping preferences, config folders or reinstalling.
If your code is clean and it's still not allowing you to click your text in Design View, you may have a z-indexing issue. I notice on pages that use a lot of positioning (APDivs) you can accidentally cover up your text boxes with blank/transparent areas of other elements and cause problems when trying to access that text at a later date.
It's all guesswork though without seeing your actual page.
Could you post a link?
Maybe you are looking for
-
Is MJPEG/RTP performance improved in Java 6 (Mustang)?
Hi, I use JMF to stream video from a web cam to a client using the Video JPEG/RTP protocol. I have some performance problems in term of fluidity and image quality. Is the (M)JPEG/RTP performance is improved in Java 6 (Mustang)? I read that Mustang al
-
This is truly devastating. Please help! Where did all my stuff go? Is it reparable?
-
So i have my items that I bring onto stage with my loop function with dynamic text fields within the loop. If I want to clear these items using a reset button on the stage how can I do this. From my code below when I press the reset button I receive:
-
Explain SQL Query execution plan: Oracle
Dear Masters, Kindly help me to understand execution plan for an SQL statement. I have following SQL execution plan for a query in system. How should I interpret it. I thank You in advace for your guidance. SELECT STATEMENT ( Estimated Costs = 1.372.
-
Database restore from a particular backup set using rman
Hi all, My OS: UNIX DB : 10.2.0.1 As i checked the database it will indicate datafile 5 is header corruption. yesterday i take complete online backup of the database using RMAN but now when i run restore database command it will show as ORA-19870: er