0
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,那么我想在表员工中插入一条记录,其中 Pdepid 为 00。为此,我使用 IF 语句。我收到此过程的编译错误。

4

1 回答 1

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;
于 2013-09-27T14:25:27.890 回答