转换为过程 pl/sql
convert to PROCEDURE pl/sql
我想编写一个程序,首先打印雇员的雇员编号和工资(即 7839)。然后它将根据以下条件增加员工7839的工资(这将是table员工中的员工编号):
Condition-1: If experience is more than 10 years, increase salary by 20%.
Condition-2: If experience is greater than 5 years, increase salary by 10%.
Condition-3: All others will get an increase of 5% in the salary.
程序会打印加薪前后的员工编号和薪水 我尝试了以下步骤但不确定它有多准确..
我需要将其转换为过程代码。
请指教
DECLARE
veno emp.empno%type:=&veno;
vsal emp.sal%type;
vexp number;
BEGIN
select empno,sal,trunc(to_char(months_between(sysdate,hiredate)/12))into veno,vsal,vexp from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('before update:' ||chr(10)||veno||chr(10)||vsal);
if vexp>=10 then
update emp set sal=sal+(sal*.20) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
elsif vexp>=5 then
update emp set sal=sal+(sal*.10) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
else
update emp set sal=sal+(sal*.05) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
end if;
END;
/
您只需将 DECLARE
(表示匿名块的开始)更改为 CREATE PROCEDURE
,并将您当前通过替换变量设置的变量作为正式参数;所以而不是:
DECLARE
veno emp.empno%type:=&veno;
vsal emp.sal%type;
vexp number;
BEGIN
...
END;
/
成功:
CREATE OR REPLACE PROCEDURE my_proc (veno IN emp.empno%type)
AS
vsal emp.sal%type;
vexp number;
BEGIN
...
END;
/
然后您可以从匿名块调用它,或者在 SQL*Plus 或 SQL 开发人员中使用 execute
shorthand:
set serveroutput on
execute my_proc(&veno);
此示例仍在使用替换变量,因此您将被提升为要使用的值,但您也可以直接传递一个数字。
Read more about creating procedures and the types of parameters.
您可以大大简化代码以减少重复和重新查询;查找 case 表达式和返回子句。但这并不直接相关。
我想编写一个程序,首先打印雇员的雇员编号和工资(即 7839)。然后它将根据以下条件增加员工7839的工资(这将是table员工中的员工编号):
Condition-1: If experience is more than 10 years, increase salary by 20%.
Condition-2: If experience is greater than 5 years, increase salary by 10%.
Condition-3: All others will get an increase of 5% in the salary.
程序会打印加薪前后的员工编号和薪水 我尝试了以下步骤但不确定它有多准确..
我需要将其转换为过程代码。
请指教
DECLARE
veno emp.empno%type:=&veno;
vsal emp.sal%type;
vexp number;
BEGIN
select empno,sal,trunc(to_char(months_between(sysdate,hiredate)/12))into veno,vsal,vexp from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('before update:' ||chr(10)||veno||chr(10)||vsal);
if vexp>=10 then
update emp set sal=sal+(sal*.20) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
elsif vexp>=5 then
update emp set sal=sal+(sal*.10) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
else
update emp set sal=sal+(sal*.05) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
end if;
END;
/
您只需将 DECLARE
(表示匿名块的开始)更改为 CREATE PROCEDURE
,并将您当前通过替换变量设置的变量作为正式参数;所以而不是:
DECLARE
veno emp.empno%type:=&veno;
vsal emp.sal%type;
vexp number;
BEGIN
...
END;
/
成功:
CREATE OR REPLACE PROCEDURE my_proc (veno IN emp.empno%type)
AS
vsal emp.sal%type;
vexp number;
BEGIN
...
END;
/
然后您可以从匿名块调用它,或者在 SQL*Plus 或 SQL 开发人员中使用 execute
shorthand:
set serveroutput on
execute my_proc(&veno);
此示例仍在使用替换变量,因此您将被提升为要使用的值,但您也可以直接传递一个数字。
Read more about creating procedures and the types of parameters.
您可以大大简化代码以减少重复和重新查询;查找 case 表达式和返回子句。但这并不直接相关。