简体   繁体   English

将 SQL_Variant 列修改为 VARCHAR

[英]Modify SQL_Variant Column to VARCHAR

I have a table where one of the columns has a sql_variant datatype.我有一个表,其中一列具有sql_variant数据类型。 I'm trying to modify the column to VarChar(800) but I'm getting an error:我正在尝试将列修改为VarChar(800)但出现错误:

Implicit conversion from data type sql_variant to varchar is not allowed.不允许从数据类型 sql_variant 到 varchar 的隐式转换。 Use the CONVERT function to run this query使用 CONVERT function 运行此查询

I tried using the Convert statement in the Alter statement but I'm getting an incorrect syntax error.我尝试在Alter语句中使用Convert语句,但出现了不正确的语法错误。 I would really appreciate it if someone can shed some light on how this problem can be resolved.如果有人能阐明如何解决这个问题,我将不胜感激。 Thank you!谢谢!

Script:脚本:

ALTER TABLE dbo.tmpEmployee
ALTER COLUMN bigVal Varchar(800)

bigVal is the column of sql_variant datatype. bigValsql_variant数据类型的列。

This is the error:这是错误:

Msg 257, Level 16, State 3, Line 5消息 257,第 16 层,State 3,第 5 行
Implicit conversion from data type sql_variant to varchar is not allowed.不允许从数据类型 sql_variant 到 varchar 的隐式转换。 Use the CONVERT function to run this query.使用 CONVERT function 运行此查询。

I'm afraid this is not an easy task, if the order of column doesn't matter, just add a new column as varchar(800) and try to insert data from sql_varient to that column, but you have to be careful for different type of data in that column to use right casting format for example for dates and binary values恐怕这不是一件容易的事,如果列的顺序无关紧要,只需添加一个新列作为 varchar(800) 并尝试将数据从 sql_varient 插入该列,但你必须小心不同该列中的数据类型以使用正确的转换格式,例如日期和二进制值

try this query试试这个查询

UPDATE dbo.tmpEmployee
          SET bigVal = CONVERT(varchar(800),bigVal) 
          FROM tmpEmployee

ALTER TABLE dbo.tmpEmployee
ALTER COLUMN bigVal Varchar(800)

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

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