我有一个T1表,并且本月的数据丢失,所以我想通过将“月”列的值更改为2月来复制此表T1中1月的数据。
我只知道月份列的名称。
提前谢谢
答案 0 :(得分:1)
据我所知不能写类似
INSERT INTO someTable(column1,*)
SELECT value, * FROM someTable WHERE ...
您将单个列名与'*'混合在一起
我认为,如果可能的话,这需要分步进行,首先创建一个临时表,并在其中填充所需月份的数据
CREATE GLOBAL TEMPORARY TABLE a
ON COMMIT PRESERVE ROWS
AS
select * from someTable where monthColumn = '201901';
然后更新临时表中的month列
UPDATE a SET monthColumn = '201902';
然后将数据“复制”回原始表
INSERT INTO someTable
SELECT * FROM a;
答案 1 :(得分:0)
类似的东西:
insert into tablename
select col1, col2, 'February', col4
from tablename
where month = 'January' and year = 2019
即您选择“一月”行,但是将“二月”放在“月”列中。
答案 2 :(得分:0)
这有点尴尬,但是-这是一个选项:它从USER_TAB_COLUMNS
中选择列名,并根据它组成INSERT
语句。
测试用例:
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> create table t1 (id number, c_date date, amount number);
Table created.
SQL> insert into t1
2 select 1, date '2019-01-12', 100 from dual union all --> to be copied
3 select 2, date '2019-01-08', 200 from dual union all --> to be copied
4 select 3, date '2018-12-25', 300 from dual;
3 rows created.
SQL> select * from t1 order by id;
ID C_DATE AMOUNT
---------- ---------- ----------
1 12.01.2019 100
2 08.01.2019 200
3 25.12.2018 300
代码:
SQL> declare
2 l_date_column varchar2(30) := 'C_DATE';
3 l_coll varchar2(200);
4 l_str varchar2(200);
5 begin
6 select listagg(column_name, ', ') within group (order by column_id)
7 into l_coll
8 from user_tab_columns
9 where table_name = 'T1';
10
11 l_coll := replace(l_coll, 'C_DATE', 'add_months(C_DATE, 1)');
12
13 l_str := 'insert into t1 select ' || l_coll || ' from t1 ' ||
14 'where trunc(c_date, ''mm'') = date ''2019-01-01''';
15 execute immediate(l_str);
16 end;
17 /
PL/SQL procedure successfully completed.
SQL> select * From t1 order by id;
ID C_DATE AMOUNT
---------- ---------- ----------
1 12.01.2019 100
1 12.02.2019 100
2 08.01.2019 200
2 08.02.2019 200
3 25.12.2018 300
SQL>