在游标中使用for循环和别名检索变量

时间:2019-06-27 03:00:43

标签: oracle for-loop plsql cursor alias

我正在尝试使用for循环打印一组数据,并且很好奇是否可以使用别名来检索信息,或者我将如何为for循环使用

DECLARE
job_ VARCHAR2(35);
employee_name VARCHAR2(35);
manager_name VARCHAR2(35);
salary_difference NUMBER(20,2);
CURSOR cur_salary_difference IS
    SELECT job_title, employees.first_name || ' ' || employees.last_name "Employee", 
    m.first_name || ' ' || m.last_name "Manager", (employees.salary-m.salary) "Salary Difference" 
    FROM employees
    LEFT OUTER JOIN employees m ON
       employees.manager_id = m.employee_id
       JOIN jobs j ON employees.job_id = j.job_id
    ORDER BY "Employee";
BEGIN
    DBMS_OUTPUT.PUT_LINE('Listings of employees with salary difference compared to manager');
    FOR people IN cur_salary_difference LOOP
        DBMS_OUTPUT.PUT_LINE (chr(10) || 'Job Title:' || people.job_title);
        DBMS_OUTPUT.PUT_LINE ('Employee:' || people.employee);
        DBMS_OUTPUT.PUT_LINE ('Manager:' || people.manager);
        DBMS_OUTPUT.PUT_LINE ('Salary Difference:' || salary_difference);

    END LOOP;
END;

2 个答案:

答案 0 :(得分:2)

  1. 您应该尝试一下。
  2. 是的,别名将起作用。由于您已将别名放在双引号中,因此Oracle会将它们视为区分大小写,并且在以后使用它们时需要双引号。 (这就是为什么您不应该在99%的时间内双引号标识符的原因。)

答案 1 :(得分:1)

应该可以,只要解决薪水上的错字

  (employees.salary-m.salary) "Salary_Difference"