SQL 将一列复制到 oracle db 中同一表中的另一列。我需要指定哪些数据去哪里吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24740745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy a column to another column within a same table in oracle db. Do I need to specify which data goes where?
提问by bn00d
I want to change the datatype (varchar2 to number) of a column in an oracle table and the column is not empty. So what I thought I will do is, create a new column, copy the data from one column to another column. Disable/Drop the previous column and rename the new column.
我想更改 oracle 表中列的数据类型(varchar2 到 number),并且该列不为空。所以我想我会做的是,创建一个新列,将数据从一列复制到另一列。禁用/删除前一列并重命名新列。
To copy data between the same columns, I can use:
要在同一列之间复制数据,我可以使用:
UPDATE TABLE_NAME SET NEW_COLUMN = TO_NUMBER(OLD_COLUMN);
But what I want to confirm before doing this is, do I need to specify which row's data goes where? Or it will be copied to its adjacent row in the column? What I meant is, do I need to do something like:
但在此之前我想确认的是,我是否需要指定哪一行的数据去哪里?或者它会被复制到列中的相邻行?我的意思是,我是否需要执行以下操作:
UPDATE (SELECT TO_NUMBER(OLD_COLUMN) AS OLDISH, NEW_COLUMN AS NEWISH FROM TABLE_NAME A, TABLE_NAME B WHERE A.ID = B.ID) SET NEWISH = OLDISH;
回答by ForguesR
The operation will be done on the same row for each rows.
该操作将在每一行的同一行上完成。
Be aware that if you do not want to update all the table rows then you need to add a WHERE
clause.
请注意,如果您不想更新所有表行,则需要添加一个WHERE
子句。
回答by 9000
UPDATE foo_table SET some_column = another_column
uses some_column
and another_column
of the same row. But note that it does so on every row of foo_table
; make sure it's what you want.
UPDATE foo_table SET some_column = another_column
使用some_column
和another_column
同一行。但请注意,它在foo_table
; 的每一行都这样做。确保它是你想要的。