简体   繁体   English

PL / SQL MERGE使用集合

[英]PL/SQL MERGE Using Collection

I am having trouble merging a table with a collection. 我在将表格与收藏合并时遇到麻烦。 Let's say I have a table emp. 假设我有一个表emp。

Here is my PL/SQL code snippet. 这是我的PL / SQL代码段。

TYPE empcol is table of emp%ROWTYPE INDEX BY BINARY_INTEGER;
tmpemp empcol;

-- Code here to load data from a CSV file into tmpemp collection  
-- tmpemp(1).emp_id := parsedstring   
-- etc.
MERGE INTO emp A using tmpemp B ON A.emp_id = B.emp_id 
WHEN MATCHED THEN UPDATE SET A.fname = B.fname, A.lname = B.lname 
WHEN NOT MATCHED THEN INSERT (emp_id, fname, lname) VALUES (b.emp_id, b.fname, b.lname);

Compiler doesn't like it. 编译器不喜欢它。 Its throwing ORA-0942 - Table or View doesn't exist. 它抛出ORA-0942-表或视图不存在。 What am I doing wrong? 我究竟做错了什么? or How can I do this better. 或我该如何做得更好。 Thanks a lot for any help you can provide. 非常感谢您提供的任何帮助。

PL/SQL types like emp%ROWTYPE or TABLE OF ... INDEX BY ... cannot be used in SQL queries. 像emp%ROWTYPE或TABLE OF ... INDEX BY ...这样的PL / SQL类型不能在SQL查询中使用。
The type must be declared as SQL type (not as PL/SQL type) to be used in SQL query. 该类型必须声明为SQL类型(而不是PL / SQL类型)才能在SQL查询中使用。

Try this approach (example): 尝试这种方法(示例):

create table emp(
  firstname varchar2(100),
  salary number
);

insert into emp values( 'John', 100 );
commit;

create type my_emp_obj is object(
     firstname varchar2(100),
     salary number
);
/

create type my_emp_obj_table is table of my_emp_obj;
/

declare
  my_emp_tab my_emp_obj_table;
begin
  null;
  my_emp_tab := my_emp_obj_table( my_emp_obj( 'John', 200 ), my_emp_obj( 'Tom', 300 ));

  MERGE INTO emp
  USING ( SELECT * FROM TABLE( my_emp_tab )) src
  ON ( emp.firstname = src.firstname )
  WHEN MATCHED THEN UPDATE SET salary = src.salary
  WHEN NOT MATCHED THEN INSERT VALUES( src.firstname, src.salary );
end;
/

select * from emp;

FIRSTNAME               SALARY
----------------------- ----------
John                           200 
Tom                            300

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

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