如何以Collection变量作为数据源来向数据表中插入数据,或者更新数据。
2007-12-06 14:30
387 查看
前面我已经告诉大家如何在Oracle中向Collection类型的变量中逐条插入数据, 如何在Oracle中修改Collection类型的变量。现在要变被动为主动,如何以Collection为数据源来处理真实数据表中的数据。
插入:
--Bulk insert into table from an array.
insert into department_teststruct
select * from table(cast(d2 as dept_array));
更新:
UPDATE Table1 O
SET Col2 = NVL((select Col2
from table(p_inventory_array_in) T
WHERE O.Col1 = T.Col1),
Col2);
不能使用下面的方法来更新
UPDATE Table1 -- Error: PL/SQL: SQL Statement ignored
SET Col2 = T. Col2 FROM Table1 O, (SELECT * FROM TABLE(p_array_in)) T --Error: PL/SQL: ORA-00933: SQL command not properly ended
WHERE O. Col1 = T. Col1;
我们可以使用两个数租来进行更新,
forall i in 1..array1.Count
update table1
set col2 = array2(i)
where col1 = array1(i);
这个方法是使用Collection进行更新的方法中最快的一种。因为这种方法会减少PL/SQL引擎和SQL引擎之间的交互内容。不能使用下面的代码: array(i).col1_value,只能使用:array(i) 。
原因是:Bind variables cannot be passed with offset addresses from another bind variable's base address.
当然这样处理的不足就是需要更新所有的数据,我们也可以循环Collection的数据来进行处理。以后我会测试一下性能问题,反馈给大家。
Reference:
How to update or sort data in collection?
How to pass arrays to plsql from OAF
插入:
--Bulk insert into table from an array.
insert into department_teststruct
select * from table(cast(d2 as dept_array));
更新:
UPDATE Table1 O
SET Col2 = NVL((select Col2
from table(p_inventory_array_in) T
WHERE O.Col1 = T.Col1),
Col2);
不能使用下面的方法来更新
UPDATE Table1 -- Error: PL/SQL: SQL Statement ignored
SET Col2 = T. Col2 FROM Table1 O, (SELECT * FROM TABLE(p_array_in)) T --Error: PL/SQL: ORA-00933: SQL command not properly ended
WHERE O. Col1 = T. Col1;
我们可以使用两个数租来进行更新,
forall i in 1..array1.Count
update table1
set col2 = array2(i)
where col1 = array1(i);
这个方法是使用Collection进行更新的方法中最快的一种。因为这种方法会减少PL/SQL引擎和SQL引擎之间的交互内容。不能使用下面的代码: array(i).col1_value,只能使用:array(i) 。
原因是:Bind variables cannot be passed with offset addresses from another bind variable's base address.
当然这样处理的不足就是需要更新所有的数据,我们也可以循环Collection的数据来进行处理。以后我会测试一下性能问题,反馈给大家。
Reference:
How to update or sort data in collection?
How to pass arrays to plsql from OAF
相关文章推荐
- hibernate中如何向关联表中插入数据,或者更新关联表数据
- Oracle中merge into的使用:该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据.
- 如何从一个表的数据更新到另外一个表的列或者插入
- 如何在Oracle中向Collection类型的变量中逐条插入数据。
- 如何提高Oracle存储过程批量插入或者更新数据的性能
- [SQL]如何使用MERGE方法有效地插入或更新数据
- 表变量及临时表数据批量插入和更新 写法
- mysql如何在一个表中插入数据的同时,更新另一个表的数据?
- 如何恢复oracle误删除、更新、插入的数据
- 如何利用存储过程插入更新IMAGE数据类型
- ASP.net(C#)]用DataSet进入对数据源的插入数据、更新数据及刪除数据
- oracle 如何用触发器实现更新刚插入的数据
- oracle 如何用触发器实现更新刚插入的数据集合
- 将A表中的数据更新或插入到B表中 id作为关联
- 关于ssh javaweb 项目插入或者更新数据后 mysql 数据乱码的问题
- Postgresql存储过程--更新或者插入数据
- 提升mysql大数据插入或者更新数据条目的执行效率
- Magento-如何新增、更新或者删除数据库中的数据
- sqlalchemy 插入或者更新数据失败 UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: o
- JDBC对数据进行插入,更新或者是删除之后得到修改或者插入的那条记录的主键值