我想使用 PL / SQL oracle 更新所有员工薪水。这是我的代码。
DECLARE
c_id customers.id%type;
c_sal customers.salary%type;
BEGIN
SELECT salary
INTO c_sal
FROM customers;
IF (c_sal <= 6500) THEN
UPDATE customers
SET salary = salary + 1000;
dbms_output.put_line ('Salary updated');
END IF;
END;
/
所以这是员工表:
从客户中选择*;
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 3000
2 Khilan 25 Delhi 1500
3 kaushik 23 Kota 2000
4 Chaitali 25 Mumbai 6500
5 Hardik 27 Bhopal 8500
6 Komal 22 MP 4500
答案 0 :(得分:0)
根据您的问题,您希望更新all the rows in Employee
表salary <= 6500
。所以你不需要任何变量或选择查询。您可以在过程中使用简单的更新语句。无需DECLARE
部分。
BEGIN
UPDATE customers
SET salary = salary + 1000
WHERE salary <= 6500;
COMMIT;
dbms_output.put_line ('Salary updated');
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('No any employee is having Salary <= 6500.');
END;
/
如果您想拥有IF-ELSE
,可以使用光标执行此操作。但是我总是会选择上面显示的第一个选项。下面是使用IF-ELSE
的游标代码:
DECLARE
CURSOR my_cursor IS select * FROM customers;
BEGIN
FOR r1 in c1
LOOP
IF (r1.salary <= 6500) THEN
UPDATE customers c SET c.salary = (c.salary + 1000) WHERE c.id = r1.id;
-- If id is not primary/unique key, use WHERE CURRENT OF c1
END IF;
COMMIT;
END LOOP;
END;
/
答案 1 :(得分:-1)
DECLARE
c_id customers.id%type := 1;
c_sal customers.salary%type;
BEGIN
SELECT salary
INTO c_sal
FROM customers
where id=c_id;
IF (c_sal <= 6500) THEN
UPDATE customers
SET salary = salary + 1000;
dbms_output.put_line ('Salary updated');
END IF;
END;
/