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


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01