Hi
I am new to cursors
I have a table
SQL> select * from commodity_m_details;
COMMODITY_ID COMMODITY_NAME STAR END INTERVAL
-------------------- ------------------------------ ---- ---- ---------------
C1 Commodity1 1996 2006 2
C2 Commodity2 1996 2000 2
C3 Commodity3 1983 1995 4
i need to write a procedure for different years for every commodity
o/p is like
For C3
year
-----------------
1983-1986
1986-1989
1989-1992
1992-1995
i have written like this -
create or replace procedure getYear(commodityid in varchar2,
-
results out resultscur.r,
-
status out varchar2)
-
is
-
V_data number(10);
-
cursor c1 is select start_year,end_year,interval_number from commodity_m_details where commodity_id=commodityid;
-
begin
-
V_data:=0;
-
FOR record in c1
-
LOOP
-
V_data:=V_data+(record.end_year-record.start_year)/record.interval_number;
-
V_data:=record.start_year+V_data;
-
end loop;
-
DBMS_OUTPUT.PUT_LINE(V_data);
-
commit;
-
status:='0';
-
exception
-
when others then
-
status:='7';
-
end getYear;
/
But it is giving only 1986
please help me
Thank you
15 4073
Hi
I am new to cursors
I have a table
SQL> select * from commodity_m_details;
COMMODITY_ID COMMODITY_NAME STAR END INTERVAL
-------------------- ------------------------------ ---- ---- ---------------
C1 Commodity1 1996 2006 2
C2 Commodity2 1996 2000 2
C3 Commodity3 1983 1995 4
i need to write a procedure for different years for every commodity
o/p is like
For C3
year
-----------------
1983-1986
1986-1989
1989-1992
1992-1995
i have written like this
create or replace procedure getYear(commodityid in varchar2,
results out resultscur.r,
status out varchar2)
is
V_data number(10);
cursor c1 is select start_year,end_year,interval_number from commodity_m_details where commodity_id=commodityid;
begin
V_data:=0;
FOR record in c1
LOOP
V_data:=V_data+(record.end_year-record.start_year)/record.interval_number;
V_data:=record.start_year+V_data;
end loop;
DBMS_OUTPUT.PUT_LINE(V_data);
commit;
status:='0';
exception
when others then
status:='7';
end getYear;
/
But it is giving only 1986
please help me
Thank you
Yes for C3 it will print only 1986 because:
check below lines:
V_data := 0;
V_data:=V_data+(record.end_year-record.start_year)/record.interval_number;
V_data:=record.start_year+V_data;
Eg:
V_data:= 0 + (1995 - 1983)/4 -- this will give you 3
V_data:= 1983 + 3 -- this will give you 1986
DBMS_OUTPUT.PUT_LINE(V_data);
In order to get 1986,1989,1992,1995, you need to add 3 to the year 4 times.
Eg:
V_data now = 1986
adding 3 to it will givve V_data:= 1989 then 1992 then 1995 as you require
So For each record from cursor, LOOP through "interval no of times" (ie 4 times here), calculate (end_year - start_year)/interval and store it in a variable V_data
Now do the following: -
-
test_data VARCHAR2(10) := start_year;
-
result VARCHAR2(100);
-
<your cursor LOOP here>
-
V_data:= (end_year - start_year)/interval;
-
For I IN interval LOOP
-
test_data:= test_data + V_data;
-
result:= result||' '||test_data;
-
END LOOP;
-
DBMS_OUTPUT.PUT_LINE(result);
-
V_data:= NULL;
-
result:= NULL;
-
END LOOP;
-
-
Hi, my table structure is changed(interval as increment)
comm_id start_year end_year increment
-------------- --------------- --------------- -----------------
C1 1996 2006 2
C2 2000 2004 1
C3 1990 2010 4
Now i want the o/p for every comm_id like
For c1
1996
1998
2000
2002
2004
2006
For c2
2000
2001
2002
2003
2004
Please give me a query for this
Thank u
Kindly post what you have tried so far.
I tried like this -
declare
-
V_x number(10);
-
V_start number(10);
-
V_end number(10);
-
V_inc number(10);
-
V_data number(10);
-
--V_res number(10);
-
begin
-
select start_year,end_year,increment_value into V_start,V_end,V_inc from commodity_m_details where commodity_id='&commodity_id';
-
DBMS_OUTPUT.PUT_LINE((V_end-V_start)/V_inc);
-
For rec in 1..((V_end-V_start)/V_inc)
-
loop
-
V_data:=0;
-
V_data:=V_data+(V_start+(2*rec-V_inc));
-
end loop;
-
DBMS_OUTPUT.PUT_LINE(V_data);
-
end;
/
I tried like this -
declare
-
V_x number(10);
-
V_start number(10);
-
V_end number(10);
-
V_inc number(10);
-
V_data number(10);
-
--V_res number(10);
-
begin
-
select start_year,end_year,increment_value into V_start,V_end,V_inc from commodity_m_details where commodity_id='&commodity_id';
-
DBMS_OUTPUT.PUT_LINE((V_end-V_start)/V_inc);
-
For rec in 1..((V_end-V_start)/V_inc)
-
loop
-
V_data:=0;
-
V_data:=V_data+(V_start+(2*rec-V_inc));
-
end loop;
-
DBMS_OUTPUT.PUT_LINE(V_data);
-
end;
/
Hint: Instead of using a single SELECT, make use of CURSOR and loop through CURSOR for each commodity_id
i am already tried using cursor
But i am unable to get the required o/p
Plase help me
i am already tried using cursor
But i am unable to get the required o/p
Plase help me
Try this: -
-
DECLARE
-
CURSOR C1 IS SELECT start_year,end_year,interval FROM table_name;
-
test_data VARCHAR2(10);
-
BEGIN
-
FOR I IN C1 LOOP
-
IF(C1%FOUND) THEN
-
test_data:= := I.start_year;
-
V_data:= (I.end_year - I.start_year)/I.INTERVAL;
-
FOR I IN 1..I.INTERVAL LOOP
-
test_data:= test_data + V_data;
-
DBMS_OUTPUT.PUT_LINE(test_data);
-
END LOOP;
-
V_data:= NULL;
-
test_data:= NULL;
-
END LOOP;
-
END IF;
-
END;
-
Ya Finally i got my requirement as
But the problem is i need to declre the out_value as a parameter of the procedure. But the o/p is a cursor.
I am declared the out_value as cursor as show in below procedure..
create or replace procedure getYear1(commodityid in varchar2,
out_val out resultscur.r)
is
begin
<< record_loop >>
for rec in (select start_year, end_year, increment_value from commodity_m_details where commodity_id=commodityid )
loop
out_val := rec.start_year;
<< output_loop >>
loop
--pipe row (out_val); -- or
dbms_output.put_line(out_val);
out_val := out_val + rec.increment_value;
exit when out_val > rec.end_year;
end loop output_loop;
end loop record_loop;
end;
/
but still i am getting the exception as
SQL> show error
Errors for PROCEDURE GETYEAR1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
8/1 PL/SQL: Statement ignored
8/16 PLS-00382: expression is of wrong type
12/1 PLS-00306: wrong number or types of arguments in call to
'PUT_LINE'
12/1 PL/SQL: Statement ignored
13/1 PL/SQL: Statement ignored
13/12 PLS-00306: wrong number or types of arguments in call to '+'
14/1 PL/SQL: Statement ignored
14/19 PLS-00306: wrong number or types of arguments in call to '>'
Please tell me how to declare the out_val as a procedure parameter
(Bcoz by taking this o/p parameter only in front end they are displaying the o/p)
Please help me on this
thank u
Use ref cursor as the OUT parameter.
ofcourse,
out_val out resultscur.r this is of type refcursor only
And also i changed like
create or replace procedure getYear1(commodityid in varchar2,
out_val out refcursor)
is
begin
<< record_loop >>
for rec in (select start_year, end_year, increment_value from commodity_m_details where commodity_id=commodityid )
loop
out_val := rec.start_year;
<< output_loop >>
loop
--pipe row (out_val); -- or
dbms_output.put_line(out_val);
out_val := out_val + rec.increment_value;
exit when out_val > rec.end_year;
end loop output_loop;
end loop record_loop;
end;
/
Then it is giving exception as
SQL> show error
Errors for PROCEDURE GETYEAR1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 PL/SQL: Compilation unit analysis terminated
2/13 PLS-00201: identifier 'REFCURSOR' must be declared
Please help me
ofcourse,
out_val out resultscur.r this is of type refcursor only
And also i changed like
create or replace procedure getYear1(commodityid in varchar2,
out_val out refcursor)
is
begin
<< record_loop >>
for rec in (select start_year, end_year, increment_value from commodity_m_details where commodity_id=commodityid )
loop
out_val := rec.start_year;
<< output_loop >>
loop
--pipe row (out_val); -- or
dbms_output.put_line(out_val);
out_val := out_val + rec.increment_value;
exit when out_val > rec.end_year;
end loop output_loop;
end loop record_loop;
end;
/
Then it is giving exception as
SQL> show error
Errors for PROCEDURE GETYEAR1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 PL/SQL: Compilation unit analysis terminated
2/13 PLS-00201: identifier 'REFCURSOR' must be declared
Please help me
say SYS_REFCURSOR and not refcursor
Thats not the matter.
Becaue before that i wrote a package as below
create or replace package resultscur
is
TYPE r IS REF CURSOR;
end resultscur;
/
Any way i tried like what u suggested,
create or replace procedure getYear1(commodityid in varchar2,
out_val out sys_refcursor)
is
begin
<< record_loop >>
for rec in (select start_year, end_year, increment_value from commodity_m_details where commodity_id=commodityid )
loop
out_val := rec.start_year;
<< output_loop >>
loop
--pipe row (out_val); -- or
dbms_output.put_line(out_val);
out_val := out_val + rec.increment_value;
exit when out_val > rec.end_year;
end loop output_loop;
end loop record_loop;
end;
/
but its also not working
Thats not the matter.
Becaue before that i wrote a package as below
create or replace package resultscur
is
TYPE r IS REF CURSOR;
end resultscur;
/
Any way i tried like what u suggested,
create or replace procedure getYear1(commodityid in varchar2,
out_val out sys_refcursor)
is
begin
<< record_loop >>
for rec in (select start_year, end_year, increment_value from commodity_m_details where commodity_id=commodityid )
loop
out_val := rec.start_year;
<< output_loop >>
loop
--pipe row (out_val); -- or
dbms_output.put_line(out_val);
out_val := out_val + rec.increment_value;
exit when out_val > rec.end_year;
end loop output_loop;
end loop record_loop;
end;
/
but its also not working
Code is erroring out or giving wrong results?
If erroring out, please post the error message else post the output that you are getting?
SQL> show error
Errors for PROCEDURE GETYEAR1:
LINE/COL ERROR
-------- -----------------------------------------------------------------
8/1 PL/SQL: Statement ignored
8/16 PLS-00382: expression is of wrong type
12/1 PLS-00306: wrong number or types of arguments in call to
'PUT_LINE'
12/1 PL/SQL: Statement ignored
13/1 PL/SQL: Statement ignored
13/12 PLS-00306: wrong number or types of arguments in call to '+'
14/1 PL/SQL: Statement ignored
14/19 PLS-00306: wrong number or types of arguments in call to '>'
create or replace procedure getYear1(commodityid in varchar2,
out_val out sys_refcursor)
is
begin
<< record_loop >>
for rec in (select start_year, end_year, increment_value from commodity_m_details where commodity_id=commodityid )
loop
out_val := rec.start_year;
<< output_loop >>
loop
--pipe row (out_val); -- or
dbms_output.put_line(out_val);
out_val := out_val + rec.increment_value;
exit when out_val > rec.end_year;
end loop output_loop;
end loop record_loop;
end;
/
The use of out_value is incorrect in your code.
We cannot use a cursor object in this way.
Why do you want to return a CURSOR here.
You dont need any value to be returned since the procedure itself prints the value using DBMS_OUTPUT package.
Instaed of using out_value in all your manipulations, make use of simple integer variable.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ed |
last post by:
Hello,
I posted a question about looping with Select in a While
loop, a few days ago. Repliers to my post advised me that
a Cursor would be much better (thanks all for your
replies). I found...
|
by: Dmitry Duginov |
last post by:
Here's a problem
--------------------------------------------
set serveroutput on
DECLARE
CURSOR cur is select ed_no, length(ed_no) as len from
listtext@pubdb.world; BEGIN
for cur_rec in cur...
|
by: Anthony Robinson |
last post by:
I have a stored procedure that needs to loop through a record set,
evaluate value, then either perform an operation or move tro the next
record - depending on some criteria. Here's the procedure:...
|
by: Søren Larsen |
last post by:
In a stored procedure (SP1) I am looping through a cursor with records
from Table1. Each record in the cursor is inserted into Table2.
Insert trigger on Table2 is inserting the record into Table3...
|
by: traceable1 |
last post by:
SQL Server 2000 SP4 with AWE hotfix. Windows 2003 SP1.
I have a stored procedure which is not working the way I think it
should be.
I have a CURSOR which has a variable in the WHERE clause:
...
|
by: debasisdas |
last post by:
SAMPLE CODE USING RECORD
===========================
declare
cursor c1 is select * from dept;
type drec is record (a dept.deptno%type,
b dept.dname%type,
c dept.loc%type);
type ttype is...
|
by: debasisdas |
last post by:
RESTRICTIONS ON CURSOR VARIABLES
=================================
Currently, cursor variables are subject to the following restrictions:
Cannot declare cursor variables in a package spec.
...
|
by: BilalGhazi |
last post by:
Hi All,
I have this strange problem. I am user of two different database (both are same version 9i). I created a procedure and within this procedure i used a cursor to select the values, this is...
|
by: debasisdas |
last post by:
This thread contains some useful tips/samples regarding cursors, that the forum members may find useful.
Cursor
===========
Implicit Cursor--sql Returns Single Line. Created By Oracle Server....
|
by: debasisdas |
last post by:
Sample example to show FOR UPDATE CURSOR
-----------------------------------------------------------------------------
DECLARE
CURSOR EMPREC IS SELECT * FROM EMP FOR UPDATE OF SAL;
MYREC...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |