How to access concatenated columns in Record using FOR Cursor Loop
-
28-10-2019 - |
Question
Trying to use records with the cursor and later index by table in the mix.
DECLARE CURSOR emp_cur IS
SELECT employee_id, first_name||' '|| last_name "name"
FROM employees
ORDER BY salary desc;
emp_rec emp_cur%ROWTYPE;
BEGIN
FOR emp_rec IN emp_cur
LOOP
DBMS_OUTPUT.PUT_LINE('Employee ID:'||emp_rec.employee_id||CHR(10)||
'Employee Name:'||emp_rec.name);
EXIT WHEN emp_cur%ROWCOUNT = 10;
END LOOP;
END;
When I run above code I get
ERROR at line 13, "NAME" must be declared.
How can I access the column first_name||' '||last_name inside the For loop? or do I just have to do traditional OPEN, simple loop, FETCH and close?
Solution
You're very close; the problem is just that identifiers that are not in double-quotes get converted to uppercase, while identifiers that are in double-quotes do not. So name
, NAME
, and "NAME"
are equivalent to each other, and all of them are different from "name"
. Either change this:
first_name||' '|| last_name "name"
to this:
first_name||' '|| last_name name
or else, if you prefer, change this:
'Employee Name:'||emp_rec.name
to this:
'Employee Name:'||emp_rec."name"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow