简体   繁体   English

批量收集和转发-Oracle

[英]Bulk Collect and ForAll - Oracle

I am looking for the syntax to access a column from the Record Type (Index by table). 我正在寻找从记录类型(按表索引)访问列的语法。 Below is the sample code. 下面是示例代码。 How shall i run the Update script in below Declare block which need empid from the V_Emprec record type. 我应该如何在下面的Declare块中运行需要更新V_Emprec记录类型的更新脚本。 I have created a proc also which needs same parameter(empid). 我创建了一个proc,它也需要相同的parameter(empid)。

Can this be done using %Rowtype or i need to create type with emp_stage.empid%type? 可以使用%Rowtype完成此操作,还是我需要使用emp_stage.empid%type创建类型? If i create 2 TYPES for Empid and Ename as emp_stg.column_name%type, can i use those to replace the Insert script using Rowtype v_emprec? 如果我为Empid和Ename创建2个TYPES作为emp_stg.column_name%type,是否可以使用Rowtype v_emprec替换那些插入脚本?

Please tell the syntax to do this. 请告诉语法来做到这一点。

create table emp_master(empid number, ename varchar2(50));
create table emp_stage (empid number, ename varchar2(50));
create procedure update_emp_name(P_empid in emp_master.empid%type)
is
begin
Update emp_stage set ename =INITCAP(ename) WHERE EMPID =P_empid;
commit;
end;

Declare
Type emprec is table of emp_master%rowtype index by pls_integer;
v_emprec emprec;
Begin
Select empid,ename bulk collect into v_emprec from emp_master;
ForAll i in 1..v_emprec.count
Insert into emp_stage values v_emprec(i);
Update emp_stage set ename =INITCAP(ename) WHERE EMPID =v_emprec.empid(i);
 /*Need Correct Syntax to use empid from the v_emprec type*/           
update_emp_name(); 
commit;
End;

Thanks 谢谢

You can do the update in a second forall , and reference the record field as you would any record field or table column; 您可以在第二个forall进行更新,并像forall任何记录字段或表列一样引用记录字段; you just have the index reference in the wrong place: 您只是将索引引用放在错误的位置:

ForAll i in 1..v_emprec.count
  Insert into emp_stage values v_emprec(i);
ForAll i in 1..v_emprec.count
  Update emp_stage set ename = INITCAP(ename)
  WHERE EMPID = v_emprec(i).empid;

You can't call a procedure with forall , it only allows DML. 您不能使用forall调用过程,它仅允许DML。 You can use a normal for loop though: 您可以使用普通的for循环:

for i in 1..v_emprec.count loop         
  update_emp_name(v_emprec(i).empid);
end loop;

But as that does single row-by-row updates, and incurs extra context switches, that will be less efficient than the forall approach; 但是这样做会导致单行更新,并引起额外的上下文切换,这将比forall方法效率低; or indeed a single update of all the rows. 或实际上是所有行的一次更新。 You can also loop around the collection and initcap the fields before doing the insert: 您还可以在执行插入操作之前,在集合周围循环并使用initcap字段:

for i in 1..v_emprec.count loop         
  v_emprec(i).ename := INITCAP(v_emprec(i).ename);
end loop;
ForAll i in 1..v_emprec.count
  Insert into emp_stage values v_emprec(i);

Or change the insert to refer to the fields separately and do the initcap during the insert (which also won't work with 10g). 或更改插入内容以分别引用这些字段,并在插入过程中执行initcap(这对于10g也无效)。 Or do the initcap in the query: 或在查询中执行initcap:

Select empid, INITCAP(ename) bulk collect into v_emprec from emp_master;
ForAll i in 1..v_emprec.count
  Insert into emp_stage values v_emprec(i);

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

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