Get two records from PLSQL cursor - plsql

My question is short. I create a cursor to get some values from my table. I want to get the current record of cursor (the fetched record) and the next record from the cursor without fetching it, because I want to make a calculation over the current record and the next record. In traditional programming it's a simple operation; you can do it with a for index by increasing it by 1.
Do you have any suggestions?

I want to make a calculation over the
current record and the next record.
Presuming you are using Oracle, this can be done quite simply in SQL using analtyical functions, specifically the lead() function. This retrieves a column's value from the next nth record (default n=1).
SQL> select empno
2 , sal as this_sal
3 , lead(sal) over (order by empno) as next_sal
4 from emp
5 order by empno
6 /
EMPNO THIS_SAL NEXT_SAL
---------- ---------- ----------
7369 800 1600
7499 1600 1250
7521 1250 2975
7566 2975 1250
7654 1250 2850
7698 2850 2450
....
This query can be using in a cursor or any other mechanism for retrieving records.

I'm not sure you can do that without moving the cursor, but you should be able to accomplish the same goal like this (psuedocode):
open cursor;
fetch cursor to rec1;
if cursor%found then
loop
fetch cursor to rec2;
exit when cursor%notfound;
perform calculations with rec1 and rec2;
rec1 := rec2;
end loop;
end if;

To answer you specific question, you could use BULK COLLECT and its limit clause into a collection (table) variable.
DECLARE
CURSOR my_cursor IS
[YOUR SQL HERE];
TYPE t_my_type IS TABLE OF my_cursor%ROWTYPE INDEX BY BINARY_INTEGER;
v_my_var t_my_type;
BEGIN
FETCH my_cursor BULK COLLECT INTO v_my_var LIMIT 2;
dbms_output.put_line('My first value: ' || v_my_var(1).some_column);
dbms_output.put_line('My second value: ' || v_my_var(2).some_column);
END;
The limit clause will cause only the first two records to be fetched and stored. The first record will have an index of 1 and the second will be 2.

You can compare two variables by saving one into local and then compare it with cursor value for example
declare
l_local1 table_name%type := null;
begin
for c1 in (select
*
from
table_name
where some clause)
loop
-- do comparation
if l_local1 is not null and c1.field_value is not null then
-- do something to compare
.
.
.
-- empty local1 variable
l_local1 := null;
end if;
-- get the value of loop in local variable
l_local1 := c1.field_value;
end loop;
end;

Related

PLSQL Return Sequence.currval When Sequence Name Is Built Using Substitution Strings

I would like to use the same application in different instances so I need to specify the workspace and sequence ID.
Example query
BEGIN
INSERT INTO STEP (STEP_CHART_TITLE)
VALUES ('Action', 'Action');
RETURN '"'||:v_workspace||'"."'||:v_seqid||'".currval';
END;
If I use:
"FREEADMIN"."ISEQ$$_111997".currval;
in the return statement it works fine.
If I use the substitution strings, it will build the string correctly, but won't return the sequence number.
Is there a way to get the sequence number?
Thanks
"PL/SQL" as Oracle's procedural extension to SQL? Asking because I don't quite understand what "workspace" represents (we call it a "user" or a "schema" or even "owner" in Oracle).
If so, then you can't fetch current value unless you first fetch next sequence value (you didn't post whole code you use so I'm not sure whether you did that or not; also, insert statement is wrong - you're inserting 2 values into a single column).
SQL> select seq.currval from dual;
select seq.currval from dual
*
ERROR at line 1:
ORA-08002: sequence SEQ.CURRVAL is not yet defined in this session
SQL> select seq.nextval from dual;
NEXTVAL
----------
6
SQL> select seq.currval from dual;
CURRVAL
----------
6
SQL>
Therefore, the whole code might look like this - you'd use dynamic SQL. As this is SQL*Plus, I'm using substitution variables; you'd use bind variables (i.e. :v_workspace instead of '&v_workspace') (if that's what you're doing). This code just displays the value - you'd return it.
SQL> create table step (id number, step_chart_title varchar2(10));
Table created.
SQL> declare
2 l_str varchar2(200);
3 l_id number;
4 begin
5 l_str := 'select ' || '&v_workspace' ||'.'|| '&v_seqid' ||'.nextval from dual';
6 execute immediate l_str into l_id;
7
8 insert into step (id, step_chart_title) values (l_id, 'Action');
9
10 dbms_output.put_line(l_id); --> L_ID now contains CURRVAL
11 end;
12 /
Enter value for v_workspace: scott
Enter value for v_seqid: seq
8 --> here it is
PL/SQL procedure successfully completed.
SQL>

Using two cursors in PL/SQL

DECLARE
AA NUMBER;
CURSOR S IS SELECT ENAME, SAL
FROM EMP;
CURSOR D IS SELECT ENAME, SAL
FROM EMP;
NAME EMP.ENAME%TYPE;
SALARY EMP.SAL%TYPE;
C NUMBER;
BEGIN
AA := :NUMBER_OF_EMP;
SELECT COUNT(EMPNO) INTO C FROM EMP;
OPEN S;
FOR A IN 1..AA LOOP
FETCH S INTO NAME, SALARY;
DBMS_OUTPUT.PUT_LINE('NAME : '||NAME||' SALARY :'||SALARY);
END LOOP;
CLOSE S;
DBMS_OUTPUT.PUT_LINE(' ');
OPEN D;
FOR A IN 1..AA LOOP
FETCH S INTO NAME, SALARY;
UPDATE EMP
SET SAL = SAL + 500;
DBMS_OUTPUT.PUT_LINE('NAME : '||NAME||' SALARY :'||SALARY);
END LOOP;
CLOSE D;
END;
i'm little confusing with cursor. in my code i wana display a name and salary of employees. For example, if the user put 3 in the first cursor will display just 3 employees with ( name and salary). the second cursor will do the same thing but it will change the salary for the same 3 employees and display it again.
ORA-01001: invalid cursor
The immediately cause of your error is the second FETCH statement. It fetches from cursor S but you closed that cursor above. Changing that to fetch from cursor D will correct the error condition.
But, your procedure has other problems.
The "select count(empno)..." is superfluous as you do nothing with the result.
The cursors S and D are the same and not open at the same time, so only one of them is necessary. It is fine to close then reopen the same cursor.
Whether you use 2 cursors or reuse just 1 there there is no guarantee the same rows are returned.
Your update will not do what you indicated you want. It will update the sal column of every row in the table the number of the loop is performed (in this case the number in AA). So if AA were 3 as for your example each row will have sal updated by 1500. This is because there is no where clause on the update. Therefore Oracle updates every row.

i want to generate a patient details report based on the input (input should be operators like '>','<','=') given by the user

//Program to generate a patient details report based on the input given by the //user
CREATE or REPLACE procedure XX_TEAM18_PROc2
(Admit_date in date,
Bill_amt in number,
action in varchar2)
is
cursor c
is
SELECT P.Patient_name,ph.Symptom_issue Symptom,ph.Consulted_dr Doctor,ph.Diagnosis,ph.Bill_Amount
FROM
XX_Patient_Master_Team18 P,XX_Patient_History_Team18 ph
WHERE
p.Patient_Id=ph.Patient_id
AND
bill_amount action bill_amount;
rec c%rowtype;
cursor c1
is
SELECT P.Patient_name,ph.Symptom_issue Symptom,ph.Consulted_dr Doctor,ph.Diagnosis,ph.Admitted_date Admitted_date
FROM
XX_Patient_Master_Team18 P,XX_Patient_History_Team18 ph
WHERE
p.Patient_Id=ph.Patient_id
AND
Admitted_date action admit_date ;
rec1 c1%rowtype;
begin
open c;
dbms_output.put_line(' records belongs to '||Bill_Amt);
fetch c into rec ;
loop
exit when c%notfound;
dbms_output.put_line(rec.Symptom||' '||rec. Doctor||' '||rec.Diagnosis||' '||rec.bill_Amount);
end loop;
close c;
open c1;
dbms_output.put_line(' records belongs to given '||Admit_date);
fetch c1 into rec1 ;
loop
exit when c1%notfound;
dbms_output.put_line(rec1.Symptom||' '||rec1.Doctor||' '||rec1.Diagnosis||' '||rec1.Admitted_date);
end loop;
close c1;
end ;
You must use dynamic SQL as parameter action cannot be used in a fixed SQL statement within PL/SQL. Parameters can only be used as substitution values, not as column names or operators unless you are using dynamic SQL.
You will need to create a varchar2 variable to hold the SQL statement, then use the DBMS_SQL package to create the cursor. DBMS_SQL has pretty good documentation within it, don't forget to close the cursor when you are done using it.

FOR UPDATE CURSOR in teradata?

This is sql block i'm using in Oracle,
Now I need to do the same way in Teradata, Is possible? I want the syntax FOR UPDATE CURSOR in Teradata!
Can you please guide me?
declare
cursor c1 is select * from Employees FOR UPDATE;
a number :=0 ;
begin
for x in c1 loop
a := a +1 ;
update employees set salary = a where current of c1;
end loop;
end;
Updateable cursors are allowed in ANSI-mode sessions only.
The syntax is quite similar:
declare c1 cursor for
select * from Employees FOR UPDATE;
a number :=0 ;
begin
for x in c1 loop
a := a +1 ;
update employees set salary = a where current of c1;
end loop;
end;
But cursors perform really bad in a parallel DBMS like Teradata as they're processed serially, one row after the other.
In almost every case cursors on data can be rewritten set-based (e.g. your example is a simple ROW_NUMBER) and then they perform several orders of magnitude faster.
Maybe you could use this instead?
UPDATE employees
FROM (
SELECT csum(1,1) new_salary, emp_id
FROM employees
) src
set salary=src.new_salary
where employees.emp_id=src.emp_id;

PL/SQL Blocks - seeing output simply ? [A Very simple question I'm sure !]

I'm sure what I want is very simple but I cannot figure out how.
I want :
To declare some variables and initialize them to certain values
To excecute a number of selects (predicated by the above variable values) and see the results as if i had executed the results straight on the sqlplus command line
I believe it's necessary to use the block structure in order that I may declare and make use of variables within the predicates of the queries. Although the examples shown here are quite simple in the real case there are numberous, much more complex SELECT's.
I tried doing this (forgetting about predicates for a moment) ...
DECLARE
EMP_EMPLOYEE_ID_IN VARCHAR2(12);
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM DEPT WHERE DEPNO';
END;
/
... but when I do that I get to execute the select without seeing the output.
I've also tried this ...
DECLARE
EMP_EMPLOYEE_ID_IN VARCHAR2(12);
BEGIN
SELECT * FROM DEPT;
END;
/
... but then I get ...
PLS-00428: an INTO clause is expected in this SELECT statement
... I really don't want to have to declare a variable for every column which would appear in my output.
Can anyone tell me how I can execute the SELECTs but simply and easily see the output as if I were on the sqlplus command line, ie to see the same output as if I did this
SQL> SELECT * FROM DEPT;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Thanks
I have now tested the answer given by Shannon Severance below and found that it will do what I want.
For the sake of later readers I thought it might be useful to show the complete script here.
set line 32000;
set trimspool on;
var V_CURSOR1 REFCURSOR;
var V_CURSOR2 REFCURSOR;
var V_CURSOR3 REFCURSOR;
DECLARE
DEPT_NUM_IN VARCHAR2(12);
BEGIN
DEPT_NUM_IN := '10';
OPEN :V_CURSOR1 FOR SELECT * FROM DEPT;
OPEN :V_CURSOR2 FOR SELECT * FROM DEPT ORDER BY LOC;
OPEN :V_CURSOR3 FOR SELECT * FROM DEPT WHERE DEPTNO = DEPT_NUM_IN ORDER BY LOC;
END;
/
print V_CURSOR1
print V_CURSOR2
print V_CURSOR3
From sqlplus, other tools may be different.
First declare a sqlplus refcursor variable
SQL> var l_cursor refcursor
Then open that cursor within a PL/SQL block, where you will have access to declared variables and everything:
SQL> edit
Wrote file afiedt.buf
1 declare
2 l_number number;
3 begin
4 open :l_cursor for select table_name from all_tables where rownum < 10;
5* end;
SQL> /
PL/SQL procedure successfully completed.
Notice above that the refcursor variable is prepended with a :, this is because we are binding a sqlplus variable into the PL/SQL anonymous block
Next, use the SQLPLUS print command:
SQL> print l_cursor
TABLE_NAME
------------------------------
ICOL$
CON$
UNDO$
PROXY_ROLE_DATA$
FILE$
UET$
IND$
SEG$
COL$
9 rows selected.
DECLARE
v_result VARCHAR2(400);
BEGIN
SELECT dummy
INTO v_result
FROM dual;
dbms_output.put_line(v_result);
END;

Resources