简体   繁体   English

Oracle 12c程序

[英]Oracle 12c procedure

I have a table like, 我有一张桌子,

Col1    Col2    Col3    Col4
1       abc      111    AAA
2       def      222    BBB
3       dhi      333    CCC

I need to change the column data using a stored procedure and the table should be like this, 我需要使用存储过程更改列数据,并且表应该像这样,

Col1    Col2    Col3    Col4
AAA     111     abc      1
BBB     222     def      2
CCC     333     dhi      3


    col1 data should go to -> Col4
    col2 data should go to  -> Col3
    col3  data should go to -> Col2
    col4  data should go to -> Col1

The procedure will need to run this update statement: 该过程将需要运行以下更新语句:

CREATE PROCEDURE switch_cols IS
BEGIN
  UPDATE t
  SET col4 = col1,
      col3 = col2,
      col2 = col3,
      col1 = col4;
END;
/

Of course, this is assuming all columns are of the same type. 当然,这是假设所有列都属于同一类型。

You need actualy to echnage the content of the column col1 and col4 respective the columns col2 and col3 . 实际上,您需要更改col1col4列以及col2col3列的内容。 You need not to do any DML to achieve this, simple RENAME COLUMN is enough. 您无需执行任何DML即可实现此目的,简单的RENAME COLUMN就足够了。

select Col1, Col2, Col3, Col4 from tab;

      COL1 COL2             COL3 COL4     
---------- ---------- ---------- ----------
         1 abc               111 AAA        
         2 def               222 BBB 


--- exchange col1 <-> col4
alter table TAB rename column col1 TO tmp;
alter table TAB rename column col4 TO col1;
alter table TAB rename column tmp TO col4;
--- exchange col2 <-> col3
alter table TAB rename column col2 TO tmp;
alter table TAB rename column col3 TO col2;
alter table TAB rename column tmp TO col3;

select Col1, Col2, Col3, Col4 from tab; 

COL1             COL2 COL3             COL4
---------- ---------- ---------- ----------
AAA               111 abc                 1 
BBB               222 def                 2 
CCC               333 dhi                 3  

This works even with different column types . 即使不同的列类型也可以使用。

Example of the table before rename: 重命名之前的表示例:

create table tab
(Col1 number,
Col2  varchar2(10),
Col3  number,
Col4  varchar2(10));

and after 之后

CREATE TABLE  TAB 
("COL4" NUMBER, 
"COL3" VARCHAR2(10), 
"COL2" NUMBER, 
"COL1" VARCHAR2(10)
);

The big advantage of this approach is, that it is only metadate update, no real data update, so it is ready in instant time even for large tables. 这种方法的最大优点是,它仅是元数据更新,而没有实际数据更新,因此即使在大型表中也可以立即准备就绪。 The only thing you must consider is, that you can't simple rename col1 to col4 if the table contains the colum col4 already. 你必须考虑的唯一的事情是,你不能简单的重命名col1col4如果表中包含科拉姆col4了。 So you must rename the first column to a temporary name to get the target name free. 因此,您必须将第一列重命名为临时名称,以释放目标名称。

The only side effect is, that the order of theh columns in the table is different. 唯一的副作用是,表中h列的顺序不同。 If this is relevant, simple re-create the table with new name and finally rename the original table to backup and the new table to the productive name: 如果相关,请简单地使用新名称重新创建表,最后将原始表重命名为备份并将新表重命名为生产性名称:

create table tab_new as
select 
col4 col1,
col3 col2,
col2 col3,
col1 col4
from tab;

rename tab to backup;
rename tab_new to tab;

You'll have to transfer existing indexes, constraints, triggers etc. 您必须转移现有的索引,约束,触发器等。

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

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