简体   繁体   English

Oracle SQL:复制和更新表内的记录

[英]Oracle SQL: Copy and update records within a table

I have a table with 30 records for 'Orders'.我有一张包含 30 条“订单”记录的表格。 I'm trying to我试着

  1. copy these records,复制这些记录,
  2. change several fields to make these records 'Sales' records, and更改几个字段以使这些记录成为“销售”记录,以及
  3. append the updated records to the table. append 更新记录到表中。

There is an ID field that's sequential.有一个连续的 ID 字段。 I'm trying to use the following code which is not working:我正在尝试使用以下不起作用的代码:

INSERT INTO TABLE_01 (
    ID, 
    CONTROL_CODE, 
    CODE, 
    RULE_CODE, 
    CAT, 
    INPUT_VALUES,
    UPDATER,
    UPDATE_TIMESTAMP) 
    SELECT 
        (SELECT MAX(ID) FROM TABLE_01) + 1,
        CONTROL_CODE, 
        CODE, 
        'SALES',
        CAT, 
        INPUT_VALUES,
        'ME',
        SYSDATE
    FROM
        TABLE_01
    WHERE
        ID IN (
            SELECT
                ID
            FROM
                TABLE_01
            WHERE
                RULE_CODE = 'ORDERS');

Any suggestions would be greatly appreciated任何建议将不胜感激

The problem is that this code问题是这段代码

(SELECT MAX(ID) FROM TABLE_01) + 1,

Returns the same value for every row.为每一行返回相同的值。 It doesn't do one insert, run the query again, do the second insert, etc. Instead it runs the query once, and then batch inserts all of the rows that come back.它不会执行一次插入、再次运行查询、执行第二次插入等。而是运行一次查询,然后批量插入所有返回的行。 If you run the select statement by itself, without the insert, you'll see what I mean.如果你自己运行 select 语句,没有插入,你会明白我的意思。 Try this instead:试试这个:

(SELECT MAX(ID) FROM TABLE_01) + rownum,

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

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