如何在 SQL Server 中一次更改多个列
我需要ALTER
表中几列的数据类型。
对于单列,以下方法可以正常工作:
ALTER TABLE tblcommodityOHLC
ALTER COLUMN
CC_CommodityContractID NUMERIC(18,0)
但是如何在一个语句中更改多个列?以下不起作用:
ALTER TABLE tblcommodityOHLC
ALTER COLUMN
CC_CommodityContractID NUMERIC(18,0),
CM_CommodityID NUMERIC(18,0)
I need to ALTER
the data types of several columns in a table.
For a single column, the following works fine:
ALTER TABLE tblcommodityOHLC
ALTER COLUMN
CC_CommodityContractID NUMERIC(18,0)
But how do I alter multiple columns in one statement? The following does not work:
ALTER TABLE tblcommodityOHLC
ALTER COLUMN
CC_CommodityContractID NUMERIC(18,0),
CM_CommodityID NUMERIC(18,0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我们可以在单个查询中更改多个列,如下所示:
只需将查询以逗号分隔即可。
We can alter multiple columns in a single query like this:
Just give the queries as comma separated.
如果我正确理解您的问题,您可以使用下面提到的查询在表中添加多列。
询问:
If i understood your question correctly you can add multiple columns in a table by using below mentioned query.
Query:
将 ALTER COLUMN 语句放在括号内,它应该可以工作。
Put
ALTER COLUMN
statement inside a bracket, it should work.使用逗号 (,) 给出所有命令并执行。这里我只是增加了varchar的长度。您可以使用您的方案来修改表的所有列。
Give all commands using comma (,) and execute. Here I am just increasing the length of varchar. You can use your senarios to modify your tables all columns.
感谢 Evan 的代码示例,我能够对它进行更多修改,并使其更具体地针对以特定列名开头的表,并处理约束的细节。我运行了该代码,然后复制了 [CODE] 列并毫无问题地执行了它。
Thanks to Evan's code sample, I was able to modify it more and get it more specific to tables starting with, specific column names AND handle specifics for constraints too. I ran that code and then copied the [CODE] column and executed it without issue.
如果您在 Management Studio 中进行更改并生成脚本,它会创建一个新表,并将旧数据插入具有更改后的数据类型的表中。这是一个更改两列数据类型的小示例
If you do the changes in management studio and generate scripts it makes a new table and inserts the old data into that with the changed data types. Here is a small example changing two column’s data types
如果您不想自己编写整个内容并将所有列更改为相同的数据类型,这可以使事情变得更容易:
您可以复制并粘贴输出作为查询
If you don't want to write the whole thing yourself and change all the columns to the same datatype this can make it easier:
You can copy and paste the output as your query
正如许多其他人所说,您将需要使用多个 ALTER COLUMN 语句,每个语句对应您要修改的每一列。
如果要将表中的所有或多个列修改为相同的数据类型(例如将 VARCHAR 字段从 50 个字符扩展到 100 个字符),您可以使用以下查询自动生成所有语句。如果您想要替换多个字段中的相同字符(例如从所有列中删除 \t),此技术也很有用。
这会为您的每一列生成一个 ALTER TABLE 语句。
As lots of others have said, you will need to use multiple
ALTER COLUMN
statements, one for each column you want to modify.If you want to modify all or several of the columns in your table to the same datatype (such as expanding a VARCHAR field from 50 to 100 chars), you can generate all the statements automatically using the query below. This technique is also useful if you want to replace the same character in multiple fields (such as removing \t from all columns).
This generates an
ALTER TABLE
statement for each column for you.以下解决方案不是用于更改多个列的单个语句,但是,它使生活变得简单:
生成表的
CREATE
脚本。将第一行的
CREATE TABLE
替换为ALTER TABLE [TableName] ALTER COLUMN
从列表中删除不需要的列。
根据需要更改列数据类型。
按如下方式执行查找和替换...:
NULL
,NULL; ALTER TABLE [TableName] ALTER COLUMN
运行脚本。
希望这会节省很多时间:))
The following solution is not a single statement for altering multiple columns, but yes, it makes life simple:
Generate a table's
CREATE
script.Replace
CREATE TABLE
withALTER TABLE [TableName] ALTER COLUMN
for first lineRemove unwanted columns from list.
Change the columns data types as you want.
Perform a Find and Replace… as follows:
NULL
,NULL; ALTER TABLE [TableName] ALTER COLUMN
Run the script.
Hope it will save lot of time :))
正如其他人回答的那样,您需要多个 ALTER TABLE 语句。
尝试以下操作:
As others have answered, you need multiple
ALTER TABLE
statements.Try following:
在单个
ALTER TABLE
语句中执行多个ALTER COLUMN
操作是不可能的。请参阅此处的
ALTER TABLE
语法,您可以执行多个
ADD
或多个DROP COLUMN
,但仅执行一个ALTER
。列
Doing multiple
ALTER COLUMN
actions inside a singleALTER TABLE
statement is not possible.See the
ALTER TABLE
syntax hereYou can do multiple
ADD
or multipleDROP COLUMN
, but just oneALTER
.COLUMN
这是不可能的。您需要一项一项地执行此操作。
您可以:
This is not possible. You will need to do this one by one.
You could: