Procedures declaring a cursor
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 AM
user618557 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
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 -
I have a procedure within a cursor.
How do I assign the "termination_date" calculated in the cursor to the "v_end_date" and pass it into the procedure?
declare
cursor x is select a.store_no || '-' || b.store_name name,
a.store_no,
landlord_turnover_date execution_date,
open_date commencement_date,
decode(
decode(sign(round(months_between(nvl(option1_end_date,open_date), open_date)/12,0) - 10), 1, option1_end_date,
decode(sign(round(months_between(nvl(option2_end_date,open_date), open_date)/12,0) - 10), 1, option2_end_date,
decode(sign(round(months_between(nvl(option3_end_date,open_date), open_date)/12,0) - 10), 1, option3_end_date,
decode(sign(round(months_between(nvl(option4_end_date,open_date), open_date)/12,0) - 10), 1, option4_end_date,
decode(sign(round(months_between(nvl(option5_end_date,open_date), open_date)/12,0) - 10), 1, option5_end_date,
decode(sign(round(months_between(nvl(option6_end_date,open_date), open_date)/12,0) - 10), 1, option6_end_date)))))),null,
nvl(option6_end_date,nvl(option5_end_date,nvl(option5_end_date,nvl(option4_end_date,nvl(option3_end_date,nvl(option2_end_date,option1_end_date)))))),
decode(sign(round(months_between(nvl(option1_end_date,open_date), open_date)/12,0) - 10), 1, option1_end_date,
decode(sign(round(months_between(nvl(option2_end_date,open_date), open_date)/12,0) - 10), 1, option2_end_date,
decode(sign(round(months_between(nvl(option3_end_date,open_date), open_date)/12,0) - 10), 1, option3_end_date,
decode(sign(round(months_between(nvl(option4_end_date,open_date), open_date)/12,0) - 10), 1, option4_end_date,
decode(sign(round(months_between(nvl(option5_end_date,open_date), open_date)/12,0) - 10), 1, option5_end_date,
decode(sign(round(months_between(nvl(option6_end_date,open_date), open_date)/12,0) - 10), 1, option6_end_date))))))) termination_date,
option1_base_rent,
option1_end_date,
option2_base_rent,
option2_end_date,
option3_base_rent,
option3_end_date,
option4_base_rent,
option4_end_date,
option5_base_rent,
option5_end_date,
option6_base_rent,
option6_end_date,
c.vendor_number,
d.vendor_id,
e.vendor_site_id,
cost_center
from lease_terms a, store b, workrent c, po_vendors d, po_vendor_sites_all e
where flag = 'Y'
and a.store_no = b.store
and a.store_no = c.store_no
and c.rent_amt_5301 > 0
and c.vendor_number = d.segment1
and d.vendor_id = e.vendor_id
and c.pay_site_name = e.vendor_site_code;
v_loc_id number;
v_norm varchar2(1);
v_term_id number;
v_lease_id number;
v_change_id number;
v_div varchar2(2);
v_exp_id number;
v_acc_id number;
PROCEDURE insert_payment ( v_term_id IN number,
v_lease_id IN number,
v_change_id IN number,
v_start_date IN date,
v_end_date IN date,
v_vendor_id IN number,
v_vendor_site_id IN number,
v_amt IN number,
v_normalize IN varchar2,
v_expense_id IN NUMBER,
v_lia_id IN NUMBER)
IS
BEGIN
insert into pn_payment_terms_all (
payment_term_id,
last_update_date,
last_updated_by,
creation_date,
created_by,
last_update_login,
payment_purpose_code,
payment_term_type_code,
frequency_code,
lease_id,
lease_change_id,
start_date,
end_date,
vendor_id ,
vendor_site_id,
actual_amount,
set_of_books_id,
currency_code,
rate,
org_id,
normalize,
schedule_day,
lease_status)
values
( v_term_id,
SYSDATE,
-1,
SYSDATE,
-1,
0,
'RENT',
'BASER',
'MON',
v_lease_id,
v_change_id,
v_start_date,
v_end_date,
v_vendor_id,
v_vendor_site_id,
v_amt,
1,
'USD',
1,
101,
v_normalize,
1,
'ACT');btw the nested DECODEs could be simplified greatly using CASE, or if this is 8i you might still be able to unnest them with something like:
decode(1,
sign(round(months_between(nvl(option1_end_date,open_date), open_date)/12,0) - 10), option1_end_date,
sign(round(months_between(nvl(option2_end_date,open_date), open_date)/12,0) - 10), option2_end_date,
sign(round(months_between(nvl(option3_end_date,open_date), open_date)/12,0) - 10), option3_end_date,
sign(round(months_between(nvl(option4_end_date,open_date), open_date)/12,0) - 10), option4_end_date,
sign(round(months_between(nvl(option5_end_date,open_date), open_date)/12,0) - 10), option5_end_date,
sign(round(months_between(nvl(option6_end_date,open_date), open_date)/12,0) - 10), option6_end_date,
NVL(...) )Unfortunately I rather lost track of it after that. You may also be able to simplify the nested NVLs using COALESCE, e.g.
coalesce
( option6_end_date,
option5_end_date,
option5_end_date,
option4_end_date,
option3_end_date,
option2_end_date,
option1_end_date ), ...although if this is 8i COALESCE may not be available.
Why they couldn't just have added more optional arguments to NVL I will never understand. -
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 -- -
Testing procedure that returns cursor.
Please
How Can I test a procedure that returns a cursor? I need to execute it on sqlplus.
Thank you very muchHi,
Example using HR schema:
The proc_cursor procedure returns a cursor and the block below test it
SET SERVEROUTPUT ON
DECLARE
TYPE t_ref_cur IS REF CURSOR;
v_cursor t_ref_cur;
v_employee employees%ROWTYPE;
PROCEDURE proc_cursor(p_cursor OUT t_ref_cur) IS
v_sql VARCHAR2(4000);
BEGIN
v_sql := 'SELECT * FROM EMPLOYEES';
OPEN p_cursor FOR v_sql;
END;
BEGIN
proc_cursor(p_cursor => v_cursor);
LOOP
FETCH v_cursor
INTO v_employee;
EXIT WHEN v_cursor%NOTFOUND;
dbms_output.put_line(v_employee.employee_id);
END LOOP;
CLOSE v_cursor;
END;
/Regards, -
SSRS 2008 Using Stored Procedure with SysRef Cursor Oracle
Hi,
I am new to SSRS.
I have a ssrs procedure which runs to Return Result of Select Query. ( In form of SYS REF Cursor in Oracle ).
When I am trying to see the Value in Test Window. It is showing Output. But when I m Running the Report in the environment it is giving me error.
Please help me if there is a better way of handling it.
As I read in some forum as SSRS has issue handling SYS Ref Cursor...
Thanks
Priyank
Thanks Priyank PiyushHi Priyank,
According to your description, you are use a Stored Procedure from SysRef Cursor Oracle as the report dataset. The query works well in Dataset Query Designer, while an error occurs when you render the report.
As per my understanding, the problem is that there are something wrong with the report design. Please double-check the design of the report. In order to trouble shoot the problem more efficiently, could you please post the detail error message? Then we can
make further analysis.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
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. -
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 -
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? -
Unable to create plsql procedure, fails at cursor
This was a working procedure which would take ID and then copy data from source parameter to destination parameter. Now I would like to have NAME as parameter, I have changed the code to accommodate the new parameters. But I am not able to create the procedure. I am getting 3 error at CURSOR (in bold red). I would like really appreciate if someone can take a look and let me know what is wrong.
Thanks in advance
3 ERRORs
1. PLS-00103: Encountered the symbol "CUR_V_HSP_COLUMN_DETAIL" when expecting one of the following:
:= . ( @ % ;
The symbol ":=" was substituted for "CUR_V_HSP_COLUMN_DETAIL" to continue.
2. PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following:
The symbol "(" was substituted for "NUMBER" to continue.
3. PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following:
CREATE OR REPLACE procedure EPM_PLAN_PLANSAMP.Copy_Details_test1 --Arguments
( in_From_Version_Name IN VARCHAR2, --HSP_object.OBJECT_NAME - Version From
in_From_Scenario_Name IN VARCHAR2 , --HSP_object.OBJECT_NAME - Scenarios From
in_From_Year_Name IN VARCHAR2 , --HSP_object.OBJECT_NAME - Year From
in_To_Version_Name IN VARCHAR2, --HSP_object.OBJECT_NAME - Version To
in_To_Scenario_name IN VARCHAR2, --HSP_object.OBJECT_NAME - Scenarios To
in_To_Year_Name IN VARCHAR2 --HSP_object.OBJECT_NAME - Year To
IS
v_From_Object_Id number; -- Version From
s_From_Object_Id number; -- Scenarios From
y_From_Object_Id number; -- Year From
v_To_Object_Id number; -- Version To
s_To_Object_Id number; -- Scenarios To
y_To_Object_Id number; -- Year To
BEGIN
Select object_id into v_From_Object_Id
from hsp_object
where object_type = 35
and object_name = in_from_version_name;
Select object_id into s_From_Object_Id
from hsp_object
where object_type = 31
and object_name = in_from_scenario_name;
Select object_id into y_From_Object_Id
from hsp_object
where object_type = 38
and object_name = in_from_year_name;
Select object_id into v_To_Object_Id
from hsp_object
where object_type = 35
and object_name = in_to_version_name;
Select object_id into s_To_Object_Id
from hsp_object
where object_type = 31
and object_name = in_to_scenario_name;
Select object_id into y_To_Object_Id
from hsp_object
where object_type = 38
and object_name = in_to_year_name;
--Select Supporting Details for the current Version
CURSOR Cur_V_HSP_COLUMN_DETAIL (cV_From_Object_Id IN NUMBER, cS_From_Object_Id IN NUMBER) IS
Select DETAIL_ID From EPM_PLAN_PLANSAMP.HSP_COLUMN_DETAIL Where DIM5 = cV_From_Object_Id AND DIM1 = cS_From_Object_Id;
li_DETAIL_ID NUMBER;
Li_Next_DETAIL_ID NUMBER;
FETCH_STATUS NUMBER := 0;
v_step_name varchar2(200);
v_rec_cnt number := 0;
v_cnt number;
v_err varchar2(2000);
-----------------------------------------Begin Copy Version ---------------------------
BEGIN
-- Delete Next version if already exists
v_step_name := 'Delete on HSP_COLUMN_DETAIL_ITEM';
Delete from HSP_COLUMN_DETAIL_ITEM
Where DETAIL_ID in (Select DETAIL_ID from HSP_COLUMN_DETAIL
Where DIM5 = v_To_Object_Id AND DIM1 = s_To_Object_Id);
v_cnt := sql%rowcount;
insert into t_copy_supporting_dtls_log values (v_step_name, v_cnt,1,'Success',sysdate);
v_step_name := 'Delete on HSP_COLUMN_DETAIL';
Delete from HSP_COLUMN_DETAIL
where DIM5 = v_To_Object_Id AND DIM1 = s_To_Object_Id;
v_cnt := sql%rowcount;
insert into t_copy_supporting_dtls_log values (v_step_name, v_cnt,1,'Success',sysdate);
Open Cur_V_HSP_COLUMN_DETAIL(v_From_Object_Id, s_From_Object_Id);
v_step_name := 'Inserts ';
LOOP
FETCH Cur_V_HSP_COLUMN_DETAIL INTO li_DETAIL_ID;
EXIT WHEN Cur_V_HSP_COLUMN_DETAIL%NOTFOUND;
-- Find next detail_id
Select Max(DETAIL_ID) + 1 INTO Li_Next_DETAIL_ID From HSP_COLUMN_DETAIL;
-- Insert Into HSP_COLUMN_DETAIL Table
Insert Into HSP_COLUMN_DETAIL ( DETAIL_ID , PLAN_TYPE , DIM1 , DIM2 , DIM3 , DIM4 , DIM5 , DIM6 ,
DIM7 , DIM8 , DIM9 , DIM10 , DIM11 , DIM12 , DIM13 , DIM14 , DIM15 ,
DIM16 , DIM17 , DIM18 , DIM19 , DIM20 )
Select Li_Next_DETAIL_ID , PLAN_TYPE , S_To_Object_Id , DIM2 , DIM3 , DIM4 , V_To_Object_Id , DIM6 ,
DIM7 , DIM8 , DIM9 , DIM10 , DIM11 , DIM12 , DIM13 , DIM14 , DIM15 ,
DIM16 , DIM17 , DIM18 , DIM19 , DIM20
From HSP_COLUMN_DETAIL
Where DETAIL_ID = li_DETAIL_ID;
v_rec_cnt := v_rec_cnt + sql%rowcount;
-- Insert Into HSP_COLUMN_DETAIL_ITEM Table
Insert Into HSP_COLUMN_DETAIL_ITEM ( DETAIL_ID , VALUE , POSITION , GENERATION , OPERATOR , LABEL)
Select Li_Next_DETAIL_ID , VALUE , POSITION , GENERATION , OPERATOR , LABEL
From HSP_COLUMN_DETAIL_ITEM Where DETAIL_ID = li_DETAIL_ID;
v_rec_cnt := v_rec_cnt + sql%rowcount;
END LOOP;
Close Cur_V_HSP_COLUMN_DETAIL;
insert into t_copy_supporting_dtls_log values (v_step_name, v_rec_cnt,1,'Success',sysdate);
commit;
exception when others then
rollback;
v_err := substr(sqlerrm,1,2000);
insert into t_copy_supporting_dtls_log values (v_step_name, 0,-1,v_err,sysdate);
commit;
END;
END;All the following statements should go into the declaration section (i.e. before the first begin)
CURSOR Cur_V_HSP_COLUMN_DETAIL (cV_From_Object_Id IN NUMBER, cS_From_Object_Id IN NUMBER) IS
Select DETAIL_ID From EPM_PLAN_PLANSAMP.HSP_COLUMN_DETAIL Where DIM5 = cV_From_Object_Id AND DIM1 = cS_From_Object_Id;
li_DETAIL_ID NUMBER;
Li_Next_DETAIL_ID NUMBER;
FETCH_STATUS NUMBER := 0;
v_step_name varchar2(200);
v_rec_cnt number := 0;
v_cnt number;
v_err varchar2(2000); -
Procedure Problem - Open Cursor for SQL
in my form I added a procedure. this is working fine in SQL but when I try to run through form it gives error.
Code of Procedure:-
PROCEDURE Proc_Purchase_all IS
BEGIN
declare
v_sql varchar2(4000);
v_user varchar2(30);
v_sep varchar2(20);
v_cur SYS_REFCURSOR;
pur_rec purchase%ROWTYPE;
begin
for u in (SELECT * FROM ALL_TABLES WHERE OWNER LIKE 'BMP%' and table_name ='PURCHASE') loop
v_sql := v_sql || v_sep || 'SELECT * from '||u.owner||'.purchase';
v_sep := ' UNION ALL ';
end loop;
open v_cur for v_sql;
Loop
Fetch v_cur into pur_rec;
Exit when v_cur%NOTFOUND;
--DBMS_OUTPUT.PUT_LINE (PUR_REC.S_CODE||' '||Pur_rec.s_name);
End Loop;
close v_cur;
end;
END;Error Line
* open v_cur for v_sql;*
Error is :-
Encountered the symbol 'V_SQL' when expecting one of the following.
select
Encountered the symbol END when expecting one of the following.
Begin function package pragma procedure externalI changed my code ALSO Showing SAME error .
SQL> Declare
2 v_sql varchar2(4000);
3 v_user varchar2(30);
4 v_sep varchar2(20);
5 TYPE R_CURSOR IS REF CURSOR RETURN purchase%ROWTYPE;
6 C_Pur R_CURSOR;
7 ER C_pur%ROWTYPE;
8 BEGIN
9 for u in (SELECT * FROM ALL_TABLES WHERE OWNER LIKE 'BMP%' and table_name ='PURCHASE') loop
10 v_sql := v_sql || v_sep || 'SELECT * from '||u.owner||'.purchase';
11 v_sep := ' UNION ALL ';
12 end loop;
13 OPEN C_Pur FOR V_sql;
14 LOOP
15 FETCH C_Pur INTO ER;
16 EXIT WHEN C_Pur%NOTFOUND;
17
18 END LOOP;
19 CLOSE C_Pur;
20 END;
21 /
OPEN C_Pur FOR V_sql;
ERROR at line 13:
ORA-06550: line 13, column 7:
PLS-00455: cursor 'C_PUR' cannot be used in dynamic SQL OPEN statement
ORA-06550: line 13, column 2:
PL/SQL: Statement ignoredEdited by: Ahmed on Jan 13, 2011 1:58 AM -
Call procedure with return cursor
Dear All,
there is a cast with my job. i have procedure like below:
CREATE OR REPLACE PROCEDURE ISISALL.P_TEST (PARAM1 IN VARCHAR2,RETURN_TABLE OUT SYS_REFCURSOR) IS
BEGIN
OPEN RETURN_TABLE FOR
SELECT FIELD2,FIELD3 FROM MYTABLE
WHERE FIELD1=PARAM1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END P_TEST;
my question, How to call the procedure ? i want insert table with source from the procedure (P_TEST)..>
How to call the procedure ? i want insert table with source from the procedure (P_TEST)..
>
This code sample that you can test in the SCOTT schema should give you enough information
CREATE OR REPLACE TYPE SCOTT.local_type IS OBJECT (
empno NUMBER(4),
ename VARCHAR2(10));
CREATE OR REPLACE TYPE SCOTT.local_tab_type IS TABLE OF local_type;
CREATE OR REPLACE PACKAGE SCOTT.test_refcursor_pkg
AS
TYPE my_ref_cursor IS REF CURSOR;
-- add more cursors as OUT parameters
PROCEDURE test_proc(p_ref_cur_out OUT test_refcursor_pkg.my_ref_cursor);
END test_refcursor_pkg;
CREATE OR REPLACE PACKAGE BODY SCOTT.test_refcursor_pkg
AS
PROCEDURE test_proc(p_ref_cur_out OUT test_refcursor_pkg.my_ref_cursor)
AS
l_recs local_tab_type;
BEGIN
-- Get the records to modify individually.
SELECT local_type(empno, ename) BULK COLLECT INTO l_recs
FROM EMP;
-- Perform some complex calculation for each row.
FOR i IN l_recs.FIRST .. l_recs.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(l_recs(i).ename);
END LOOP;
-- Put the modified records back into the ref cursor for output.
OPEN p_ref_cur_out FOR
SELECT * from TABLE(l_recs);
-- open more ref cursors here before returning
END test_proc;
END;
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
l_cursor test_refcursor_pkg.my_ref_cursor;
l_ename emp.ename%TYPE;
l_empno emp.empno%TYPE;
BEGIN
test_refcursor_pkg.test_proc (l_cursor);
LOOP
FETCH l_cursor
INTO l_empno, l_ename;
EXIT WHEN l_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_ename || ' | ' || l_empno);
END LOOP;
CLOSE l_cursor;
END;
/Are you sure you wouldn't be better off using a PIPELINED function instead? Then you can just select from it as if it were a table and do whatever you want with the data
-- type to match emp record
create or replace type emp_scalar_type as object
(EMPNO NUMBER(4) ,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2)
-- table of emp records
create or replace type emp_table_type as table of emp_scalar_type
-- pipelined function
create or replace function get_emp( p_deptno in number )
return emp_table_type
PIPELINED
as
TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;
emp_cv EmpCurTyp;
l_rec emp%rowtype;
begin
open emp_cv for select * from emp where deptno = p_deptno;
loop
fetch emp_cv into l_rec;
exit when (emp_cv%notfound);
pipe row( emp_scalar_type( l_rec.empno, LOWER(l_rec.ename),
l_rec.job, l_rec.mgr, l_rec.hiredate, l_rec.sal, l_rec.comm, l_rec.deptno ) );
end loop;
return;
end;
select * from table(get_emp(20)) -
I am creating a procedure to update the value (unique id) using cursor.the result is not what i want, it is updating all the rows,it seems that
where current of is not working in " WHERE CURRENT OF CUR_CPN;"
Declare
CURSOR CUR_CPN IS
SELECT * FROM CPN_MASTER_TEMP FOR UPDATE;
VAL NUMBER(5);
UNI_VAL NUMBER(15);
BEGIN
FOR R1 IN CUR_CPN
LOOP
SELECT ZONE_DETAILS_DATA.UTM_ZONE,ZONE_DETAILS_DATA.UNIQUE_ID INTO VAL,UNI_VAL FROM ZONE_DETAILS_DATA;
--SELECT ZONE_DETAILS_DATA.UTM_ZONE INTO VAL FROM ZONE_DETAILS_DATA;
--SELECT ZONE_DETAILS_DATA.UNIQUE_ID INTO UNI_VAL FROM ZONE_DETAILS_DATA;
IF R1.UTM_ZONE=VAL THEN
UPDATE CPN_MASTER_TEMP SET ZONE_DETAIL_ID_TEMP=UNI_VAL WHERE CURRENT OF CUR_CPN;
END IF;
END LOOP;
END;
output is as follows
SQL> SELECT ZONE_DETAIL_ID_TEMP FROM CPN_MASTER_TEMP;
ZONE_DETAIL_ID_TEMP
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
20 rows selected.
the output should be only for rows where uniqueid is existing, now it is updating all rows
please suggest help
thankxselect the rows only you want to update in cursor select condition
Edited by: 781237 on Jul 10, 2010 4:45 AM -
Stored procedure and ref cursor problem
I am trying to create a stored procedure that can be used in Crystal Reports. To do this I have to create a package and a ref cursor. My SQL is below:
--Package
Create or Replace Package Test_Package
as type test_type is ref cursor;
end;
--Procedure
Create or Replace Procedure Test_Proc
(test_cursor in out test_package.test_type,
parameter in string
as
begin
open test_cursor for
select ClientName from apbpman.cv_client where clientref = parameter;
end;
--When trying to execute the SP in Oracle I use:-
set serveroutput on
declare
test_cursor apbpman.test_package.test_type;
resultset test_cursor%rowtype;
begin
apbpman.test_proc(test_cursor,'0096');
if not test_cursor%isopen then
dbms_output.put_line ('OK');
else
dbms_output.put_line ('Not OK');
end if;
fetch test_cursor into resultset;
while test_cursor%found loop
dbms_output.put_line(resultset.ClientName);
fetch test_cursor into resultset;
end loop;
end;
Whenever this runs I receive the following error reports:
resultset test_cursor%rowtype;
ERROR at line 3:
ORA-06550: line 3, column 13:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 3, column 13:
PL/SQL: Item ignored
ORA-06550: line 11, column 25:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 11, column 2:
PL/SQL: SQL Statement ignored
ORA-06550: line 13, column 24:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 13, column 3:
PL/SQL: Statement ignored
ORA-06550: line 14, column 26:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 14, column 3:
PL/SQL: SQL Statement ignored
I have tried in vain to find a resolution to this but have failed. Please help.
Thanks,
PaulUnless you are running a really old version of Oracle, any weak ref cursor can just be declared SYS_REFCURSOR. Also, you can't use a weak ref cursor for %ROWTYPE. You can test the procedure in SQL*Plus by using it to populate a refcursor variable.
-
Newbie question, run procedure that returns cursor
Hi All,
I want to run a procedure that takes a cursor as an input and also returns a cursor and then display the contents of the output in Oracle SQL Developer.
If I right click on the procedure and click run I get something like;
DECLARE
P_REP_CURSOR CISADM.Cr_Package.CR_CURSOR;
P_BILL_ID CHAR(200);
BEGIN
P_REP_CURSOR := NULL;
P_BILL_ID := NULL;
MyProcedure(
P_REP_CURSOR => P_REP_CURSOR,
P_BILL_ID => P_BILL_ID
:P_REP_CURSOR := P_REP_CURSOR; --<-- Cursor
END;
I understand that in order to make this work I have to supply input variables for;
P_REP_CURSOR := NULL;
P_BILL_ID := NULL;
I can supply a bill id, but how do I
- supply a cursor as input and
- display the output cursor?
Thanks,
ec :-)866928 wrote:
Hi All,
I want to run a procedure that takes a cursor as an input and also returns a cursor and then display the contents of the output in Oracle SQL Developer.
If I right click on the procedure and click run I get something like;
DECLARE
P_REP_CURSOR CISADM.Cr_Package.CR_CURSOR;
P_BILL_ID CHAR(200);
BEGIN
P_REP_CURSOR := NULL;
P_BILL_ID := NULL;
MyProcedure(
P_REP_CURSOR => P_REP_CURSOR,
P_BILL_ID => P_BILL_ID
:P_REP_CURSOR := P_REP_CURSOR; --<-- Cursor
END;
I understand that in order to make this work I have to supply input variables for;
P_REP_CURSOR := NULL;
P_BILL_ID := NULL;
I can supply a bill id, but how do I
- supply a cursor as input and
- display the output cursor?
Thanks,
ec :-)while a shovel is a fine tool, it is suboptimal when the wrong end (the handle) is used to make the hole.
It appears you are using the wrong end of Oracle.
what problem are you really trying to solve?
because as you recognize your "design" is seriously flawed.
Maybe you are looking for
-
Dunning Letter Report for Portugal
Hi All For Dunning Letter Portugal language rdf file made some changes in layout like removing break for credit and debit transactions and moving address field to right side But fields from Dunning letter setup in applications (first line) is getting
-
Execution error while running rule in ERPI.
Hi, I am facing error while, running the rule from the ERPI (workspace) , when i am checking the session in ODI, i am getting the error where as in ODI there is no such logical schema HFM_TGT. i am doing the integration of EBS rel12 with Planning usi
-
How can I change picture data to image data
I have an array that I am displaying in a picture control as an 16-bit data type. How can I change this into an Image data type with out saving it first?
-
Sequence of execution of events
Can anyone tell me what is the sequence of execution of events i
-
When I originally got itunes, I had it set to automatically sync when I connected my ipod. Now that I have an iphone, I do not want it to auto sync, but I can't find how to change that setting. How can I disable the auto sync in itunes for Windows (V