简体   繁体   中英

Alter Column Datatype from Int to Ntext and vice-versa

I have a SQL table:

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 . I want to alter the data type of the ID column to Ntext .

For that I am using the following Sql Query:

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

While when I alter ID column Datatype from Int to navarchar it works fine.

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.

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?

You're trying to do an implicit conversion which is disallowed from int to ntext . Why, I don't know, but I guess because it's 2 steps (int->string, string->LOB).

Also, don't use ntext. It's deprecated. Use nvarchar(max) which should allow implicit conversion.

Finally, if ID is the primary key then why do want a LOB type? You do have a primary key...?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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