简体   繁体   English

PL / SQL循环/批量输出

[英]PL/SQL Loop/bulk output

I'm learning PL/SQL and trying to write some code that would calculate the salary, yearly salary tax and monthly tax of all employees in a table Then essentially output the information for each employee to the screen 我正在学习PL / SQL,并尝试编写一些代码来计算表中所有员工的薪水,年薪税和月税,然后将每个员工的信息基本上输出到屏幕上

I did some research and learned about the bulk into function in plsql. 我进行了一些研究,并了解了plsql中的大部分功能。 I'm currently not in the position to test the code right now, but would this work? 我目前无法立即测试代码,但这行得通吗? Am I missing anything? 我有什么想念的吗?

What I'm really looking for is if there is an easier way to do all of this. 我真正在寻找的是是否有更简单的方法来完成所有这些工作。

SET serveroutput ONDECLARE
TYPE t_employee_test
IS
  TABLE OF employee%ROWTYPE;
  b_firstname T_EMPLOYEE_TEST;
  b_lastname T_EMPLOYEE_TEST;
  b_salary T_EMPLOYEE_TEST;
  yr_salary HR,employees.salary%TYPE;
  name  VARCHAR2(100);
  taxes NUMBER(7,2);
  sal hr.employees.salary%TYPE;
BEGIN
  SELECT salary,
         firstname
                || ' '
                || lastname bulk collect
  INTO   b_salary,
         b_name
  FROM   employees;

  FOR indx IN 1 .. b_name.count
  LOOP
    yr_salary:= B_salary(indx) * 12 taxes := yr_salary(indx) * 0.25;
    csal := L_salary(indx)     * 0.25;
    dbms_output.put_line(‘employee: ‘
    || name
    || ’ monthly salari tax
  IS
    : ‘
    || taxes);
    dbms_output.put_line(‘25 % OF employee: ‘
    || name
    || ’salary
  IS
    : ‘
    || csal);
    dbms_output.put_line(‘employee: ‘
    || name
    || ’ has a yearly salary OF ‘
    || yr_sal);
  END LOOP;
END;

You don't need collections for this task. 您不需要此任务的集合。 It's easier to just use a cursor and print out the needed values, like this: 只需使用游标并打印出所需的值就容易得多,如下所示:

begin
   for r_emp in (select salary * 0.25 salary_tax
                 , first_name || ' ' || last_name name
                 , salary * 12 yearly_salary
                 from employees )
   loop
      dbms_output.put_line(‘employee: ‘ || r_emp.name || ’ monthly salary tax is : ‘ || r_emp.salary_tax);
      dbms_output.put_line(‘employee: ‘ || r_emp.name || ’ has a yearly salary of ‘ || r_emp.yearly_salary);
   end loop;
end;

The only way to know if you code realy works as you think/hope is to try it out. 知道您是否真正编写代码的唯一方法就是尝试/尝试。 Even experienced programmers have code that they think works, and then a bug/error pops up when they try it. 即使是经验丰富的程序员也有他们认为可行的代码,然后在尝试时会弹出错误/错误。 :-) :-)

About you code. 关于您的代码。 If you want to do things for every member in the table you could also make a cursor that fetches the information you want. 如果要对表中的每个成员执行操作,则还可以使一个游标获取所需的信息。 Then you can use a record to grab the information one by one and do somthing with it. 然后,您可以使用一条记录来逐个获取信息并对其进行处理。

Link: Cursor and Records 链接: 光标和记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM