我创建了一个程序,该程序从薪水表中计算所有雇员的工资总额。使用执行语句执行存储过程时出错:出现“无效的SQL语句”,而当我使用PLSQL块执行过程时,则出现了错误”:PLS-00905对象发生HR.PROC_GROSSSALARY无效
-创建存储过程-
create or replace procedure proc_grosssalary
AS
begin
select s.*,(Basic+HRA+DA+CA+Medical) Gross_Salary from salary s;
end;
-使用EXECUTE调用SP-
execute proc_grosssalary;
-使用PLSQL块调用SP-
begin
proc_grosssalary;
end;
以表格结构形式显示工资表中具有已计算的Gross_Salary的所有数据
答案 0 :(得分:0)
您的PL / SQL语法不正确
create or replace procedure proc_grosssalary (out gross_salary number)
AS
begin
select (Basic+HRA+DA+CA+Medical)
into gross_salary
from salary s;
end;
答案 1 :(得分:0)
您需要使用游标,以便可以循环访问表中的记录。
这就是我所做的-
#create a table for sample data
create table tsalary0918(Basic number,HRA number,DA number,CA number,Medical number)
#inserting sample data
insert into tsalary0918 values (100,100,100,100,100)
#checking if data is inserted
select * from tsalary0918;
#query output
BASIC HRA DA CA MEDICAL
100 100 100 100 100
#create a stored procedure
create or replace procedure testproc
AS
cursor cur is select s.*,(Basic +HRA + DA +CA +Medical ) Gross_salary from tsalary0918 s;
begin
for records in cur
loop
dbms_output.put_line('DA is ' || records.DA);
dbms_output.put_line('Gross Salary is ' || records.Gross_salary);
end loop;
end;
/
#execute the stored procedure
begin
testproc;
end;
/
#output of the above execution
dbms_output:
DA is 100
Gross Salary is 500
在线检查-https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1744281398fe62a6cb42250641947249
答案 2 :(得分:0)
您还应该考虑创建视图的选项。
Create or replace view v_grosssalary
as select s.*,(Basic+HRA+DA+CA+Medical) Gross_Salary
from salary s;
,只需执行select * from v_grosssalary
即可获得输出。
对于过程,还有一个PRINT
绑定变量的CURSOR
命令(在SQL * Plus和SQL Developer中有效)
VARIABLE x REFCURSOR
create or replace procedure proc_grosssalary
AS
begin
OPEN cursor :x for
select s.*,(Basic+HRA+DA+CA+Medical) Gross_Salary from salary s;
end;
/
Print x -- This will display the required result.
答案 3 :(得分:0)
但是。.您想做什么?如果要计算每个员工的薪水,并且将信息放在同一行中,则可以使用简单的SQL来获取信息,如下所示:
Select id_emp, name_emp, surname_emp, (Basic+HRAs+DA+CA+Medical) as salary
from salary;
这将比具有奇怪的功能和过程的速度更快。如果您不喜欢它,请创建类似这样的视图,然后您将获得一个列有薪水的列,更简洁,更清晰
答案 4 :(得分:0)
任务已完成:
create or replace procedure p1
IS
cursor c1 is select s.*,(Basic+HRA+CA+DA+Medical) Gross_Salary from salary s;
emp_rec c1%rowtype;
begin
open c1;
dbms_output.put_line('Emp_No Basic HRA Gross_Salary');
dbms_output.put_line('---------------------------------------');
loop
fetch c1 into emp_rec;
exit when c1%notfound;
dbms_output.put_line(emp_rec.employee_number||' '||emp_rec.Basic||' '||emp_rec.HRA||' '||' '||emp_rec.Gross_Salary);
end loop;
close c1;
end;
-使用PLSQL块执行过程-
begin
p1;
end;
预期如下最终结果:
----------------------------------------------
Employee_number Basic HRA Gross_salary
----------------------------------------------
1 25000 10000 36750
2 7000 2800 11650
3 10000 4000 15950