Declaring private cursors in package header
Hi.
Im tunning a PLSQL package , and im having the following doubt.
This package has alot of cursors declared in the package header, but the cursors are private. (called within the package only)
Could the public functions from the package have a loss of performance by using those cursors? (should the private cursors be declared within the functions or at least inside package body?)
Im kind of new with plsql, so please bare with me :) .
Cheers.
there's no performance loss.
however, unless the cursors are to be accessible outside the package (which is what happens when they are declared in the spec (not header)), then there's no benefit either. if they are truely meant to be private, then you can remove them from the spec. and if you're wrong, and they are called from elsewhere, you'll find out soon enough.
Similar Messages
-
Can we declare a Cursor in Package Specs?
Dear buddies
Can I Declare a Cursor in Package Specs so that I can call that cursor and use its data in some procedures and functions of package. Otherwise I've to write that cursor for every sub-program of a package which I don't feel a smart way to accomplish the job.Hi,
here is a short example with the whole way down. Maybe the concept is getting clearer with this:
first of all, if you do not have the table emp, here the DDL for this example.
Be carefull, works only for german clients because of the names of months, sorry for that.
CREATE TABLE EMP
(EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2));
set echo on
INSERT INTO EMP VALUES
(7369, 'SMITH', 'CLERK', 7902,
TO_DATE('17-DEZ-1980', 'DD-MON-YYYY'), 800, NULL, 20);
INSERT INTO EMP VALUES
(7499, 'ALLEN', 'SALESMAN', 7698,
TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30);
INSERT INTO EMP VALUES
(7521, 'WARD', 'SALESMAN', 7698,
TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
INSERT INTO EMP VALUES
(7566, 'JONES', 'MANAGER', 7839,
TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
INSERT INTO EMP VALUES
(7654, 'MARTIN', 'SALESMAN', 7698,
TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30);
INSERT INTO EMP VALUES
(7698, 'BLAKE', 'MANAGER', 7839,
TO_DATE('1-MAI-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
INSERT INTO EMP VALUES
(7782, 'CLARK', 'MANAGER', 7839,
TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
INSERT INTO EMP VALUES
(7788, 'SCOTT', 'ANALYST', 7566,
TO_DATE('09-DEZ-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES
(7839, 'KING', 'PRESIDENT', NULL,
TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10);
INSERT INTO EMP VALUES
(7844, 'TURNER', 'SALESMAN', 7698,
TO_DATE('8-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30);
INSERT INTO EMP VALUES
(7876, 'ADAMS', 'CLERK', 7788,
TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20);
INSERT INTO EMP VALUES
(7900, 'JAMES', 'CLERK', 7698,
TO_DATE('3-DEZ-1981', 'DD-MON-YYYY'), 950, NULL, 30);
INSERT INTO EMP VALUES
(7902, 'FORD', 'ANALYST', 7566,
TO_DATE('3-DEZ-1981', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES
(7934, 'MILLER', 'CLERK', 7782,
TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10);2. Package Spec:
create or replace
package test_cursor as
--Type for the returncode of the function
TYPE typ_emp IS TABLE OF emp%rowtype;
--Array for fetching, of course also possible in the body
t_emp typ_emp;
--function wich returns the array from fetching the cursor
function get_emp return typ_emp;
--function for manupilation data retrieved by the function
PROCEDURE man_emp;
end test_cursor;3. Package Body
create or replace
package body test_cursor as
FUNCTION get_emp RETURN typ_emp AS
cursor c_emp is select * from emp;
BEGIN
open c_emp;
fetch c_emp BULK COLLECT INTO t_emp;
CLOSE c_emp;
--t_emp returns the whole table set from emp
return t_emp;
end get_emp;
PROCEDURE man_emp AS
--just for not confusing names, is the same as t_emp of course
v_emp_array typ_emp;
BEGIN
--call the function and retrieve the whole data set
v_emp_array := get_emp;
--now manipulate the data, in this case just write the names to the calling client
FOR rec IN v_emp_array.FIRST .. v_emp_array.LAST
loop
dbms_output.put_line(v_emp_array(rec).ename);
end loop;
end man_emp;
end test_cursor;4. Calling the procedure
SET serveroutput ON
exec test_cursor.man_emp;5. And this is the result:
anonymer Block abgeschlossen
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLERPlease be aware, this is just for demonstration purpose, of course it makes no sense to display the names this way. But how to call a funktion returning arrays with datasets from fetching cursors is shown here.
Hth
Joerg -
Initialize record type constant in package header
I am creating a package to hold application constants in Oracle 8.1.7.4. I want one of my constants to be a programmer-defined record type. A constant table type of that record type will then hold multiple records. I'm new to using record data types though, and think I'm having a syntactical problem. Here's a snip of the code. Any ideas?
CREATE OR REPLACE PACKAGE kes_constants
AS
TYPE T_CLASS IS RECORD (code classes.cls_code%TYPE);
TYPE T_CLASSES IS TABLE OF T_CLASS
INDEX BY BINARY_INTEGER;
con_proj_realestate CONSTANT T_CLASS;
con_proj_realestate.code := 'RE';
END kes_constants;It was a bit of a simplification, but I see your point. The desire is to create varchar2 constants and a collection constant containing said varchar2 constants. The values of the constants could be stored in a table instead and selected into the collection at initialization, but I'd like to do it all in the package header without any database tables if possible.
I've modified my code to use varchar2 rather than records to hold the values. Then I try to initialize the collection with the constant values. When I compile my example code as shown here I get error: "PLS-00492: variable or constant initialization may not refer to functions declared in the same package". When I take the type definitions out of this package and put them in a separate package, change the code shown here to refer to the types with absolute dot notation names, I get error: "PLS-00222: no function with name 'T_CLASS_TABLE' exists in this scope".
It seems to me there should be a way to do this. Do you think I'm way off base?
Thanks for your help, Andrew.
CREATE OR REPLACE PACKAGE kes_constants
AS
TYPE t_class_table IS TABLE OF VARCHAR2(4)
INDEX BY BINARY_INTEGER;
TYPE t_rtype_table IS TABLE OF VARCHAR2(4)
INDEX BY BINARY_INTEGER;
con_proj_realestate CONSTANT VARCHAR2(4) := 'RE';
con_proj_vehiclesv CONSTANT VARCHAR2(4) := 'V';
con_proj_other CONSTANT VARCHAR2(4) := 'O';
con_proj_maint CONSTANT VARCHAR2(4) := 'M';
con_proj_replacement CONSTANT VARCHAR2(4) := 'CR';
con_proj_swing CONSTANT VARCHAR2(4) := 'RS';
con_proj_refurb CONSTANT VARCHAR2(4) := 'RF';
con_proj_additions CONSTANT VARCHAR2(4) := 'EA';
con_proj_cap CONSTANT t_class_table := t_class_table(con_proj_realestate,
con_proj_vehicles,
con_proj_other,
con_proj_replacement,
con_proj_swing,
con_proj_refurb,
con_proj_additions);
con_proj_noncap CONSTANT t_class_table := t_class_table(con_proj_maint);
END kes_constants; -
Private Constructs in Packages
Can't I refer a private function in a SQL statement which is inside a Public Procedure. Both Private Function and Public Procedure are in the same Package.
I just recreated the scenario using a simple procedure and a function inside a Package. Please take a look at the errors which I got when trying to create the package body.
CREATE OR REPLACE PACKAGE Pac_1 AS
PROCEDURE Pub_Pro;
END pac_1;
CREATE OR REPLACE PACKAGE BODY Pac_1 AS
FUNCTION Pri_Fun RETURN VARCHAR2 AS
BEGIN
RETURN 'Vamsi';
END Pri_Fun;
PROCEDURE Pub_Pro AS
BEGIN
INSERT INTO Test VALUES (Pri_Fun);
END Pub_Pro;
END Pac_1;
ERROR I GOT
Line Pos Text
116 Create package body, executed in 0.107 sec.
130 26 PLS-00231: function 'PRI_FUN' may not be used in SQL
130 26 PL/SQL: ORA-00904: "PRI_FUN": invalid identifier
130 1 PL/SQL: SQL Statement ignored
Total execution time 0.122 sec.
* Same execution works if the make the Function I used as a Public Function by also declaring it in the Package Specification AND also works if I use the function in a PL/SQL expression and not in a SQL statement.*
Thanks in Advance.
VamsiThis is absolutely natural behaviour.
Look at in what Oracle translates your call of the SQL statement from PL/SQL then the function
is specified as public:
SQL> CREATE OR REPLACE PACKAGE Pac_1 AS
2
3 PROCEDURE Pub_Pro;
4 FUNCTION Pri_Fun RETURN VARCHAR2;
5
6 END pac_1;
7 /
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY Pac_1 AS
2
3
4 FUNCTION Pri_Fun RETURN VARCHAR2 AS
5
6 BEGIN
7
8 RETURN 'Vamsi';
9
10 END Pri_Fun;
11
12
13 PROCEDURE Pub_Pro AS
14
15 BEGIN
16
17 INSERT INTO Test easy_to_find VALUES (Pri_Fun);
18
19 END Pub_Pro;
20
21 END Pac_1;
22 /
Package body created.
SQL> exec pac_1.pub_pro
PL/SQL procedure successfully completed.
SQL> select sql_text from v$sqlarea where sql_text like '%EASY_TO_FIND%';
SQL_TEXT
INSERT INTO TEST EASY_TO_FIND VALUES (PAC_1.PRI_FUN)The last row is the real form of INSERT operator. It shows that Oracle resolves this reference by
trying to use the public function from the package specification.
If you want to use private function in SQL you have to use local variables:
SQL> CREATE OR REPLACE PACKAGE Pac_1 AS
2
3 PROCEDURE Pub_Pro;
4
5 END pac_1;
6 /
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY Pac_1 AS
2
3
4 FUNCTION Pri_Fun RETURN VARCHAR2 AS
5 BEGIN
6 RETURN 'Vamsi';
7 END Pri_Fun;
8
9
10 PROCEDURE Pub_Pro AS
11 x varchar2(10);
12 BEGIN
13 x := pri_fun;
14 INSERT INTO Test easy_to_find VALUES (x);
15 END Pub_Pro;
16
17 END Pac_1;
18 /
Package body created.And meanwhile it would be better to use local variable form even if your function is public ;)
Rgds. -
Variable refreshing in package header
Hello Experts,
Through a function in my package header I fill up a global variable. The function gets the value out of a table in my database:
PV_FILENAME PARAMETERS.VALUE%TYPE DL$PARAMETERS.VALUE_BY_NAME('FILE_NAME'); This works fine, however when I change the value in the table and commit the changes the problem begins.
When I execute the following statement to check if the value changed:
select DL$PARAMETERS.VALUE_BY_NAME('FILE_NAME') from dual;It shows me the new value...
But the variable in the package still contains the old value. When I restart sql developer the variable adjusts.
Does anyone know what I can do? It seems like the package is caching the value or something. Or is it obligatory to put global variables in the body?
greets
Edited by: iadgroe on May 21, 2012 6:37 AMHello Arun,
I can check this because the return value I use in the function is the name of an xml file (e.g. 'xmlfile_1.xml'):
SELECT .....
FROM XMLTABLE('/employees/employee' PASSING XMLTYPE(BFILENAME(PV_DIRECTORY, -->PV_FILENAME<--), NLS_CHARSET_ID('AL32UTF16'))
COLUMNS USERNAME VARCHAR2(20) PATH './name/userName' , TIMEREG XMLTYPE PATH './timeRegistration/days' ) H,
XMLTABLE('days/day' PASSING H.TIMEREG COLUMNS TIMESHEET_DATUM In the above code I use the name to retrieve the xml file. I insert the data in the xml file into one of my own tables.
When I change the value of the xml-name(e.g. 'xmlfile_2.xml') in my table that the function returns, the variable should reference another xml-file. However when I execute the procedure in my package it still insert data from the previous xml(xmlfile_1.xml) file into my table.
I this clear enough? :-)
Thanks a lot! -
9I: How can get the names of all objects in a package header? [RESOLVED]
Is there a way to get the names fo all objects (function, procedures) from package header
select * from user_objects only gives the the header name, I need the object in the header.
John
Message was edited by:
johnminkjanHere is an example with dbms_output package!
SQL> select owner, object_name,procedure_name from all_procedures
2 where object_name='DBMS_OUTPUT';
OWNER OBJECT_NAME PROCEDURE_NAME
SYS DBMS_OUTPUT GET_LINES
SYS DBMS_OUTPUT GET_LINE
SYS DBMS_OUTPUT NEW_LINE
SYS DBMS_OUTPUT PUT_LINE
SYS DBMS_OUTPUT PUT_LINE
SYS DBMS_OUTPUT PUT
SYS DBMS_OUTPUT PUT
SYS DBMS_OUTPUT DISABLE
SYS DBMS_OUTPUT ENABLE
9 rows selected.
"or"
SQL> desc dbms_output -
Package header failed verification during sfr package download
Hello,
I am trying to download the package asasfr-5500x-boot-5.3.1-152.pkg. But everytime it comes with an error "Package header failed verification - object of type 'NoneType' has no len() Please verify that the package is not corrupted.Upgrade aborted." I have tried with ftp as well as http. But every case it is happening the same. My question is whether it is the right package. From Cisco doc I have read asasfr-sys-5.3.1-44.pkg. But this version I didn't found on cisco download software site.
QW-SFR-boot>system install http://www.XXXXX.com/asasfr-sys-531-152.pkg
Verifying
Package header failed verification - object of type 'NoneType' has no len()
Please verify that the package is not corrupted.
Upgrade aborted.
thanks in advance.
SaimunSounds like the package could have been corrupted when downloaded from CCO. Did you run an MD5 hash of the package and compare to the values posted on CCO for the file?
There's a number of free MD5 checksum programs that you can use. My favorite is one called HashCheck.
Hash for SFR package is in red below. If it doesn't match, re-download the image from CCO again and make sure the hashes match. -
hi all,
is it possible to declare a cursor like this ..???
declare
cursor is select ab.col1,cd.col2 from
(select a.col1,b.col2
from a,b
conditions)ab
(select c.col1,d.col2...
from c,d
conditions..)cd
i am getting the following error
PL/SQL: ORA-00923: FROM keyword not found where expectedPlenty of syntactical error in your cursor declaration. It should be like this ->
cursor c1
is
select ab.col1,
cd.col2
from (
select a.col1,
b.col2
from a,b
where conditions
) ab,
select c.col1,
d.col2...
from c,d
where conditions..) cd
where ab.cond1 = cd.cond1;Regards.
Satyaki De. -
Why does SQL Developer insist on opening up the package header?
When expanding a package (clicking the '+') SQL Developer keeps opening up the package header (at least the first time you click it). I'm very rarely interested in viewing the header and it's annoying to have all those tabs open.
See SQL Dev 2.1 RC1 - Expanding packages under Schema Browser
Don't know if dev filed a bug against this, so to be sure you'd have to log a SR on Metalink/MOS.
Regards,
K. -
Error when Compiling package header and body - how change pkb file associat
Hi everyone,
it has already been noticed elsewhere (in the thread "Compiling package header and body" of Jan 12, 2010) that the compilation of package scripts sometimes fails (apparently because of a sqldeveloper bug) when the script contains the terminating slash /.
Is this bug still open?
Next question: In the above mentioned thread it is recommended as workaround to change the corresponding file type association from pl/sql to sql. I would like to do that; I know the place in the preferences dialog, but most of the associations there seem to be hard coded and cannot be changed.
I would appreciate any ideas!
Thanks in advance,
user8632123.For the workaround: you'd have to change the file's extension, not the association (to e.g. .sql).
Have fun,
K. -
Declaring a cursor in an Anonymous Block
How do I declare a cursor in an Anonymous Block? I've looked in several sources and haven't found anything that explains this.
Does anyone have a link to where this would be explained?
ThanksDepends on whether you're talking about an explicit or an implicit cursor, but it's basically the same way you declare either in a non-anonymous block, i.e.
DECLARE
CURSOR cursor_name IS ...
BEGIN
END;or
BEGIN
FOR x IN (SELECT ... )
LOOP
END LOOP;
END;Justin -
can we declare private methods in Interface?? private members for the interface
can we declare private methods in Abstract class??
like private int i; in interface or abstract.kingcnu wrote:
thank you michaleyou are welcome
Not sure what you mean with "implement primitive datatypes"? You can use Java Primitives (char, int, long, float, double, byte) everywhere you wish to. -
Hi ,
I have the following I am declaring a cursor within a procedure before a BEGIN - How can i decalre the cursor inside the the procedure
as this is not correct below ?
Something like so :
PROCEDURE abc(
table_name IN VARCHAR2)
IS
--v_sql varchar2(4000);
DECLARE
temp VARCHAR(20);
CURSOR ...................IS
BEGIN
using the cursor
END
Edited by: user618557 on Feb 25, 2009 3:47 AMuser618557 wrote:
Thanks that worked ...That's good.
As to your new question; I have no idea as to what you are trying to do.
Ever seen this?
SQL> select dbms_metadata.get_ddl ('TABLE', 'EMP', 'SCOTT') from dual
DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT')
CREATE TABLE "SCOTT"."EMP"
( "EMPNO" NUMBER(4,0) NOT NULL ENABLE,
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0)
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"
1 row selected.Regards
Peter -
How to declare a cursor with stored proc?
Hi All,
Can we declare a cursor with stored proc?
For Example -
CREATE PROCEDURE DDL_proc() LANGUAGE SQLSCRIPT AS
CURSOR c_cursor1 (v_isbn VARCHAR(20)) FOR CALL SYS.GET_OBJECT_DEFINITION(<SCHEMA_NAME>, <TABLE_NAME>).;
BEGIN
FOR cur_row as c_cursor1 DO
END FOR;
END;
Could you please have a look on the same?
Thank you,
VijeeshOracle PL/SQL also has a select into statement which is described in the same manual the link takes you to part of.
select column_list
into variable_list
from table(s)
where conditions
The PL/SQL Users Guide is something you are going to want to have gone over cover to cover before you start converting because so that you make the best choices for how to rewrite the code: select into, explicit cursor, implicit cursor, for loop, simple loop, while loop, collections,bulk load, etc ....
HTH -- Mark D Powell -- -
Declaration of cursor type in package/sp
Hi,
I'm using package with sp that using cursor like below:
--#1============================ PACKAGE
{PACKAGE ORA_PK_TR2 AS
Type CURS_01 IS REF CURSOR; --- return RYBB.T_COLLECT%rowtype;
Procedure ORA_SP_CUST (EXP_DATE IN date,
END
--#2============================ BODY
create or replace
PACKAGE BODY ORA_PK_TR2 as
Procedure ORA_SP_CUST (EXP_DATE IN date,
open_CURS_01 OUT CURS_01 ) IS
BEGIN
SQL_string = '(select * from RYBB.T_COLLECT where col='||EXP_DATE)'
OPEN open_CURS_01 FOR SQL_STRING;
END;
--#3============================= RUN_PORTION
DECLARE
EXP_DATE DATE;
OPEN_CURS_01 RYBB.ORA_PK_TR2.CURS_01;
TYPE_IN RYBB.T_COLLECT%ROWTYPE; --/***** <==== need to move into package
BEGIN
EXP_DATE := '10-sep-10';
RYBB.ORA_PK_TR2.ORA_SP_CUST(
EXP_DATE => EXP_DATE,
OPEN_CURS_01 => OPEN_CURS_01
LOOP
FETCH open_CURS_01 INTO TYPE_IN;
EXIT WHEN open_CURS_01%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(TYPE_IN.COL1||' '||TYPE_IN.COL2); --/ for sample
END LOOP;
END;}
I need to put TYPE_IN declation for cursor inside the package, so user who will run this pack/sp won't deal with this structure. How I can do this,
I tried to use <return RYBB.T_COLLECT%rowtype;> in package but then I get :
Error(122,6): PLS-00455: cursor 'open_CURS_01' cannot be used in dynamic SQL OPEN statement.
Not sure can I put it somehow into the BODY and make it available for user ?
Appreciate you help.
BEst
Trent
Edited by: trento on Sep 13, 2010 2:36 PM--#1============================ PACKAGE
PACKAGE ORA_PK_TR2 AS
Type CURS_01 IS REF CURSOR; --- return RYBB.T_COLLECT%rowtype;
Type CURS_01_TYP IS RYBB.T_COLLECT%rowtype;
--#3============================= RUN_PORTION
DECLARE
EXP_DATE DATE;
OPEN_CURS_01 RYBB.ORA_PK_TR2.CURS_01;
TYPE_IN RYBB.ORA_PK_TR2.CURS_01_TYP;
BEGIN
Difficult to read, don't you think?
Try use the tags B-)
Your code has other issues and (as shown here) won't compile. If you have a working solution, why not paste that instead?
Maybe you are looking for
-
IPhone 4S battery draining too fast...Need to charge 3 times a day
Finally after 4 years I was very excited to get an iPhone that has all the features I wanted…but I am disappointed so far. I purchased an iPhone 4S 32GB on 10/7 and has not been impressed with the battery life. I have to charge my iPhone at least 3 t
-
How do I sign up for an itune's account without a credit card if I already have an apple ID?
How do I sign up for an itune's account without a credit card if I already have an apple ID? There is no none option for payment method, even if I buy a free app. I have no credit card.
-
Assassin's Creed and Boot Camp bricked my iMac!
Okay, so it is not quite bricked, but it is very close, and I am at a loss. For the first time I have exhausted my knowledge and a series of Google searches and have come up short. Hopefully one of you guys can help. Sorry about how long this is, but
-
Bootcamp with multiple drives?
Hi there, I have a mid 2012 macbook pro running 10.9.1. I recently installed an SSD in place of my old HDD and put the HDD in place of the optical drive. I was hoping to install windows 7 (or 8.1) with bootcamp onto the HDD but there is a partition e
-
How do I get IE7 after iTunes install banished it?
I installed iTunes and Quicktime on my Windows XP, IE7 PC. I subscribed to get an automatic download of a radio broadcast. Now, whenever I click on icon for IE7, I get iTunes and I can't get IE7.