Oracle Stored Procedures using temp tables - oracle

I am fairly new to oracle and need to solve the following problem.
inside a stored proc I want to return 2 cursors like this.
PROCEDURE MyProcA()
as
begin
open refcursorA FOR
select id, ..... FROM tableA where ..... long series of conditions
open refcursorB FOR
select * FROM table b where b.id IN (select id FROM tableA where ..... long series of conditions)
This is how I have the stored proc at the moment but I don't like the repetition. The where clause SQL in the parentheses in the 2nd cursor is exactly the same the first cursor. How can I load it into a temp table or associative array or something to use in both cursors.
Thanks in advance

It seems like you need REF Cursor
How to declare ?
cursor cursor_name is ref cursor;
How to use it ?
open cursor_name for your select query
loop
fetch cursor_name into some_variable;
exit when cursor_name%notfound;
do_something;
end loop;
close cursor_name;
So, same cursor variable will point to different sql area dynamically.
OR
You can see Cursor Expression

Related

Getting the corresponding record of a Cursor/Select when in a Cursor LOOP statement

The question seems easy. I have built a package, where there is a quite massive cursor, let's say on all invoices of my company for the whole year.
CURSOR c_invoices(p_year IN INTEGER) IS
SELECT all_invoices.invoicenumber,
all_invoices.invoicedate,
all_invoices.customernumber
FROM all_invoices
WHERE all_invoices.year = p_year
;
After opening it and using a LOOP statement, I want to get some data from another table (forbidden_customers), but only if the customer is in this very last table.
What I'd like to do, is to open another cursor (or a SELECT ?) at the very beginning of my package, browsing the whole table(forbidden_customers), and then getting to the corresponding record when in my invoices LOOP.
So, something like :
CURSOR c_forbidden_customers IS
SELECT forbidden_customers.customernumber,
forbidden_customers.customeradress
FROM forbidden_customers
;
And then :
OPEN c_invoices(v_year);
LOOP FETCH c_invoices INTO invoices_cursor;
BEGIN
EXIT WHEN c_invoices%NOTFOUND;
*IF invoices_cursor.customernumber IS FOUND IN c_forbidden_customers ...
THEN ...*
This is what I do meanwhile (I know it is bad):
SELECT COUNT(*)
INTO v_exist /*INTEGER*/
FROM forbidden_customers
WHERE forbidden_customers.customernumber= p_customernumber
IF v_exist <> 0
THEN...
I tried to make it as clear as possible. Thank you for your time
Don't do it twice; join both tables in the same cursor and use it. Also, if you switch to a cursor FOR loop, you'll save yourself from some typing as Oracle will do most of boring stuff for you (declaring cursor variable, opening the cursor, closing it, exiting the loop ...):
create or replace procedure p_test (p_year in integer) is
begin
for c_invoices in
(select a.invoicenumber,
a.invoicedate,
a.customernumber,
c.customeraddress
from all_invoices a join forbidden_customers c on c.customernumber = a.customernumber
where a.year = p_year)
loop
-- do something
end loop;
end;
If the table forbidden_customers is not large and it will fit oracle's session memory, you can use a pl/sql table to store all id's from forbidden_customers and check it later. The check is done in memory only, so it is much faster than any regular select.
create table all_invoices
(id number,
year number,
customer_number number);
create table forbidden_customers
(customer_number number);
CREATE OR REPLACE TYPE t_number_table IS TABLE OF number
/
CREATE OR REPLACE PROCEDURE test23
IS
forbidden_customers_list t_number_table;
CURSOR c_invoices (p_year IN INTEGER)
IS
SELECT all_invoices.customer_number
FROM all_invoices
WHERE all_invoices.year = p_year;
BEGIN
SELECT customer_number
BULK COLLECT INTO forbidden_customers_list
FROM forbidden_customers;
FOR rec_invoices in c_invoices(2022) loop
if forbidden_customers_list.exists(rec_invoices.customer_number) then
null;
end if;
end loop;
end;
/

Can an Oracle procedure's resultset output be used in a select statement?

Basically, I'd like to write a procedure that contains a complex query whose output will be used in several places in the application. So I'd like to keep just one copy of the complex query inside a procedure if possible. If not, what would be an alternative solution. Is something like the following possible in Oracle PL/SQL? Thanks a lot.
TYPE retCur IS REF CURSOR;
PROCEDURE get_rows (
c_OutCursor OUT retCur
);
BEGIN
OPEN c_OutCursor FOR
SELECT * FROM some_table;
END get_rows;
PROCEDURE use_rows
BEGIN
SELECT a.some_columns, b.some_columns
FROM tableA a, get_rows b
WHERE a.id = b.id;
END;

Select Statement in oracle procedure

I can't able to create a select statement in oracle procedure. Please help me to create this.
Now I create the insert,update.delete statement in a procedure but i can't create a select statement. Please help me to create the select statement using cursor.
c_dbuser OUT SYS_REFCURSOR
ELSIF (TYPE_ =1) THEN
OPEN c_dbuser FOR
SELECT * FROM tbl_discount_master ;
CLOSE c_dbuser;
END IF;
call procedure_name(xx,xx,xx,1);
how can i get the selected value using call procedure statement.
In addition to the other suggestion, you have this solution when you are getting exactly one row.
DECLARE
myvar1 mytable.mycolumn1%TYPE;
myvar2 mytable.mycolumn2%TYPE;
BEGIN
SELECT mycolumn1, mycolumn2
INTO myvar1, myvar2
FROM mytable
WHERE …;
END;
This will throw an exception if there is no selected row (NO_DATA_FOUND) or if there is more than one row (TOO_MANY_ROWS).
The difference between select and the insert/update/delete is that you need to select into some structure, either one or more variables or a rowtype variable.
Avoid explicit cursors whenever possible in favour of the faster, less verbose and less error prone implicit cursor.
eg.
for cur_my_query in
select column1,
column2,
...
from ...
where ...
loop
refer here to cur_my_query or my_query.column1 etc
end loop

How to get next and previous record from cursor?

I have a table named testtransaction which stores pervQuestionId and NextQuestionId...
How to insert records in this table through cursor?
there is something cursoe.getnext()...how do i implement it?
My code is shown below:
create or replace function store_data(disciplineid in char,
NoOfQuestions in number)
is
cur_out sys_refcursor;
begin
open cur_out for
select getguid() tmp,
QuestionNo,Disciplineid
from tbliffcoQuestionmaster
where (DisciplineId=discipline1 AND rownum <= disc1_NoOfQuestions)
order by tmp ;
///now it should insert records.
end;
I don't want to completely write the answer since it's homework and you're supposed to be doing the work. One of the basic formats of a cursor loop is:
LOOP
FETCH cursor INTO x;
EXIT WHEN cursor%NOTFOUND;
--do something
END LOOP;
Maybe that will get you on the right track. Googling for "Oracle cursor" should get you dozens of examples of how cursors are used.

What is the difference between explicit and implicit cursors in Oracle?

I am a bit rusty on my cursor lingo in PL/SQL. Anyone know this?
An implicit cursor is one created "automatically" for you by Oracle when you execute a query. It is simpler to code, but suffers from
inefficiency (the ANSI standard specifies that it must fetch twice to check if there is more than one record)
vulnerability to data errors (if you ever get two rows, it raises a TOO_MANY_ROWS exception)
Example
SELECT col INTO var FROM table WHERE something;
An explicit cursor is one you create yourself. It takes more code, but gives more control - for example, you can just open-fetch-close if you only want the first record and don't care if there are others.
Example
DECLARE
CURSOR cur IS SELECT col FROM table WHERE something;
BEGIN
OPEN cur;
FETCH cur INTO var;
CLOSE cur;
END;
An explicit cursor is defined as such in a declaration block:
DECLARE
CURSOR cur IS
SELECT columns FROM table WHERE condition;
BEGIN
...
an implicit cursor is implented directly in a code block:
...
BEGIN
SELECT columns INTO variables FROM table where condition;
END;
...
An explicit cursor is one you declare, like:
CURSOR my_cursor IS
SELECT table_name FROM USER_TABLES
An implicit cursor is one created to support any in-line SQL you write (either static or dynamic).
In answer to the first question. Straight from the Oracle documentation
A cursor is a pointer to a private SQL
area that stores information about
processing a specific SELECT or DML
statement.
1.CURSOR: When PLSQL issues sql statements it creates private work area
to parse & execute the sql statement is called cursor.
2.IMPLICIT: When any PL/SQLexecutable block issues sql statement.
PL/SQL creates implicit cursor and manages automatically means
implcit open & close takes place. It used when sql statement return
only one row.It has 4 attributes SQL%ROWCOUNT, SQL%FOUND,
SQL%NOTFOUND, SQL%ISOPEN.
3.EXPLICIT: It is created & managed by the programmer. It needs every
time explicit open,fetch & close. It is used when sql statement
returns more than one row. It has also 4 attributes
CUR_NAME%ROWCOUNT, CUR_NAME%FOUND, CUR_NAME%NOTFOUND,
CUR_NAME%ISOPEN. It process several rows by using loop.
The programmer can pass the parameter too to explicit cursor.
Example: Explicit Cursor
declare
cursor emp_cursor
is
select id,name,salary,dept_id
from employees;
v_id employees.id%type;
v_name employees.name%type;
v_salary employees.salary%type;
v_dept_id employees.dept_id%type;
begin
open emp_cursor;
loop
fetch emp_cursor into v_id,v_name,v_salary,v_dept_id;
exit when emp_cursor%notfound;
dbms_output.put_line(v_id||', '||v_name||', '||v_salary||','||v_dept_id);
end loop;
close emp_cursor;
end;
Implicit cursors require anonymous buffer memory.
Explicit cursors can be executed again and again by using their name.They are stored in user defined memory space rather than being stored in an anonymous buffer memory and hence can be easily accessed afterwards.
These days implicit cursors are more efficient than explicit cursors.
http://www.oracle.com/technology/oramag/oracle/04-sep/o54plsql.html
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1205168148688
From a performance point of view, Implicit cursors are faster.
Let's compare the performance between an explicit and implicit cursor:
SQL> DECLARE
2 l_loops NUMBER := 100000;
3 l_dummy dual.dummy%TYPE;
4 l_start NUMBER;
5 -- explicit cursor declaration
6 CURSOR c_dual IS
7 SELECT dummy
8 FROM dual;
9 BEGIN
10 l_start := DBMS_UTILITY.get_time;
11 -- explicitly open, fetch and close the cursor
12 FOR i IN 1 .. l_loops LOOP
13 OPEN c_dual;
14 FETCH c_dual
15 INTO l_dummy;
16 CLOSE c_dual;
17 END LOOP;
18
19 DBMS_OUTPUT.put_line('Explicit: ' ||
20 (DBMS_UTILITY.get_time - l_start) || ' hsecs');
21
22 l_start := DBMS_UTILITY.get_time;
23 -- implicit cursor for loop
24 FOR i IN 1 .. l_loops LOOP
25 SELECT dummy
26 INTO l_dummy
27 FROM dual;
28 END LOOP;
29
30 DBMS_OUTPUT.put_line('Implicit: ' ||
31 (DBMS_UTILITY.get_time - l_start) || ' hsecs');
32 END;
33 /
Explicit: 332 hsecs
Implicit: 176 hsecs
PL/SQL procedure successfully completed.
So, a significant difference is clearly visible. Implicit cursor is much faster than an explicit cursor.
More examples here.
With explicit cursors, you have complete control over how to access information in the database. You decide when to OPEN the cursor, when to FETCH records from the cursor (and therefore from the table or tables in the SELECT statement of the cursor) how many records to fetch, and when to CLOSE the cursor. Information about the current state of your cursor is available through examination of the cursor attributes.
See http://www.unix.com.ua/orelly/oracle/prog2/ch06_03.htm for details.
Google is your friend: http://docstore.mik.ua/orelly/oracle/prog2/ch06_03.htm
PL/SQL issues an implicit cursor
whenever you execute a SQL statement
directly in your code, as long as that
code does not employ an explicit
cursor. It is called an "implicit"
cursor because you, the developer, do
not explicitly declare a cursor for
the SQL statement.
An explicit cursor is a SELECT
statement that is explicitly defined
in the declaration section of your
code and, in the process, assigned a
name. There is no such thing as an
explicit cursor for UPDATE, DELETE,
and INSERT statements.
A cursor is a SELECTed window on an Oracle table, this means a group of records present in an Oracle table, and satisfying certain conditions. A cursor can SELECT all the content of a table, too. With a cursor you can manipulate Oracle columns, aliasing them in the result. An example of implicit cursor is the following:
BEGIN
DECLARE
CURSOR C1
IS
SELECT DROPPED_CALLS FROM ALARM_UMTS;
C1_REC C1%ROWTYPE;
BEGIN
FOR C1_REC IN C1
LOOP
DBMS_OUTPUT.PUT_LINE ('DROPPED CALLS: ' || C1_REC.DROPPED_CALLS);
END LOOP;
END;
END;
/
With FOR ... LOOP... END LOOP you open and close the cursor authomatically, when the records of the cursor have been all analyzed.
An example of explicit cursor is the following:
BEGIN
DECLARE
CURSOR C1
IS
SELECT DROPPED_CALLS FROM ALARM_UMTS;
C1_REC C1%ROWTYPE;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO c1_rec;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE ('DROPPED CALLS: ' || C1_REC.DROPPED_CALLS);
END LOOP;
CLOSE c1;
END;
END;
/
In the explicit cursor you open and close the cursor in an explicit way, checking the presence of records and stating an exit condition.
Implicit cursor returns only one record and are called automatically. However, explicit cursors are called manually and can return more than one record.
As stated in other answers, implicit cursors are easier to use and less error-prone.
And Implicit vs. Explicit Cursors in Oracle PL/SQL shows that implicit cursors are up to two times faster than explicit ones too.
It's strange that no one had yet mentioned Implicit FOR LOOP Cursor:
begin
for cur in (
select t.id from parent_trx pt inner join trx t on pt.nested_id = t.id
where t.started_at > sysdate - 31 and t.finished_at is null and t.extended_code is null
)
loop
update trx set finished_at=sysdate, extended_code = -1 where id = cur.id;
update parent_trx set result_code = -1 where nested_id = cur.id;
end loop cur;
end;
Another example on SO: PL/SQL FOR LOOP IMPLICIT CURSOR.
It's way more shorter than explicit form.
This also provides a nice workaround for updating multiple tables from CTE.
In PL/SQL, A cursor is a pointer to this context area. It contains all the information needed for processing the statement.
Implicit Cursors:
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in it.
Explicit Cursors:
Explicit cursors are programmer-defined cursors for gaining more control over the context area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement which returns more than one row.
The syntax for creating an explicit cursor is:
CURSOR cursor_name IS select_statement;
Every SQL statement executed by the Oracle database has a cursor associated with it, which is a private work area to store processing information. Implicit cursors are implicitly created by the Oracle server for all DML and SELECT statements.
You can declare and use Explicit cursors to name the private work area, and access its stored information in your program block.
Explicit...
cursor foo is select * from blah;
begin
open fetch exit when close cursor yada yada yada
don't use them, use implicit
cursor foo is select * from blah;
for n in foo loop
x = n.some_column
end loop
I think you can even do this
for n in (select * from blah) loop...
Stick to implicit, they close themselves, they are more readable, they make life easy.

Resources