在 SQL Server 中将数据类型 Varchar 更改为 Varbinary(max) [英] Change Data Type Varchar To Varbinary(max) In SQL Server

查看:72
本文介绍了在 SQL Server 中将数据类型 Varchar 更改为 Varbinary(max)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下查询将 SQL Server 中的 varchar 更改为 varbinary(max):

I want to change varchar to varbinary(max) in SQL Server with this query:

ALTER TABLE  [dbo].[Attachments]
ALTER COLUMN [Content]  varbinary(max)  NOT NULL

但这会引发以下异常:

不允许从数据类型 varchar 隐式转换为 varbinary(max).使用 CONVERT 函数运行此查询

Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query

在这种情况下我应该改变什么?

What should I change in this situation ?

推荐答案

您确定要使用 varbinary(max) 吗?如果是这样,我相信您需要分步执行此操作:

Are you sure you want varbinary(max)? If so, I believe you need to do this in steps:

ALTER TABLE Attachments
ADD Content2 varbinary(max)

UPDATE Attachments
SET Content2 = CONVERT(varbinary(MAX),Content)

ALTER TABLE Attachments
DROP COLUMN Content

sp_RENAME 'Attachments.[Content2]' , '[Content]', 'COLUMN'

根据表格的性质,通过选择将其转换为:

Depending on the nature of the table, it might be faster to convert it via a select into:

SELECT Content = CAST(Content AS VARBINARY(MAX))
       ,other fields
INTO NewTable
FROM OldTable

然后删除旧表并重命名新表:

Then drop the old table and rename the new:

DROP TABLE OldTable
GO
SP_RENAME 'NewTable', 'OldTable'

这篇关于在 SQL Server 中将数据类型 Varchar 更改为 Varbinary(max)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆