我想使用以下查询将 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(max) 吗?如果是这样,我相信您需要分步骤执行此操作:
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 Content = CAST(Content AS VARBINARY(MAX))
,other fields
INTO NewTable
FROM OldTable
然后删除旧表并重命名新表:
DROP TABLE OldTable
GO
SP_RENAME 'NewTable', 'OldTable'
关于sql - 在 SQL Server 中将数据类型 Varchar 更改为 Varbinary(max),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135157/