Question : New to plsql and want to create a cursor with an alias column.  I seem to be having problems display this alias after it is fetched.

I'm inexperienced with PLSQL.  Below in an example of something I'm trying to do where my CURSOR has an alias column in the query and I'm trying to use this variable after the FETCH.  In this case I'm just trying to display it.  Yes SQL> set serveroutput on; has been set.  Are you allowed to use an ALIAS column as a variable?  If not how would you do this?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
CREATE OR REPLACE procedure TEST
 
cursor cur_test IS
select a, b, a+b as sum_ab
from table_name;
 
begin
 open cur_test
 
fetch cur_test INTO a, b, sum_ab
exit fetch when NOT cur_test%found;
DBMS_OUTPUT.PUT_LINE('sum_ab ' || sum_ab);
 
END LOOP;
 
CLOSE cur_test;
 
End test;
Open in New Window Select All

Answer : New to plsql and want to create a cursor with an alias column.  I seem to be having problems display this alias after it is fetched.

You need to fetch the cursor into something (e.g. PL/SQL variables).  Then you output the variable.  Example:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
CREATE OR REPLACE procedure TEST
  CURSOR cur_test IS
  SELECT a, b, a+b as sum_ab
  FROM table_name;
  
  la     table_name.a%TYPE;
  lb     table_name.b%TYPE;
  lSumAB NUMBER;
BEGIN
  OPEN cur_test;
  
  LOOP
    FETCH cur_test INTO la, lb lSumAB
    EXIT WHEN NOT cur_test%found;
    DBMS_OUTPUT.PUT_LINE('sum_ab ' || lSumAB);
  END LOOP;
 
  CLOSE cur_test;
END test;
Open in New Window Select All
Random Solutions  
 
programming4us programming4us