按照PL/SQL过程的顺序,我需要执行一个作为参数接收部门编号、薪资和百分比的过程;并提高调用中所示部门的所有员工的薪资。增加的数额将是电话中显示的百分比或薪金(以每一种情况下对雇员更有利者为准)。我有这个代码,但不起作用。
CREATE OR REPLACE PROCEDURE pujarSalari (numdept number,diners number,percentatge number)
AS
souapujar number(10);
CURSOR buscarnoms IS SELECT codi_emp FROM empleats WHERE codi_dept=numdept;
BEGIN
OPEN buscarnoms;
FETCH buscarnoms INTO souapujar;
WHILE buscarnoms%FOUND LOOP
IF (empleats.sou*(1+percentatge/100))>=(empleats.sou+diners) THEN
UPDATE empleats set empleats.sou=empleats.sou*(1+percentatge/100) where codi_emp=souapujar;
ELSE
UPDATE empleats set empleats.sou=empleats.sou+diners where codi_emp=souapujar;
END IF;
FETCH buscarnoms INTO souapujar;
END LOOP;
CLOSE buscarnoms;
END; 错误代码是
Procedure PUJARSALARI compilado
LINE/COL ERROR
--------- -------------------------------------------------------------
9/1 PL/SQL: Statement ignored
9/14 PLS-00357: Table,View Or Sequence reference 'EMPLEATS.SOU' not allowed in this context
Errores: comprobar log de compilador发布于 2020-04-29 11:15:31
您的IF语句引用的是表列。在PL/SQL中,我们只能引用变量(包括隐式游标)。(这是嵌入SQL语句之外的。)
您有一个游标buscarnoms,它从empleats中选择,但不包括所需的所有列。因此,您必须更改光标投影,然后在代码中引用游标。
CREATE OR REPLACE PROCEDURE pujarSalari
(numdept number,
diners number,
percentatge number)
AS
CURSOR buscarnoms IS
SELECT *
FROM empleats
WHERE codi_dept = numdept;
souapujar buscarnoms%rowtype; -- inherits the projection of the cursor
BEGIN
OPEN buscarnoms;
FETCH buscarnoms INTO souapujar;
WHILE buscarnoms%FOUND LOOP
IF (souapujar.sou*(1+percentatge/100)) >= (souapujar.sou+diners) THEN
UPDATE empleats
set empleats.sou = empleats.sou*(1+percentatge/100)
where codi_emp = souapujar.codi_emp;
ELSE
UPDATE empleats
set empleats.sou = empleats.sou+diners
where codi_emp = souapujar.codi_emp;
END IF;
FETCH buscarnoms INTO souapujar;
END LOOP;
CLOSE buscarnoms;
END; 有几种方法可以简化此代码,例如,将显式游标替换为隐式游标。甚至用基于集合的update语句替换游标循环。但是,我已经尽可能地将代码留在了原来的地方,以突出显示问题的解决方案。
--您说这是几种简化代码的方法。我如何简化我的代码?
隐含游标:
CREATE OR REPLACE PROCEDURE pujarSalari
(numdept number,
diners number,
percentatge number)
AS
BEGIN
FOR buscarnoms in (SELECT *
FROM empleats
WHERE codi_dept = numdept)
LOOP
IF (souapujar.sou*(1+percentatge/100)) >= (souapujar.sou+diners) THEN
UPDATE empleats
set empleats.sou = empleats.sou*(1+percentatge/100)
where codi_emp = souapujar.codi_emp;
ELSE
UPDATE empleats
set empleats.sou = empleats.sou+diners
where codi_emp = souapujar.codi_emp;
END IF;
END LOOP;
END; 以组为基础:
CREATE OR REPLACE PROCEDURE pujarSalari
(numdept number,
diners number,
percentatge number)
AS
BEGIN
UPDATE empleats
set sou = greatest(empleats.sou + diners,
empleats.sou * (1+percentatge/100) )
where codi_dept = numdept;
END; 发布于 2020-04-29 18:35:07
事实上,这是一个不同的问题,应该以这种方式提出。但是最大的简化如何。将其简化为一条语句:
create or replace procedure pujarsalari
(numdept number,
diners number,
percentatge number)
as
begin
update empleats
set sou = greatest(sou*(1+percentatge/100), sou+diners)
where codi_dept = numdept;
end pujarsalari;一天中的小贴士:在SQL中,停止考虑单个行。不要一次考虑所有行的集合。
注意:没有测试,因为您认为不适合发布两个样本数据和表定义。
https://stackoverflow.com/questions/61500280
复制相似问题