Oracle 需要知道在PL/SQL中存储过程中是否正确使用了if语句
我对PL/SQL非常不熟悉,不知道是否正确使用了if语句。我正在使用Oracle Live SQL。这都是试图在名为“employees”的表中插入新行。唯一的非空值是employeeid、employeename和jobidOracle 需要知道在PL/SQL中存储过程中是否正确使用了if语句,oracle,stored-procedures,plsql,Oracle,Stored Procedures,Plsql,我对PL/SQL非常不熟悉,不知道是否正确使用了if语句。我正在使用Oracle Live SQL。这都是试图在名为“employees”的表中插入新行。唯一的非空值是employeeid、employeename和jobid CREATE OR REPLACE PROCEDURE employees.insert_employee ( p_employeeid employees.employeeid%TYPE, p_employeename
CREATE OR REPLACE PROCEDURE employees.insert_employee
(
p_employeeid employees.employeeid%TYPE,
p_employeename employees.employeename%TYPE,
p_phone employees.phone%TYPE,
p_jobid employees.jobid%TYPE,
p_salary employees.salary%TYPE,
p_managerid employees.managerid%TYPE,
p_departmentid employees.departmentid%TYPE
)
AS
BEGIN
IF p_employeeid IS NULL THEN /* If one of the mandatory values are null */
RAISE VALUE_ERROR;
END IF;
IF p_employeename IS NULL THEN
RAISE VALUE_ERROR;
END IF;
IF p_jobid IS NULL THEN
RAISE VALUE_ERROR;
END IF;
IF p_jobid != employees.jobid THEN /* if jobid entered is not in the table */
RAISE VALUE_ERROR;
END IF;
IF p_salary < 0 THEN /* if the entered salary is negative */
RAISE VALUE_ERROR;
END IF;
IF p_departmentid != employees.departmentid THEN /* if the departmentid entered is not in the table */
RAISE VALUE_ERROR;
END IF;
IF p.employeeid = employees.employeeid THEN /* if the employeeid already exists */
RAISE RAISE_APPLICATION_ERROR(-2000);
END IF;
INSERT INTO employees (employeeid, employeename, phone, jobid, salary, managerid, departmentid)
VALUES(p_employeeid, p_employeename, p_phone, p_jobid, p_salary, p_managerid, p_departmentid);
END;
即使语法正确,我也不认为您正确使用了它们 1如果不允许为空,则在表中将其标记为非空 2如果departmentID必须存在,则这是外键约束 3如果employeeID存在,那么即使您的语法有效,它也应该是唯一的约束,而事实并非如此
正确声明,DB引擎将为您确保所有这些。我认为它甚至不会编译。此外,你不应该像别人告诉你的那样做。如果可以的话,还有一些反对意见,关于最初的问题:你是否正确使用if 第一批IFs可以使用或缩短: 没有employees.jobid-您必须先选择它。例如:
declare
l_cnt;
begin
select count(*)
into l_cnt
from employees e
where e.jobid = p_jobid;
if l_cnt = 0 then -- there's no such job in the table
raise value_error;
end if;
end;
最后,你检查了最后的情况,并试图提出一些问题
错误的原因有三:
你不养我。。。
用户定义异常的范围为-20001到-20999五位数,而不是4位数
RAISE_应用程序_错误需要另一个参数
所以-正确-你应该
raise_application_error(-20001, 'That does not exist');
这与IF语句无关。您登录的用户可能没有编译过程的权限,也可能没有将IF语句插入到表中的权限。但是,如果IF语句使用正确,这就是我想知道的。非常感谢
declare
l_cnt;
begin
select count(*)
into l_cnt
from employees e
where e.jobid = p_jobid;
if l_cnt = 0 then -- there's no such job in the table
raise value_error;
end if;
end;
RAISE RAISE_APPLICATION_ERROR(-2000);
raise_application_error(-20001, 'That does not exist');