Oracle SQL to change column type from number to varchar2 while it contains data(Oracle SQL 在包含数据时将列类型从 number 更改为 varchar2)
问题描述
我在 Oracle 11g 中有一个表(包含数据),我需要使用 Oracle SQLPlus 来执行以下操作:
I have a table (that contains data) in Oracle 11g and I need to use Oracle SQLPlus to do the following:
目标:将UDA1
表中TEST1
列的类型从number
改为varchar2
.
Target: change the type of column TEST1
in table UDA1
from number
to varchar2
.
建议的方法:
- 备份表
- 将列设置为空
- 更改数据类型
- 恢复值
以下方法无效.
create table temp_uda1 AS (select * from UDA1);
update UDA1 set TEST1 = null;
commit;
alter table UDA1 modify TEST1 varchar2(3);
insert into UDA1(TEST1)
select cast(TEST1 as varchar2(3)) from temp_uda1;
commit;
与索引有关(以保持顺序),对吗?
There is something to do with indexes (to preserve the order), right?
推荐答案
create table temp_uda1 (test1 integer);
insert into temp_uda1 values (1);
alter table temp_uda1 add (test1_new varchar2(3));
update temp_uda1
set test1_new = to_char(test1);
alter table temp_uda1 drop column test1 cascade constraints;
alter table temp_uda1 rename column test1_new to test1;
如果列上有索引,您需要重新创建它.
If there was an index on the column you need to re-create it.
请注意,如果旧列中的数字大于 999,则更新将失败.如果这样做,则需要调整 varchar
列的最大值
Note that the update will fail if you have numbers in the old column that are greater than 999. If you do, you need to adjust the maximum value for the varchar
column
这篇关于Oracle SQL 在包含数据时将列类型从 number 更改为 varchar2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle SQL 在包含数据时将列类型从 number 更改为 varchar2


基础教程推荐
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01