Working with nested loops in pl/sql but not displaying the proper output - database

SET SERVEROUTPUT ON SIZE 4000;
DECLARE
call_id COURSE.CALL_ID%type;
sec_num COURSE_SECTION.SEC_NUM%type;
fname STUDENT.S_FIRST%TYPE ;
lname STUDENT.S_LAST%TYPE;
CURSOR c_info is
SELECT CALL_ID , SEC_NUM
FROM COURSE_SECTION ,COURSE,TERM
WHERE COURSE_SECTION.COURSE_ID = COURSE.COURSE_ID
AND TERM.TERM_ID = COURSE_SECTION.TERM_ID
AND TERM.TERM_DESC = 'Summer 2007' ;
CURSOR S_NAME IS
SELECT DISTINCT S_FIRST, S_LAST
FROM STUDENT,COURSE_SECTION,TERM,ENROLLMENT
WHERE TERM.TERM_ID = COURSE_SECTION.TERM_ID
AND COURSE_SECTION.C_SEC_ID = ENROLLMENT.C_SEC_ID
AND COURSE_SECTION.TERM_ID=TERM.TERM_ID
AND ENROLLMENT.S_ID = STUDENT.S_ID
AND TERM.TERM_DESC LIKE 'Summer 2007';
BEGIN
OPEN c_info;
LOOP
FETCH c_info INTO call_id , sec_num ;
EXIT WHEN c_info%notfound;
DBMS_OUTPUT.PUT_LINE('==================================');
DBMS_OUTPUT.PUT_LINE(call_id || ' ' || 'Sec. ' || sec_num);
DBMS_OUTPUT.PUT_LINE('==================================');
OPEN S_NAME;
LOOP
FETCH S_NAME INTO fname , lname ;
EXIT WHEN S_NAME%notfound;
DBMS_OUTPUT.PUT_LINE(fname || ' ' || lname );
END LOOP;
CLOSE S_NAME ;
END LOOP;
CLOSE c_info;
END;
-- The output expected
-- I have having some issues, I am unable to display the proper output. I am trying to use a nested loop but i made some mistake when implementing it. Plus i think an explicit cursor is much better to be used.
Make use of the Northwood university database.
https://drive.google.com/file/d/1M_g7FbgOUahoFtE943OK28UxIFbUFgRk/view?usp=sharing
The script

I'm making a lot of assumptions here - I'm guessing you are getting all students for all courses in your inner loop, but you really just want get students for the particular course section you are dealing with in your outer loop.
So your second query will need to reference the right course section ID to limit the students to just that section.
You don't need to explicitly define cursors unless you need them for some reason - if you just iterating through them, its better to reference them directly in the FOR loop.
So that brings me to the following
set serveroutput on size 4000;
begin
for c_info in (
select call_id,
sec_num,
SEC_ID -- PK to link to enrollment later
from course_section,
course,
term
where course_section.course_id = course.course_id
and term.term_id = course_section.term_id
and term.term_desc = 'Summer 2007' ;
)
loop
dbms_output.put_line('==================================');
dbms_output.put_line(c_info.call_id || ' ' || 'Sec. ' || c_info.sec_num);
dbms_output.put_line('==================================');
for s_name in (
select distinct s_first, s_last
from student,
course_section,
term,
enrollment
where term.term_id = course_section.term_id
and course_section.c_sec_id = enrollment.c_sec_id
and course_section.term_id=term.term_id
and enrollment.s_id = student.s_id
and term.term_desc like 'Summer 2007'
AND ENROLLMENT.C_SEC_ID = C_INFO.SEC_ID -- get students just for THIS course section
)
loop
dbms_output.put_line(s_name.s_first || ' ' || s_name.s_last );
end loop;
end loop;
end;
where I've put query alterations in CAPS.
Since I cut/pasted your SQL there are no aliases in the first query - I'd recommend you correct that, as aliasing columns is always good practice.
Simiarly, I retained the DISTINCT in the second query, but I'd suspect its redundant, because I imagine a student wont enroll more than once for a single course section. (And in reality, if you had two different students named Sue Smith, you would probably want to print them out twice, no?)

Related

Looping through a cursor with a condition on an updated field [PLSQL]

I am currently implementing a PL/SQL procedure, which balances a value in two lists.
Consider this example as background:
Rec_1 | 2
Rec_2 | 1
Rec_3 | 2
Rec_A | -1
Rec_B | -3
Rec_C | -2
I want to loop through all these values one-by-one and settle as much as possible, i.e. that after the first settlement Rec_1 should be 1, Rec_A should be 0. Afterwards, Rec_1 will be settled with Rec_B such that it gets 1, Rec_B gets -2, and so on.
I want to use two cursors to do this, and update the values in its own procedure (or function, if that is necessary), since there is a little more to do than just update this value.
Now, here is my challenge:
How do I know which cursor to fetch after a settlement has happened?
Right now, my code for this function looks like this:
PROCEDURE SettleLists (
ListNegV SYS_REFCURSOR,
ListPosV SYS_REFCURSOR
) IS
currentNegV TABLENAME%ROWTYPE;
currentPosV TABLENAME%ROWTYPE;
BEGIN
FETCH ListNegV INTO currentNegV;
FETCH ListPosV INTO currentPosV;
LOOP
EXIT WHEN ListNegV%NOTFOUND;
EXIT WHEN ListPosV%NOTFOUND;
IF (currentNegV.NUMERICVALUE < 0)
THEN
IF (currentPosV.NUMERICVALUE > 0)
THEN
Settle(currentPosV, currentNegV);
ELSE
FETCH ListPosV INTO currentPosV;
END IF;
ELSE
FETCH ListNegV INTO currentNegV;
END IF;
END LOOP;
END;
Inside the settle procedure, there will be an UPDATE on both records. Since the variables and cursor values are not updated, this will produce an infinite loop. I could update the parameter of settle when the record is updated in the database as well, but since I am not used to cursors, you might have a better idea.
I could consider the cursor to be strongly typed, if that makes any difference. If there is a better way than using a cursor, feel free to suggest it.
Finally, I was playing around with SELECT FOR UPDATE and UPDATE WHERE CURRENT OF, but it did not seem to work when passing the cursor to a procedure in between. If anyone has some idea on this, I would also appreciate your help.
Here is a what I would do. I will surely add some comments.
This is running fine in Oracle 11Gr2 and I am hoping it will run fine even in 7i :).
declare
cursor c1 is
select 'REC_1' HEader,2 Val from dual
union
select 'REC_2' HEader,1 Val from dual
union
select 'REC_3' HEader,2 Val from dual;
cursor c2 is
select 'REC_A' HEader,-1 Val from dual
union
select 'REC_B' HEader,-3 Val from dual
union
select 'REC_C' HEader,-2 Val from dual;
num_bal1 number;
num_bal2 number;
num_settle_amt number;
rec_type_c1 c1%rowtype;
rec_type_c2 c2%rowtype;
begin
Open c1;
open c2;
fetch c1 into rec_type_c1;
fetch c2 into rec_type_c2;
num_bal1 := nvl(rec_type_c1.val,0);
num_bal2 := rec_type_c2.val;
Loop
exit when c1%notfound or c2%notfound;
Loop
dbms_output.put_line('Processing ' || rec_type_c1.header || ' with ' || num_bal1);
--In your example there are only +ve for 1 and -ve for 2. But if that is not correct, check for signs and next 3 statements
num_settle_amt := least(abs(num_bal1), abs(num_bal2) );
num_bal1 := num_bal1 - num_settle_amt;
num_bal2 := num_bal2 + num_settle_amt;
dbms_output.put_line('Setteled ' || num_settle_amt || ' of ' || rec_type_c1.header || ' with ' || rec_type_c2.header );
if num_bal1 = 0 then
--Update in the table. It will not impact variable.
fetch c1 into rec_type_c1;
num_bal1 := nvl(rec_type_c1.val,0);
end if;
if num_bal2 = 0 then
--Update in the table. It will not impact variable.
fetch c2 into rec_type_c2;
num_bal2 := nvl(rec_type_c2.val,0);
end if;
End loop;
end loop;
close c1;
close c2;
end;

ORA-6504 Type of result set variables does not match

I am trying to print records that are mismatching (by some criteria) in my two tables that I have queried below in my procedure
CREATE OR REPLACE PROCEDURE one_two_mismatch( p_rc OUT SYS_REFCURSOR,
p_rc2 OUT SYS_REFCURSOR )
AS
BEGIN
dbms_output.put_line('T1 Table');
OPEN p_rc
FOR select t1.ENTITY_KEY, t1.ENTITY_ID, t1.COMPONENT_ID, t1.PARENT_KEY,
t1.ENTITY_TYPE_KEY from entity t1,(select entity_id from (select
c.entity_id, count(e.component_id) as ONE_CNT, count(c.component_id) as
TWO_CNT from entity e, entity_cmm c where e.entity_id =
c.entity_id group
by c.entity_id) where ONE_CNT <> TWO_CNT) t2 where t1.ENTITY_ID =
t2.ENTITY_ID;
dbms_output.put_line('T2 Table');
OPEN p_rc2
FOR select t3.ENTITY_KEY, t3.ENTITY_ID, t3.COMPONENT_ID, t3.PARENT_KEY,
t3.ENTITY_TYPE_KEY from entity_cmm t3,(select entity_id from
(select c.entity_id, count(e.component_id) as ONE_CNT,
count(c.component_id) as TWO_CNT from est_entity e, entity_cmm c
where e.entity_id = c.entity_id group by c.entity_id) where ONE_CNT <>
TWO_CNT)
t4 where t3.ENTITY_ID = t4.ENTITY_ID;
END one_two_mismatch;
I have written the following statements below in my PL/SQL developer SQL window to execute the above procedure but am encountering an error in line 9 which says type of result set variable or query doesn't match
declare
p_rc sys_refcursor;
p_rc2 sys_refcursor;
l_rec est_entity%rowtype;
m_rec est_entity_cmm%rowtype;
begin
one_two_MISMATCH(p_rc, p_rc2);
LOOP
FETCH p_rc INTO l_rec;
EXIT WHEN p_rc%NOTFOUND;
DBMS_OUTPUT.put_line(l_rec.ENTITY_KEY || ',' || l_rec.ENTITY_ID ||
',' || l_rec.COMPONENT_ID ||',' || l_rec.PARENT_KEY ||','||
l_rec.ENTITY_TYPE_KEY );
END LOOP;
CLOSE p_rc;
LOOP
FETCH p_rc2 INTO m_rec;
EXIT WHEN p_rc2%NOTFOUND;
DBMS_OUTPUT.put_line( m_rec.ENTITY_KEY || ',' || m_rec.ENTITY_ID || ',' ||
m_rec.COMPONENT_ID ||',' || m_rec.PARENT_KEY ||','||
m_rec.ENTITY_TYPE_KEY);
END LOOP;
CLOSE p_rc2;
end;
Can someone please help?
Based on the information provided, it sounds like one or both queries in procedure cmm_st_mismatch do not have the correct column list. In the anonymous block, the two variables declared to receive the ref cursor records
l_rec est_entity%rowtype;
m_rec est_entity_cmm%rowtype;
are declared as row types. The ref cursor being fetched from must have the same number, type and order of columns as the table referenced in the rowtype variable declaration. Judging from the error you described, there is a mismatch.

Postgresql: Array value must start with "{" or dimension information

CREATE OR REPLACE FUNCTION f_old_transactions (IN p_fromdate date, IN p_todate date, IN p_transtype varchar,IN OUT p_cancelled boolean,
OUT p_transaction_date date,
OUT p_type varchar,
OUT p_description varchar,
OUT p_amount numeric)
RETURNS SETOF record AS
$BODY$
declare lRunQuery text;
declare lTotalRec record;
declare lBranchList text[];
declare lTranstype text[];
BEGIN
select into lTranstype
dt.type
from
v_data_types dt;
lTranstype := regexp_split_to_array(p_transtype, ',');
lrunquery := 'select
it.transaction_date trandate,
dt.type,
it.description,
ita.amount,
it.cancelled
from
import_transaction it
inner join import_transaction_account ita on it.import_transaction_id=ita.import_transaction_id
where
it.transaction_date >= ' || quote_literal(p_fromdate) || '
and it.transaction_date <= ' || quote_literal(p_todate) || '
and dt.type = any(' || quote_literall(p_transtype) || ') and';
if (p_cancelled = TRUE) then
lrunquery := lrunquery || '
it.cancelled = ' || quote_literal(p_cancelled) || '';
else
lrunquery := lrunquery || '
it.cancelled = ' || quote_literal(p_cancelled) || '';
end if;
FOR lTotalrec in
execute lRunQuery
LOOP
p_transaction_date := ltotalrec.trandate;
p_type :=ltotalrec.type;
p_description :=ltotalrec.description;
p_amount :=ltotalrec.amount;
p_cancelled := ltotalrec.cancelled;
return next;
END LOOP;
return ;
end;
$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100
ROWS 1000;
ALTER FUNCTION f_old_transactions(date,date,varchar,boolean) OWNER TO "CompuLoanPostgres";
select * from f_old_transactions ('01-Jan-2010','31-Dec-2018','Receipt Cash','FALSE')
I'm getting an error that my array value must start with "{". My array I'm trying to create is from a view v_data_type the view consist of only one column with a varchar type.
Can anyone please direct me where the issue in my code is?
Thank you in advance
I don't think you've given us enough information to know for certain what's going wrong. Notably, I have no idea what the tables should look like, either in schema or content. Without that, I can't build a test case in my own DB to debug.
That said, I noticed a couple things, specifically around the lTranstype variable:
You're assigning lTranstype twice. First you SELECT INTO it, and then you immediately assign it to a value unpacked from the p_transtype argument. It's not clear to me what you want in that variable.
When constructing your query later, you include and dt.type = any(' || quote_literall(p_transtype) || '). The problem is that p_transtype is a varchar argument, and you're trying to access it like an array. I suspect you want that to read and dt.type = any(' || quote_literall(lTranstype) || '), but I could be mistaken.
I'm guessing that your type error is coming from that second problem, but it seems like you need to reassess what the different variables in this function are intended for. Good luck.

For loop cursor in teradata

In my Teradata Stored Procedure, I want to have a for loop cursor against a dynamic sql.
Below is the code snippet
SET get_exclude_condition = '';
SET colum_id = 'SELECT MIN (parent_criteria_id) ,MAX (parent_criteria_id) FROM arc_mdm_tbls.intnl_mtch_criteria WHERE act_ind = 1 AND criteria_typ = ''Exclude'' AND mtch_technique_id ='||mtch_technique_id||';' ;
PREPARE input_stmt FROM colum_id;
OPEN flex_cursor;
FETCH flex_cursor INTO parent_criteria_id_min , parent_criteria_id_max ;
CLOSE flex_cursor;
SET get_exclude_condition = '';
WHILE (parent_criteria_id_min <= parent_criteria_id_max)
DO
SET get_exclude_condition = get_exclude_condition || '( ';
SET for_loop_stmt = 'SELECT criteria FROM arc_mdm_tbls.intnl_mtch_criteria WHERE act_ind = 1 AND mtch_technique_id ='||mtch_technique_id||' AND criteria_typ= ''Exclude'' AND parent_criteria_id ='||parent_criteria_id_min||';';
FOR for_loop_rule AS c_cursor_rule CURSOR FOR
for_loop_stmt
DO
Can I declare a for loop cursor like this ?
Or do I need to have something like this only ?
FOR for_loop_rule AS c_cursor_rule CURSOR FOR
SELECT rule_id
FROM arc_stage_tbls.assmt_scoring_rules
WHERE rule_typ = :v_RuleType
ORDER BY rule_id
DO
I mean can I first frame the dynamic sql and then have a for loop cursor on top of that or with the cursor declaration only I need to have a static sql query ?
Please clarify.
While you haven't posted everything that the stored procedure is trying to accomplish, it does appear that what you are asking can be accomplished using SET based logic and not looping through a cursor. If you need to parameterize the 'mtch_technique_id' you can use a Teradata macro which will allow you to maintain a SET based approach.
Here is the SQL for creating a macro that returns a result set based on my interpretation of what your snippet of the stored procedure is trying to accomplish:
REPLACE MACRO {MyDB}.Intnl_Mtch_Criteria(mtch_technique_id INTEGER) AS
(
SELECT criteria
FROM arc_mdm_tbls.intnl_mtch_criteria
WHERE act_ind = 1
AND (much_technique_id, criteria_typ) IN
(SELECT MIN((parent_criteria_id), MAX (parent_criteria_id)
FROM arc_mdm_tbls.intnl_mtch_criteria
WHERE act_ind = 1
AND criteria_typ = 'Exclude'
AND mtch_technique_id = :mtch_technique_id;
);

getting data from memory instead of table

I have a parameter table with 10 rows. Called parameter_table.
In my PL/SQL procedure, I do loop in 2 million records. And each time querying this parameter table too.
I want to load this parameter table in to the memory and decrease the I/O process.
What is the best way to do this?
FOR cur_opt
IN (SELECT customer_ID,
NVL (customer_type, 'C') cus_type
FROM invoice_codes
WHERE ms.invoice_type='RT')
LOOP
....
...
Select data From parameter_table Where cus_type = cur_opt.cus_type AND cr_date < sysdate ; -- Where clause is much complex than this..
....
...
END LOOP;
You can just join it to your main query:
select customer_id, data
from parameter_table t, invoice_codes c
where t.cus_type = nvl(c.customer_type, 'C')
and t.cr_date < sysdate
However, if you've got 2 million records in invoice_codes, then joining to the parameter table is the least of your concerns - looping through this will take some time (and is probably the real cause of your I/O problems).
I Think you may change the query ,joining to parameter_table, so there will be no need to hit the select statement inside the loop. (like what #Chris Saxon solution)
But as a way to use cashed data,
You could fill a dictionary like, array and then refer it when necessary
Something like this may help:
you have to call Fill_parameters_cash before starting the main process and call get_parameter to fetch the data, the input parameter to call get_parameter is the dictionary key
TYPE ga_parameter_t IS TABLE OF parameter_table%ROWTYPE INDEX BY BINARY_INTEGER;
ga_parameter ga_parameter_t;
procedure Fill_parameters_cash is
begin
ga_parameter.DELETE;
SELECT * BULK COLLECT
INTO ga_parameter
FROM parameter_table;
end Fill_parameters_cash;
FUNCTION get_parameter(cus_type invoice_codes.cus_type%TYPE,
is_fdound OUT BOOLEAN)
RETURN parameter_table%ROWTYPE IS
result_value parameter_table%ROWTYPE;
pos NUMBER;
BEGIN
result_value := NULL;
is_fdound := FALSE;
IF cus_type IS NULL THEN
RETURN NULL;
END IF;
pos := ga_parameter.FIRST;
WHILE pos IS NOT NULL
LOOP
EXIT WHEN ga_parameter(pos).cus_type = cus_type;
pos := ga_parameter.NEXT(pos);
END LOOP;
IF pos IS NOT NULL THEN
is_fdound := TRUE;
result_value := ga_parameter(pos);
END IF;
RETURN result_value;
END get_parameter;
I'd guess looping through a million records is already causing issues. Not quite sure how this parameter table lookup is really worsening it.
Anyways, if this is really the only approach you can take, then you could do an inner or outer join in the cursor declaration.
----
FOR cur_opt
IN (SELECT customer_ID,
NVL (customer_type, 'C') cus_type
FROM invoice_codes codes,
parameter_table par
WHERE ms.invoice_type='RT'
and codes.cus_type = par.cus_type -- (or an outer join) maybe?
) loop
..........

Resources