更改列的数据类型

时间:2022-11-30 22:48:18

I need to alter the data type of a column in one table of mine, but this table contains data my problem is that I can't empty the table to do my purpose so I need to Alter the column data type without emptying the table.

我需要在我的一个表中更改列的数据类型,但是这个表包含数据我的问题是我无法清空表以实现我的目的所以我需要更改列数据类型而不清空表。

What can I do ?

我能做什么 ?

2 个解决方案

#1


2  

Your best choice here is to create a new table new_table with the appropriate columns.

这里您最好的选择是使用适当的列创建一个新表new_table。

Once created, insert into this table the values of the existing table table as you want them. Only then reintroduce constraints etc (if you do this before insert, you'll penalize insert performance).

创建后,根据需要将现有表格的值插入此表中。只有这样才能重新引入约束等(如果你在插入之前这样做,你会惩罚插入性能)。

Once done, rename table to old_table and new_table to table.

完成后,将表重命名为old_table,将new_table重命名为table。

Of course, you need to do this with your application offline.

当然,您需要离线执行此应用程序。

#2


0  

have you tried Alter Table

你试过Alter Table吗?

ALTER TABLE table_name
MODIFY column_name column_type;

The data type of text, ntext and image columns can be changed only in the following ways:

text,ntext和image列的数据类型只能通过以下方式更改:

  • text to varchar(max), nvarchar(max), or xml
  • text到varchar(max),nvarchar(max)或xml

  • ntext to varchar(max), nvarchar(max), or xml
  • ntext到varchar(max),nvarchar(max)或xml

  • image to varbinary(max)
  • 图像到varbinary(最大)

For other conversions you will have to do casting. Refer http://technet.microsoft.com/en-us/library/ms187928.aspx for casting

对于其他转换,您必须进行投射。请参阅http://technet.microsoft.com/en-us/library/ms187928.aspx进行投射

#1


2  

Your best choice here is to create a new table new_table with the appropriate columns.

这里您最好的选择是使用适当的列创建一个新表new_table。

Once created, insert into this table the values of the existing table table as you want them. Only then reintroduce constraints etc (if you do this before insert, you'll penalize insert performance).

创建后,根据需要将现有表格的值插入此表中。只有这样才能重新引入约束等(如果你在插入之前这样做,你会惩罚插入性能)。

Once done, rename table to old_table and new_table to table.

完成后,将表重命名为old_table,将new_table重命名为table。

Of course, you need to do this with your application offline.

当然,您需要离线执行此应用程序。

#2


0  

have you tried Alter Table

你试过Alter Table吗?

ALTER TABLE table_name
MODIFY column_name column_type;

The data type of text, ntext and image columns can be changed only in the following ways:

text,ntext和image列的数据类型只能通过以下方式更改:

  • text to varchar(max), nvarchar(max), or xml
  • text到varchar(max),nvarchar(max)或xml

  • ntext to varchar(max), nvarchar(max), or xml
  • ntext到varchar(max),nvarchar(max)或xml

  • image to varbinary(max)
  • 图像到varbinary(最大)

For other conversions you will have to do casting. Refer http://technet.microsoft.com/en-us/library/ms187928.aspx for casting

对于其他转换,您必须进行投射。请参阅http://technet.microsoft.com/en-us/library/ms187928.aspx进行投射