![](/img/trans.png)
[英]Implicit conversion from data type varchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query
[英]OPENROWSET - Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query
嗨,我正在尝试将存储过程的结果插入到名为MyBinaryTable的表中。 MyBinaryTable包含两列:(FileId [type:int]和BulkColumn [type:varbinary(max)])。 该存储过程返回两列,分别称为FileId和BulkColumn。 当从存储过程中将值插入MyBinaryTable时,出现此错误:
Implicit conversion from data type varchar to varbinary(max) is not allowed.
Use the CONVERT function to run this query.
这是我的存储过程:
CREATE PROCEDURE [dbo].[GenerateBinary] @Route VARCHAR(300), @FileId
VARCHAR(10)
AS
Declare @sql varchar(max)
Set @sql='SELECT convert(varbinary(max),((SELECT BulkColumn FROM OPENROWSET(
BULK ''' + @Route + ''' , SINGLE_BLOB) as Data)), 0) as ''BulkColumn'',''' +
@FileId + ''' as ''FileId'''
Print @sql
Exec(@sql)
这是我插入值的方式:
INSERT INTO MyBinaryTable
EXEC GenerateBinary 'xyz.docx', @FileId = 254
不要养成不良习惯。 您没有在insert语句中指定列列表-这是一个不良习惯,也是导致我们出错的原因。 该过程的结果集为[varbinary(max),varchar(10)]。 这与表的结构相同吗? 不会。您还可以将fileid参数定义为varchar强制进行另一个隐式转换。 因此,更改您的过程以将fileid定义为int并在insert语句中指定列:
insert dbo.MyBinaryTable(BulkColumn, FileId)
select ...;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.