在SQL Server中将数据类型Varchar更改为Varbinary(max)

我想使用此查询sql Server中将varchar更改为varbinary(max):
ALTER TABLE  [dbo].[Attachments]
ALTER COLUMN [Content]  varbinary(max)  NOT NULL

但是这会引发以下异常:

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

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

解决方法

你确定要varbinary(最大)吗?如果是这样,我相信您需要按步骤执行此操作:
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'

根据表的性质,通过select转换为以下内容可能会更快:

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

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

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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...