[英]OPENROWSET - Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query
Hi I am trying to insert results of a stored procedure into a table called MyBinaryTable. 嗨,我正在尝试将存储过程的结果插入到名为MyBinaryTable的表中。 MyBinaryTable contains two columns: (FileId [type:int], and BulkColumn [type:varbinary(max)]).
MyBinaryTable包含两列:(FileId [type:int]和BulkColumn [type:varbinary(max)])。 The stored procedure returns two columns called FileId and BulkColumn.
该存储过程返回两列,分别称为FileId和BulkColumn。 When Inserting the values into MyBinaryTable from the stored procedure, I am greeted with this error:
当从存储过程中将值插入MyBinaryTable时,出现此错误:
Implicit conversion from data type varchar to varbinary(max) is not allowed.
Use the CONVERT function to run this query.
Here is my stored procedure: 这是我的存储过程:
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)
Here is how I am inserting the values: 这是我插入值的方式:
INSERT INTO MyBinaryTable
EXEC GenerateBinary 'xyz.docx', @FileId = 254
Do not develop bad habits. 不要养成不良习惯。 You did not specify the column list in the insert statement - that is a bad habit and the cause of our error.
您没有在insert语句中指定列列表-这是一个不良习惯,也是导致我们出错的原因。 The resultset of your procedure is [varbinary(max), varchar(10)].
该过程的结果集为[varbinary(max),varchar(10)]。 Is that the same as the structure of your table?
这与表的结构相同吗? No. And you also force another implicit conversion with your fileid argument defined as varchar.
不会。您还可以将fileid参数定义为varchar强制进行另一个隐式转换。 So change your procedure to define fileid as int and specify the columns in the insert statement:
因此,更改您的过程以将fileid定义为int并在insert语句中指定列:
insert dbo.MyBinaryTable(BulkColumn, FileId)
select ...;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.