How to insert a value into a nested table without losing data in that table?(如何将值插入嵌套表而不丢失该表中的数据?)
问题描述
如何将新数据插入到现有行的嵌套表中?例如,我已经定义了
How can I insert new data into an existing row's nested table? For example, I have defined
CREATE OR REPLACE TYPE businessTableForCategories AS TABLE OF VARCHAR(128);
/
CREATE TABLE Category (
name VARCHAR(128) PRIMARY KEY,
businesses businessTableForCategories
) NESTED TABLE businesses STORE AS categoryBusinessTable;
假设在类别中有一个条目,其中 name = 'Restaurant'
和 businesses = businessTableForCategories('xzqpehc234ajdpa8')
.
Say in Category there is an entry with name = 'Restaurant'
and businesses = businessTableForCategories('xzqpehc234ajdpa8')
.
如何在不删除条目或丢失嵌套表中存储的数据的情况下,为 Category 中的该条目插入新数据到嵌套表中?
How can I insert new data into that nested table for that entry in Category without removing the entry, or losing the data stored in the nested table?
我之所以这么问,是因为我尝试插入的其中一个条目需要一个长度为 25137 个字符的插入语句,这超出了 Oracle 对单个命令的限制.这是因为该类别中有许多企业.我想创建类别,然后将业务一个一个(或者可能是小分组)插入到嵌套表业务"中.
I ask because one of the entries I am trying to insert requires an insert statement that is 25137 characters long, which is way past Oracle's limit for a single command. This is because there are many businesses in the category. I would like to create the category, and then insert the businesses one by one (or maybe small groupings) into the nested table "businesses".
推荐答案
使用 MULTISET UNION [ALL|DISTINCT]
运算符:
SQL 小提琴
Oracle 11g R2 架构设置:
CREATE OR REPLACE TYPE businessTableForCategories AS TABLE OF VARCHAR(128);
/
CREATE TABLE Category (
name VARCHAR(128) PRIMARY KEY,
businesses businessTableForCategories
) NESTED TABLE businesses STORE AS categoryBusinessTable
/
INSERT INTO Category VALUES (
'Restaurant',
businessTableForCategories('xzqpehc234ajdpa8')
)
/
UPDATE Category
SET businesses = businesses
MULTISET UNION ALL
businessTableForCategories('other_value')
WHERE name = 'Restaurant'
/
查询 1:
SELECT *
FROM category
结果:
| NAME | BUSINESSES |
|------------|------------------------------|
| Restaurant | xzqpehc234ajdpa8,other_value |
查询 2:
或者使用绑定变量在查询中包含集合:
Or use a bind variable to include the collection in the query:
DECLARE
businesses businessTableForCategories := businessTableForCategories();
BEGIN
businesses.EXTEND( 10000 );
FOR i IN 1 .. 10000 LOOP
businesses(i) := DBMS_RANDOM.STRING( 'x', 128 );
END LOOP;
INSERT INTO Category VALUES ( 'lots of data', businesses );
END;
查询 3:
SELECT name, CARDINALITY( businesses )
FROM Category
结果:
| NAME | CARDINALITY(BUSINESSES) |
|--------------|-------------------------|
| lots of data | 10000 |
| Restaurant | 2 |
这篇关于如何将值插入嵌套表而不丢失该表中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将值插入嵌套表而不丢失该表中的数据?
基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01