简体   繁体   English

将数据类型Char转换为Nvarchar

[英]Converting datatype Char to Nvarchar

I am currently trying to convert all columns in all tables of my database from char to nvarchar in SQL server 2012. 我目前正在尝试将SQL Server 2012中数据库的所有表中的所有列从char转换为nvarchar。

the query I am trying to run is 我试图运行的查询是

select cast((select COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'char') as nvarchar(MAX))

Not sure if I should be changing the data type through INFORMATION_SCHEMA.COLUMNS but I found this to be the best way to grab the datatype. 不知道我是否应该通过INFORMATION_SCHEMA.COLUMNS更改数据类型,但是我发现这是获取数据类型的最佳方法。

the error I am currently getting is: 我目前收到的错误是:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Is there a better way to accomplish this or a way around this error, as it seems it doesn't like me changing datatypes to multiple rows at the same time. 有没有更好的方法可以解决此问题或解决此错误的方法,因为似乎我不希望同时将数据类型更改为多行。

Selecting from INFORMATION_SCHEMA.COLUMNS is a good way to determine what columns need to be converted, 从INFORMATION_SCHEMA.COLUMNS中进行选择是确定需要转换哪些列的好方法,

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE = 'CHAR';

But it can't be used to directly change a column's data type. 但是它不能用于直接更改列的数据类型。 ALTER TABLE is used to modify column data types: ALTER TABLE用于修改列数据类型:

ALTER TABLE [dbo].[TableName] ALTER COLUMN [ColumnName] NVARCHAR(50);

While you're at it, avoid using NVARCHAR(MAX) unless it's absolutely necessary. 除非绝对必要,否则请避免使用NVARCHAR(MAX) Make sure your data types are sized specifically for the attribute. 确保为属性指定大小的数据类型。 If your CHAR columns are already sized correctly, use the following script to generate the ALTER TABLE statements: 如果您的CHAR列已正确调整大小,请使用以下脚本生成 ALTER TABLE语句:

SELECT  'ALTER TABLE ' +
        QUOTENAME(TABLE_SCHEMA) + '.' +
        QUOTENAME(TABLE_NAME) +
        ' ALTER COLUMN ' +
        QUOTENAME(COLUMN_NAME) +
        ' NVARCHAR(' +
        CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ');'
FROM    INFORMATION_SCHEMA.COLUMNS
WHERE   DATA_TYPE = 'char';

Keep in mind that this only generates the ALTER TABLE statements, you'll need to copy the results and execute in a new tab to change the data types. 请记住,这只会生成ALTER TABLE语句,您需要复制结果并在新选项卡中执行以更改数据类型。

First things first. 首先是第一件事。 You are getting the error because the select returns more than one row and you are trying to cast to nvarchar(MAX) all the column_name. 由于选择返回多个行,并且试图将所有column_name强制转换为nvarchar(MAX),因此出现错误。 Any subquery that you might need to do it in the future must only return one row. 将来可能需要执行的任何子查询都只能返回一行。

select cast((select top 1 COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'char') as nvarchar(MAX))

This will fix the error, but that is not what you are trying to do. 这样可以修复错误,但这不是您要尝试的操作。

To update all the columns you need to do something based on the following command: 要更新所有列,您需要根据以下命令进行操作:

ALTER TABLE {TableName} ALTER COLUMN {ColumnName} NVARCHAR(size) ALTER TABLE {TableName} ALTER COLUMN {ColumnName} NVARCHAR(大小)

What you could do is to get the table_name and column_name from INFORMATION_SCHEMA.COLUMNS and build a dynamic script to update all the columns in the database from char to nvarchar. 您可以做的是从INFORMATION_SCHEMA.COLUMNS获取table_name和column_name并构建一个动态脚本以将数据库中的所有列从char更新为nvarchar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM