CREATE TABLE WORK(
WorkerNo NUMBER(10) NOT NULL,
TotalSalary NUMBER(10),
CONSTRAINT WORK_PKEY PRIMARY KEY (WorkerNo),
INSERT INTO WORK( 1, NULL );
INSERT INTO WORK( 2, NULL );
CREATE TABLE WorkSalary(
City VARCHAR2(30),
Postal VARCHAR2(30),
Salary NUMBER(9) NOT NULL,
CONSTRAINT WorkSalary_PKEY PRIMARY KEY(City,Postal),
INSERT INTO WorkSalary('New York City', '123456', 5000);
INSERT INTO WorkSalary('Washington DC', '154876', 4500);
INSERT INTO WorkSalary('New Jersey', '458741', 3500);
INSERT INTO WorkSalary('Maryland', '487451', 2000);
CREATE TABLE WORKDONE(
WorkerNo NUMBER(10) NOT NULL,
JobNo NUMBER(2) NOT NULL,
City VARCHAR2(30) NOT NULL,
Postal VARCHAR2(30) NOT NULL,
CONSTRAINT WORKDONE_PKEY PRIMARY KEY (WorkerNo, JobNo),
CONSTRAINT WORKDONE_FKEY1 FOREIGN KEY (WorkerNo)
REFERENCES WORK(WorkerNo),
CONSTRAINT WORKDONE_FKEY2 FOREIGN KEY (City)
REFERENCES WorkSalary(City),
CONSTRAINT WORKDONE_FKEY3 FOREIGN KEY (Postal)
REFERENCES WorkSalary(Postal) );
INSERT INTO WORKDONE VALUES( 1, 1, 'New York City', '123456');
INSERT INTO WORKDONE VALUES( 1, 2, 'Maryland', '487451');
INSERT INTO WORKDONE VALUES( 1, 3, 'New Jersey', '458741');
INSERT INTO WORKDONE VALUES( 2, 1, 'New Jersey', '458741');
INSERT INTO WORKDONE VALUES( 2, 2, 'New York City', '123456');
INSERT INTO WORKDONE VALUES( 2, 3, 'Washington DC', '154876');
我需要创建一个函数来获取工人在表 workdone 上所做的所有薪水的总和。例如,workerno 1 完成了 3 项工作,显示在 workdone 表上。我需要根据他工作的地方通过参考表薪来获得他所做的每项工作的薪水。然后我将把他所有的薪水加在一起,并将该工人的总薪水插入到具有列 totalsalary 的表工作中。
CREATE OR REPLACE FUNCTION SALARYCOUNT ( worker_no IN NUMBER ) RETURN NUMBER IS
wcity WORKDONE.City%TYPE;
wpostal WORKDONE.Postal%TYPE;
total NUMBER:
i INTEGER: = 1;
total_salary NUMBER;
wsalary NUMBER;
BEGIN
SELECT COUNT(*)
INTO total
FROM WORKDONE
WHERE WorkerNo = worker_no;
WHILE i < total
LOOP
SELECT City,Postal INTO wcity,wpostal FROM WORKDONE WHERE WorkerNo = worker_no;
SELECT Salary INTO wsalary FROM WorkSalary WHERE wcity = City AND wpostal = Postal;
total_salary: =total_salary+wsalary ;
UPDATE WORK SET TotalSalary: = total_salary
WHERE WorkerNo = worker_no;
i: = i+1;
END LOOP;
RETURN(total_salary);
END SALARYCOUNT ;
/
创建函数时出现编译错误。我知道,如果我能够毫无错误地创建函数,那么我创建此函数的逻辑也可能不正确。请帮助我解决错误和代码的逻辑。我对此很陌生。