我的游标SQL查询有什么问题

时间:2019-04-06 04:13:51

标签: oracle plsql database-cursor

这是我应该做的。假设已创建表,并且所有列均正确命名为

“使用存储的过程和光标,显示工作ID为IT_PROG或SA_MAN且工资大于3000的经理的位置(包括街道,邮政编码,城市和国家/地区”)。

这是我到目前为止编写的代码,但是游标的sql语句似乎不起作用。对于DEPARTMENTS表,FK是MANAGER_ID和LOCATION_ID,对于EMPLOYEES表,FK是JOB_ID,而LOCATIONS表没有FK。所有主键都已设置

代码如下:

create or replace procedure mgtLocation
is
    cursor getLoc is
        select LOCATIONS.STREET_ADDRESS, LOCATIONS.POSTAL_CODE, LOCATIONS.CITY, 
               LOCATIONS.COUNTRY, LOCATIONS.LOCATIONS_ID, LOCATIONS.LOCATIONS_ID
          from LOCATIONS 
    inner join DEPARTMENTS on DEPARTMENTS.MANAGER_ID = EMPLOYEES.EMPLOYEE_ID
    inner join LOCATIONS on LOCATIONS.LOCATION_ID = DEPARTMENTS.LOCATION_ID
         where EMPLOYEES.Job_ID in (select Job_ID from EMPLOYEES where Job_ID = 'IT_PROG' or Job_ID = 'SA_MAN' and SALARY > 3000); 
    EmpLoc getLoc%rowtype;
begin
    dbms_output.put_line('=================');
    open getLoc;
    loop
        fetch getLoc into EmpLoc;
        EXIT WHEN getLoc%NOTFOUND;
        dbms_output.put_line('Street: ' || EmpLoc.STREET_ADDRESS ||
            ' Zip Code: ' || EmpLoc.POSTAL_CODE ||
            ' City: ' || EmpLoc.CITY ||
            ' Country: ' || EmpLoc.COUNTRY);
    end loop;
    dbms_output.put_line('=================');
    close getLoc;
end;
/
execute mgtLocation;

内部联接出现错误,我似乎无法弄清楚如何对其进行修复以使其起作用。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

cursor getLoc is
    select LOCATIONS.STREET_ADDRESS, 
          LOCATIONS.POSTAL_CODE, LOCATIONS.CITY, 
          LOCATIONS.COUNTRY, LOCATIONS.LOCATIONS_ID, 
          LOCATIONS.LOCATIONS_ID
    from LOCATIONS 
    inner join DEPARTMENTS  on   
          DEPARTMENTS.LOCATION_ID = LOCATIONS.LOCATION_ID  
    inner join EMPLOYEES on 
          EMPLOYEES.EMPLOYEE_ID = 
          DEPARTMENTS.MANAGER_ID
    where (EMPLOYEES.Job_ID = 'IT_PROG' or 
          EMPLOYEES.Job_ID = 'SA_MAN') 
    and SALARY > 3000;