get every result of a query to use after - oracle

I'm trying to get every result of a query to then print an output and make other query with the result. I'm trying to make it with a Cursor. But when I try to print the query result it says me:
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
My code is:
DECLARE
-- Store the SELECT query in a cursor
CURSOR l_cur IS select memname from emuser.DEF_TABLES#controlm t, emuser.DEF_JOB#controlm j where (t.TABLE_ID = j.TABLE_ID) and t.sched_table = 'DWHRAC_DIARIOS2_DC2';
--Create a variable that will hold each result from the cursor
l_cur_rec l_cur%ROWTYPE;
BEGIN
-- Open the Cursor so that we may retrieve results
OPEN l_cur;
LOOP
-- Get a result from the SELECT query and store it in the variable
FETCH l_cur INTO l_cur_rec;
-- EXIT the loop if there are no more results
EXIT WHEN l_cur%NOTFOUND;
-- INSERT INTO another table that has the same structure as your results
DBMS_OUTPUT.PUT_LINE( l_cur_rec);
END LOOP;
-- Close the cursor to release the memory
CLOSE l_cur;
END;
Could you help me please?
Thanks

DBMS_OUTPUT.PUT_LINE accepts a single string as a parameter, but here it is being passed a variable of type l_cur%ROWTYPE which is not allowed. You should change the call to PUT_LINE to specify the name of the column(s) you want to print, such as:
DBMS_OUTPUT.PUT_LINE(l_cur_rec.MEMNAME);
Share and enjoy.

Related

PL SQL Record Operations

Hi I am working on an academic assignment and need some help setting up records:
Write a PL/SQL block to print information about a publisher.
Declare a PL/SQL record based on the structure of the bk_publishers table.
In the declarative section, use the %ROWTYPE attribute and declare the variable publisher_record of type bk_publisher.
In the executable section, get all the information from the bk_publishers table by using publ_id and put it in your record. Display the publ_id and publ_name from the record using a cursor for loop.
Reference Database Chart
So far I have been able to write a block that outputs the contents, but I don't know how to get the contents of the record to print.
Any insights would be very helpful!
Thanks
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
TYPE bk_record IS RECORD
(publ_id bk_publishers.publ_id%TYPE,
publ_name bk_publishers.publ_name%TYPE);
publisher_record bk_publishers%ROWTYPE;
CURSOR bk_cur IS
SELECT * FROM bk_publishers;
BEGIN
OPEN bk_cur;
FETCH bk_cur INTO publisher_record;
CLOSE bk_cur;
FOR publ_no in bk_cur
LOOP
DBMS_OUTPUT.PUT_LINE(publ_no.publ_id || ' ' || publ_no.publ_name);
END LOOP;
END;
/
A simple RECORD variable can hold the contents of a single row, so you have to display column values of individual rows within a loop.
DECLARE
TYPE bk_record IS RECORD ( publ_id bk_publishers.publ_id%TYPE,
publ_name bk_publishers.publ_name%TYPE );
publisher_record bk_publishers%rowtype;
CURSOR bk_cur IS SELECT *
FROM bk_publishers;
BEGIN
OPEN bk_cur;
LOOP
FETCH bk_cur INTO publisher_record;
EXIT WHEN bk_cur%notfound; --Condition to exit the loop.
dbms_output.put_line(publisher_record.publ_id
|| ' ' || publisher_record.publ_name);
END LOOP;
CLOSE bk_cur;
END;
/

PLS-00382: expression is of wrong type in Cursor

DECLARE
A_NAME STUDENTS_1.NAME%TYPE;
B_NAME STUDENTS_1.NAME%TYPE;
C_NAME STUDENTS_1.NAME%TYPE;
GRADE_ST STUDENTS_1.GRADE%TYPE;
CURSOR A IS SELECT NAME FROM STUDENTS_1 WHERE REGEXP_LIKE(GRADE,'(A)','i');
CURSOR B IS SELECT NAME FROM STUDENTS_1 WHERE REGEXP_LIKE(GRADE,'(B)','i');
CURSOR C IS SELECT NAME FROM STUDENTS_1 WHERE REGEXP_LIKE(GRADE,'(C)','i');
BEGIN
GRADE_ST:= &CHOICE;
IF (GRADE_ST='A') THEN
OPEN A;
LOOP
FETCH A INTO A_NAME;
EXIT WHEN A%NOTFOUND;
INSERT INTO SCHOLARSHIP_A VALUES(A_NAME);
END LOOP;
CLOSE A;
ELSIF (GRADE_ST='B') THEN
OPEN B;
LOOP
FETCH B INTO B_NAME;
EXIT WHEN B%NOTFOUND;
INSERT INTO SCHOLARSHIP_B VALUES(B_NAME);
END LOOP;
CLOSE B;
ELSE
OPEN C;
LOOP
FETCH C INTO C_NAME;
EXIT WHEN C%NOTFOUND;
INSERT INTO SCHOLARSHIP_C VALUES(C_NAME);
END LOOP;
CLOSE C;
END IF;
END;
/
I'm making three different scholarship tables with grades A,B,C if i user input values rather than grades i'm getting the solution but i don't want to give values as user input i want user input should be grades
like if i input A i should get all names with grade A from the table
but i'm ending up with this error
SQL*Plus or SQL Developer is substituting the choice variable value before the PL/SQL block is parsed, and as #mathguy suggested that may not ultimately be what you want. But here if you had set verify on you would able to see the code that is actually being compiled, and would see that is causing this error; this part:
BEGIN
GRADE_ST:= &CHOICE;
ends up actually being, if the user supplies the value A:
BEGIN
GRADE_ST:= A;
and in this scope A is the cursor name. So it's an invalid assignment, of a cursor to a string; hence the error you get.
Because your choice is a char/string you need to enclose it in quotes, so you should be doing:
BEGIN
GRADE_ST:= '&CHOICE';
You could greatly simplify what you are doing, and don't actually need PL/SQL, but that is beyond the scope of your question.
You are using a substitution variable (&choice) to pass in the argument. This is a SQL*Plus feature; you will need to pass in the value before your code is compiled, and then it is hard-coded in the block. Rather, what you need is a bind variable (:choice), or write this as a procedure that takes an in parameter.

Multiple line output in pl/sql

I have a PL/SQL file that has a loop structure.
The script is as follows.
SET SERVEROUTPUT ON
declare
c_id employee.id%type;
c_name employee.name%type;
c_address employee.address%type;
CURSOR c_employee is
SELECT id, name, address from employee;
begin
open c_employee;
LOOP
FETCH c_employee into c_id, c_name, c_address;
EXIT when c_employee%notfound;
dbms_output.put_line(c_id||' '||c_name||' '||c_address);
END LOOP;
close c_employee;
end;
/
When I run this from SQLPlus I get only the details of the first row but not the rest. What am I doing wrong? How to get all the outputs for the loop.
Try to convert your code to use a for loop instead of the open statement, like so -
for r_employee in c_employee
LOOP
dbms_output.put_line(r_employee.c_id||' '||r_employee.c_name||' '||r_employee.c_address);
END LOOP;
Where r_employee is a variable of employee%type.
The way you currently wrote it does not iterate through the cursor, and this is why only the first row is presented.
Even though your code looks correct, it should iterate through all the
row not just one. Try to use below snippet and run it in SQL plus if
still single row then there may be some other issue.
SET SERVEROUTPUT ON
DECLARE
BEGIN
FOR I IN
(SELECT id, name, address FROM employee
)
LOOP
dbms_output.put_line(I.ID||' '||I.name||' '||I.address);
END LOOP;
END;
/

Using cursor parameter results in a different execution plan?

For some reason I'm trying to figure out why the following query executes by full table scan which takes ages because the table has ~31M rows
PROCEDURE d1(k_uni_in IN data_par.k_uni%TYPE) AS
CURSOR d1_cur IS
SELECT d.*
FROM data_par d
WHERE d.k_uni = k_uni_in
ORDER BY d.k_date;
BEGIN
FOR i IN d1_cur LOOP
...
END LOOP;
END;
However seemingly similar query runs index range scan and is pretty much instant
PROCEDURE d1(k_uni_in IN data_par.k_uni%TYPE) AS
CURSOR d1_cur(k_cv IN data_par.k_uni%TYPE) IS
SELECT d.*
FROM data_par d
WHERE d.k_uni = k_cv
ORDER BY d.k_date;
BEGIN
FOR i IN d1_cur(k_uni_in) LOOP
...
END LOOP;
END;
Why does that happen? Should I always use cursor parameters instead of using suprogram parameters in cursors?
If your table DATA_PAR has a column named K_UNI_IN, then Oracle is interpreting this line:
WHERE d.k_uni = k_uni_in
As meaning
WHERE d.k_uni = d.k_uni_in
And, since that's obviously not a condition that an index can help with, you're getting a full table scan.
See also: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/nameresolution.htm#LNPLS2038

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.

Resources