出现错误“Table,在此上下文中不允许查看或序列引用 'EMPLOYEES.EMP_NAME'”
Getting error 'Table,View Or Sequence reference 'EMPLOYEES.EMP_NAME' not allowed in this context'
create table Employees (emp_id number, emp_name varchar2(50), salary number, department_id number, DESIGNATION varchar2(50), DEVELOPED_TESTED varchar2(50)) ;
insert into Employees values(1,'ALex',10000,10,'Developer','VLC');
insert into Employees values(2,'Duplex',20000,20,'Developer','VLC');
insert into Employees values(3,'Charles',30000,30,'Tester','Tested_VLC');
insert into Employees values(4,'Demon',40000,40,'Tester ','Tested_VLC');
insert into employees values(5,'Chaem',5000,50,'Developer','');
要求:
我要return2 如果指定是'Developer'并且developed_tested列为空。但是在输出中我得到的是 null 而不是 2。你能检查一下吗?
代码:
create or replace FUNCTION calculate_royalty (
i_empno IN NUMBER
) RETURN VARCHAR2 IS
l_employee employees%ROWTYPE;
BEGIN
SELECT *
INTO l_employee
FROM
employees
WHERE emp_id = i_empno;
IF l_employee.designation = 'Developer' THEN
RETURN l_employee.developed_tested;
ELSIF l_employee.designation = 'Developer' and l_employee.developed_tested is null THEN
RETURN 2;
ELSE
RETURN 1;
END IF;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN 0;
END ;
select CALCULATE_ROYALTY(5) 来自双重; -- 输出为 NULL,但理想情况下应为 2。
只是select整行变成一个变量,不需要使用计数。使用NO_DATA_FOUND
异常处理用户不存在的情况。
您的问题提到了与“版税”列相关的一些功能。这是您的测试用例中缺少的,但修改下面的代码应该是小菜一碟,以满足您的所有需求。
create or replace FUNCTION calculate_royalty (
empno_i IN NUMBER
-- don't use column names as names of input parameters, that is a recipe for disaster. Use prefix/suffix.
) RETURN VARCHAR2 IS
lv_count NUMBER;
l_employee employees%ROWTYPE;
BEGIN
SELECT *
INTO l_employee
FROM
employees
WHERE emp_id = empno_i;
IF l_employee.designation = 'Developer' THEN
RETURN l_employee.salary;
ELSE
RETURN 1;
END IF;
-- dbms_output just sends data to a buffer that you then print to the console. it cannot be used as a return value.
-- RETURN dbms_output.put_line('Emp Name :'...
-- elsif needs to be followed by an expression. It cannot be followed by a RETURN keyword. Use "ELSE" instead if there is no condition.
-- elsif return 1;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN 0;
END ;
/
set serveroutput on size 999999
clear screen
BEGIN
dbms_output.put_line('output for Developer: '|| calculate_royalty(EMPNO_I => 2));
dbms_output.put_line('output for no Developer: '|| calculate_royalty(EMPNO_I => 3));
dbms_output.put_line('output for non-existing user: '|| calculate_royalty(EMPNO_I => 10));
END;
output for Developer: 20000
output for no Developer: 1
output for non-existing user: 0
PL/SQL procedure successfully completed.
create table Employees (emp_id number, emp_name varchar2(50), salary number, department_id number, DESIGNATION varchar2(50), DEVELOPED_TESTED varchar2(50)) ;
insert into Employees values(1,'ALex',10000,10,'Developer','VLC');
insert into Employees values(2,'Duplex',20000,20,'Developer','VLC');
insert into Employees values(3,'Charles',30000,30,'Tester','Tested_VLC');
insert into Employees values(4,'Demon',40000,40,'Tester ','Tested_VLC');
insert into employees values(5,'Chaem',5000,50,'Developer','');
要求:
我要return2 如果指定是'Developer'并且developed_tested列为空。但是在输出中我得到的是 null 而不是 2。你能检查一下吗?
代码:
create or replace FUNCTION calculate_royalty (
i_empno IN NUMBER
) RETURN VARCHAR2 IS
l_employee employees%ROWTYPE;
BEGIN
SELECT *
INTO l_employee
FROM
employees
WHERE emp_id = i_empno;
IF l_employee.designation = 'Developer' THEN
RETURN l_employee.developed_tested;
ELSIF l_employee.designation = 'Developer' and l_employee.developed_tested is null THEN
RETURN 2;
ELSE
RETURN 1;
END IF;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN 0;
END ;
select CALCULATE_ROYALTY(5) 来自双重; -- 输出为 NULL,但理想情况下应为 2。
只是select整行变成一个变量,不需要使用计数。使用NO_DATA_FOUND
异常处理用户不存在的情况。
您的问题提到了与“版税”列相关的一些功能。这是您的测试用例中缺少的,但修改下面的代码应该是小菜一碟,以满足您的所有需求。
create or replace FUNCTION calculate_royalty (
empno_i IN NUMBER
-- don't use column names as names of input parameters, that is a recipe for disaster. Use prefix/suffix.
) RETURN VARCHAR2 IS
lv_count NUMBER;
l_employee employees%ROWTYPE;
BEGIN
SELECT *
INTO l_employee
FROM
employees
WHERE emp_id = empno_i;
IF l_employee.designation = 'Developer' THEN
RETURN l_employee.salary;
ELSE
RETURN 1;
END IF;
-- dbms_output just sends data to a buffer that you then print to the console. it cannot be used as a return value.
-- RETURN dbms_output.put_line('Emp Name :'...
-- elsif needs to be followed by an expression. It cannot be followed by a RETURN keyword. Use "ELSE" instead if there is no condition.
-- elsif return 1;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN 0;
END ;
/
set serveroutput on size 999999
clear screen
BEGIN
dbms_output.put_line('output for Developer: '|| calculate_royalty(EMPNO_I => 2));
dbms_output.put_line('output for no Developer: '|| calculate_royalty(EMPNO_I => 3));
dbms_output.put_line('output for non-existing user: '|| calculate_royalty(EMPNO_I => 10));
END;
output for Developer: 20000
output for no Developer: 1
output for non-existing user: 0
PL/SQL procedure successfully completed.