Fetch MULTIPLE ROWS and STORE in 1 VARIABLE - ORACLE STORED PROCEDURE - oracle

I am working on ORACLE STORED PROCEDURES and I have a doubt.
I have a query which fetches more than 1 row and I want to store all those 3 row's values in 1 Variable.
Can anybody please help me with this.
My QUERY goes like this :
SELECT STUDENT_NAME
FROM STUDENT.STUDENT_DETAILS
WHERE CLASS_ID= 'C';
Here this query fetches 3 names
Jack,
Jill,
Bunny
I want all those 3 names to be stored in 1 variable i.e C_NAMES.
And after that I am using that variable in further steps of my procedure.
Can anyone please help me with this.
I would highly appreciate your time and effort.
Thanks in advance,
Vrinda :)

CREATE PROCEDURE a_proc
AS
CURSOR names_cur IS
SELECT student_name
FROM student.student_details
WHERE class_id = 'C';
names_t names_cur%ROWTYPE;
TYPE names_ntt IS TABLE OF names_t%TYPE; -- must use type
l_names names_ntt;
BEGIN
OPEN names_cur;
FETCH names_cur BULK COLLECT INTO l_names;
CLOSE names_cur;
FOR indx IN 1..l_names.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(l_names(indx).student_name);
END LOOP;
END a_proc;

Depending on your Oracle version(>= 11G(11.2)), you can use LISTAGG:
SELECT LISTAGG(STUDENT_NAME,',') WITHIN GROUP (ORDER BY STUDENT_NAME)
FROM STUDENT.STUDENT_DETAILS
WHERE CLASS_ID= 'C';
EDIT:
If your Oracle version is inferior to 11G(11.2), take a look here

Hi all and Thank you for your time.
I have resolved the question and all thanks to Ederson.
Here is the solution :
SELECT WM_CONCAT(STUDENT_NAME)
FROM STUDENT.STUDENT_DETAILS WHERE CLASS_ID= 'C';
Now if you are using this in a stored procedure or PLSQL you just have to create a variable and use SELECT INTO with it and print the variable.
Here is the code
DECLARE
C_NAMES VARCHAR2(100);
BEGIN
SELECT WM_CONCAT(STUDENT_NAME) INTO C_NAMES
FROM STUDENT.STUDENT_DETAILS WHERE CLASS_ID= 'C';
dbms_output.put_line(sname);
END;
Thanks again for your help people.

You'll need a cursor for that:
DECLARE
CURSOR stud_cur IS
SELECT STUDENT_NAME FROM STUDENT.STUDENT_DETAILS WHERE CLASS_ID= 'C';
l_stud STUDENT.STUDENT_DETAILS%ROWTYPE;
BEGIN
OPEN stud_cur;
LOOP
FETCH stud_cur INTO l_stud;
EXIT WHEN stud_cur%NOTFOUND;
/* The first time, stud_cur.STUDENT_NAME will be Jack, then Jill... */
END LOOP;
CLOSE stud_cur;
END;

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;
/

stored procedure using the IF NOT EXISTS

I am researching issues regarding oracle. I'm creating stored procedures and boot the following errors I also show them in the picture, I hope to help me resolve the error.
[]
You can add variable v_count number :=0; in your procedure to check if value exists.
Example:
CREATE OR REPLACE PROCEDURE PROCEDURE_NAME(PARAMETERS) IS
V_COUNT NUMBER := 0;
BEGIN
SELECT COUNT(1)
INTO V_COUNT
FROM YOUR_TABLE
WHERE .. .
IF V_COUNT = 0 THEN INSERT ...
ELSIF UPDATE ...
COMMIT;
END IF;
END;
Merge is one way to do this. Another way is
INSERT INTO..
SELECT ....
FROM DUAL
WHERE NOT EXISTS (SELECT * FROM...)
I'm not going to try and transcribe your screenshot

Select count from collection

Is there a way to capture the count in a collection table for a certain condition.
For example :
SELECT COUNT(*)
BULK COLLECT INTO v_cnt
FROM tt_product
WHERE region = 'NA';
The above query throws error saying:
Error(649,13): PL/SQL: ORA-00942: table or view does not exist
Any insights would be highly appreciated .
Thanks
Firstly, remove BULK COLLECT, you are getting a single value...
Secondly, if I understand you right, and you are talking about the collection of e.g. type
CREATE OR REPLACE T AS TABLE OF ...
then wrap the name with the table() keyword
SELECT COUNT(*)
INTO v_cnt
FROM table(tt_product)
WHERE region = 'NA';
Dump your results into a #TEMP table first then do your count from there.
Something like:
SELECT * BULK COLLECT INTO #TEMP
FROM tt_product
Where region = 'NA'
SELECT COUNT(*) FROM #TEMP
This is untested so you might have to play with it a little to get it to work.
My example is for SQL Server, you'll have to find the equivalent for ORACLE as I'm not as familiar with it.
declare
b l_number := new l_number(1,2,3,4); -- type L_number is table of number ;
v1 number;
v2 number;
begin
select count(*) into v1 from table(b);
select CARDINALITY(b) into v2 from dual;
dbms_output.put_line(v1);
dbms_output.put_line(v2);
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 use variables in an Oracle PL/SQL where clause

I can't seem to get variables to work in an Oracle PL/SQL where clause. I come from a Microsoft SQL Server background and there it was easy. For example, what would be all steps needed to do something similar to the following?
declare #var int set #var = 1
select * from SomeTable where SomeField = #var
This doesn't seem like it should be hard in PL/SQL, but evidently it is. :-/ I hear I need to use cursors and the like in PL/SQL?
Any help would be greatly appreciated. Thanks.
What do you want to do with the data that the SELECT returns? If you just want to see it you don't need PL/SQL at all, just do this in SQL Plus:
variable var number
exec :var := 1
select * from SomeTable where SomeField = :var;
Or in a tool like SQL Developer or Toad, just do this:
select * from SomeTable where SomeField = :var;
and it will prompt you to enter the value for :var.
The following code declares a variable var to use in the WHERE clause, and a variable result to put the result in then executes it inside a PL/SQL block.
DECLARE
var INT := 1;
result INT;
BEGIN
SELECT 123
INTO result
FROM DUAL
WHERE var = 1;
DBMS_OUTPUT.put_line (var);
DBMS_OUTPUT.put_line (result);
END;
The DBMS_OUTPUT.PUT_LINE calls make it produce this DBMS output:
1
123
declare
type t_rec is record
(
col1 number,
col2 myTable.col2%type
);
v_rec t_rec;
type t_tab is table of v_rec%type index by binary_integer;
v_tab t_tab;
begin
select col1, col2
bulk collect into v_tab
from myTable
where col3 = 'BLAH';
-- do something great with v_tab...
end;
Also know that if you try to select into (or bulk collect into) a variable and no rows are returned, you'll get a no_data_found exception, so you may want to handle that situation.
See more here on pl/sql collections. Above uses an associative array, but there are nested tables and varrays as well. Again, see the link.
Hope that helps.
I use it like this
select * from sec_mainmenu where serno = '&MENU_ID';
When you run it, pl/sql will prompt for MENU_ID value.

Resources