I am trying to write a block of SQL code that compares two dates from two tables. One table shows when an item was acquired (ci_acquired_date). Another table shows when an item was an assigned to an employee (date_acquired). My goal is to use a loop that loops through the inventory and compare the dates and see if any of the dates assigned to an employee is less than the date the item was acquired (because it is impossible to assign an employee an item that was never bought) and use DBMS to show me which item ID it is and the dates it was acquired and assigned.
This is my code:
declare
cursor task_five is
select a.date_assigned, a.ci_inv_id, b.ci_acquired_date
from jaherna42.employee_ci a
join jaherna42.ci_inventory b
on a.ci_inv_id = b.ci_inv_id
where a.user_or_support = 'USER';
row_one jaherna42.employee_ci%rowtype;
row_two jaherna42.ci_inventory%rowtype;
begin
for row_one in task_five
loop
if(row_one.date_assigned < row_two.ci_acquired_date)
then
dbms_output.put_line('The error is: ' || row_one.ci_inv_id);
dbms_output.put_line('The date assigned is: ' || row_one.date_assigned);
dbms_output.put_line('The date acquired is: ' || row_two.ci_acquired_date);
end if;
end loop;
end;
When I run it, the script output box would show
PL/SQL procedure successfully completed.
But there would be nothing showing up in my dbms output box. What is the error?
row_twois a local variable that is declared but no value is ever assigned to it. Thus, every field in the record is alwaysnull. When you compare a valuerow_one.date_assignedagainst anull, the comparison will always evaluate tofalse. So yourifstatement is alwaysfalseand nothing will ever be printed.I'm a bit surprised that the code you posted compiles.
row_oneis declared as ajaherna42.employee_ci%rowtypebut the query you've defined doesn't appear to return all the columns fromemployee_ci. Perhaps you're just lucky that the order and types of the columns in your table matches the order and types in your query even though that query pulls data from multiple tables. But that's a lucky accident.If you want to keep the cursor in the declaration section, you'd almost certainly want your local variable to be declared against the cursor's
%rowtypenot the table's. So something like this is potentially what you're really after.Unless it's a requirement of your assignment, though, it would probably make sense to avoid the declarations entirely and just use an implicit cursor in the code. And to do the comparison in SQL rather than PL/SQL
I'd also generally suggest using meaningful aliases in your query. Aliasing tables to
aandbis like using single character variable names-- unlikely to help someone reading the code understand what it is doing. It would almost certainly be more useful to use some consistent naming convention where, say,employee_ciis always aliased toempandci_inventoryis always aliased toinv.