简体   繁体   English

Oracle SQL,单插入语句,多列

[英]Oracle sql, single insert statement, multi columns

Few weeks ago I saw an article about oracle sql optimization. 几周前,我看到了有关oracle sql优化的文章。 There was some way to insert fe 200 rows in one INSERT statement, but it was not INSERT ALL clause like: 有一种方法可以在一条INSERT语句中插入fe 200行,但不是INSERT ALL子句,例如:

INSERT ALL
INTO sales (prod_id, cust_id, time_id, amount)
VALUES (product_id, customer_id, weekly_start_date, sales_sun)
INTO sales (prod_id, cust_id, time_id, amount)
VALUES (product_id, customer_id, weekly_start_date+1, sales_mon)
SELECT product_id, customer_id, weekly_start_date, sales_sun,
sales_mon, sales_tue, sales_wed, sales_thu, sales_fri, sales_sat
FROM sales_input_table;

I am pretty sure (99%) it has been done with USING Clause in PL/SQL(?). 我很确定(99%)已经在PL / SQL(?)中USING子句完成了。 Main problem was to "save" one insert statement and executing it 200 times instead of "saving" 200 inserts and executing every of each of them. 主要问题是“保存”一个插入语句并执行200次,而不是“保存” 200个插入并执行每个插入。

I tried to search in Google but always INSERT ALL came up. 我尝试在Google中搜索,但总是出现INSERT ALL Anyone have an idea or have similar article about "this"? 有人对“ this”有想法或类似文章吗? Thanks for help. 感谢帮助。

I would generate a bunch of numbers and do this in a single insert . 我会生成一堆数字,然后一次insert This version uses union all to handle each of the weekday columns: 此版本使用union all处理每个工作日列:

with n as (
     select level as n
     from dual
     connect by level <= 52
    )
insert into sales (prod_id, cust_id, time_id, amount)
    select product_id, customer_id, weekly_start_date + n.n - 1, sales_sun
    from sales_input_table cross join n
    union all
    select product_id, customer_id, weekly_start_date + n.n - 1, sales_mon
    from sales_input_table cross join n
    . . .

What you have seen is (sorry... MAY HAVE BEEN - see my comment to your original question though) the insert into (select ...) syntax. 您所看到的是(对不起...可能已经过了-尽管请参阅我对原始问题的评论)插入(select ...)语法。 For example, suppose I have a table t, and I want to clone it. 例如,假设我有一个表t,我想克隆它。 I create a table s with the same number, name and data type (and order) of columns at t, and now I want to copy all the data from t to s. 我创建了一个表s,该表s在t处具有相同的列数,名称和数据类型(和顺序),现在我想将所有数据从t复制到s。 The simple way to do that is 简单的方法是

insert into s (select * from t);

What's in parentheses (the subquery) can be a full-blown subquery; 括号(子查询)中的内容可以是完整的子查询; for example, I can use a WHERE clause to limit which rows are copied to s; 例如,我可以使用WHERE子句来限制将哪些行复制到s; or I can use aggregate functions and GROUP BY in the subquery to insert summaries in s, instead of cloning the data; 或者我可以在子查询中使用聚合函数和GROUP BY在s中插入摘要,而不是克隆数据; the only condition is that the number and data type of resulting columns in the subquery must match those of s. 唯一的条件是,子查询中结果列的数量和数据类型必须与s相匹配。

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

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