简体   繁体   English

将列数据类型从Int更改为Ntext,反之亦然

[英]Alter Column Datatype from Int to Ntext and vice-versa

I have a SQL table: 我有一个SQL表:

CREATE TABLE [UserTable]
(
    [ID] [int] NULL,
    [Name] [nvarchar](50) NULL,
    [City] [nvarchar](50) NULL
) ON [PRIMARY]

In this table the ID column has the datatype int . 在此表中,ID列的数据类型为int I want to alter the data type of the ID column to Ntext . 我想将ID列的数据类型更改为Ntext

For that I am using the following Sql Query: 为此,我正在使用以下Sql查询:

ALTER TABLE UserTable
ALTER COLUMN ID NTEXT

This query gives the following error message: 此查询给出以下错误信息:

Msg 206, Level 16, State 2, Line 1 Operand type clash: int is incompatible with ntext 消息206,级别16,状态2,行1操作数类型冲突:int与ntext不兼容

While when I alter ID column Datatype from Int to navarchar it works fine. 当我将ID列的数据类型从Int更改为navarchar时,它可以正常工作。

After that I am trying to Alter ID (now the data type of the Id column is Nvarchar) Column data type to Ntext by Alter Query it alters ID column data type successfully. 在那之后我想改变ID(即现在的ID列的数据类型类型为nvarchar)通过更改查询列的数据类型为ntext它成功地改变ID列的数据类型。

Why can't we alter column data type directly Int to Ntext, while we can do this via INT to Nvarchar after that Nvarchar to Ntext? 为什么不能在将Nvarchar转换为Ntext之后通过INT转换为Nvarchar来直接将Int更改为Ntext呢?

You're trying to do an implicit conversion which is disallowed from int to ntext . 您正在尝试执行从int到ntext的隐式转换。 Why, I don't know, but I guess because it's 2 steps (int->string, string->LOB). 为什么,我不知道,但是我猜是因为这是2个步骤(int-> string,string-> LOB)。

Also, don't use ntext. 另外,不要使用ntext。 It's deprecated. 它被弃用了。 Use nvarchar(max) which should allow implicit conversion. 使用应允许隐式转换的nvarchar(max)。

Finally, if ID is the primary key then why do want a LOB type? 最后,如果ID是主键,那么为什么要使用LOB类型? You do have a primary key...? 有一个主键...?

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

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