create table employee
(
employedID number,
employeeName varchar2(30),
deptid number,
salary number
);
Create table department
(
deptid number,
deptname varchar2(30)
);
CREATE OR REPLACE PROCEDURE insertemp
(
PemployedID IN number,
PemployeeName IN varchar,
Pdepid IN number,
Psalary IN number
)
IS
BEGIN
IF Pdepid NOT IN (SELECT * from department.deptid) THEN
INSERT INTO employee VALUES (PemployedID,’PemployeeName’,00,Psalary);
ELSE
INSERT INTO employee VALUES (PemployedID,’PemployeeName’,Pdepid, Psalary);
END IF;
COMMIT;
END;
/
我正在尝试创建一个存储过程,我不熟悉oracle SQl语法。如果表部门没有Pdepid那么我想在表雇员中插入一条记录,其中00为Pdepid。为此我使用IF语句。我收到了此程序的编译错误。
答案 0 :(得分:2)
这样的东西应该在Oracle中起作用:
CREATE OR REPLACE PROCEDURE insertemp
(
PemployedID IN number,
PemployeeName IN varchar,
Pdepid IN number,
Psalary IN number
)
IS
v_tmp NUMBER;
BEGIN
SELECT COUNT(1) INTO v_tmp
FROM department
WHERE deptid = Pdepid;
IF v_tmp = 0 THEN
INSERT INTO employee VALUES (PemployedID,PemployeeName,00,Psalary);
ELSE
INSERT INTO employee VALUES (PemployedID,PemployeeName,Pdepid, Psalary);
END IF;
END;
你可以像这样简化这个功能:
CREATE OR REPLACE PROCEDURE insertemp
(
PemployedID IN number,
PemployeeName IN varchar,
Pdepid IN number,
Psalary IN number
)
IS
BEGIN
INSERT INTO employee VALUES (PemployedID,PemployeeName,
DECODE((SELECT COUNT(1) FROM department WHERE deptid = Pdepid), 0, 0, Pdepid),Psalary);
END;