Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the below select statement from a stored procedure:

ALTER PROCEDURE [dbo].[Test]  
   --Params 
   @SolutionId   INT 
   ,@APIKey    varbinary(256)   
AS  
   SELECT 
       SK.SolutionID        
       ,SK.APIKey   
       ,SK.Enabled
    FROM    
       dbo.SolutionKey SK
    WHERE
       SK.SolutionID = @SolutionId 
       AND SK.APIKey = @APIKey 
       AND Enabled = 1

The issue is that SK.APIKey is a varbinary datatype but in the stored procedure from the code it is passed on as 'sampledata' and so I get the error

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

Can someone please tell me how can I resolve this?

like image 228
user505210 Avatar asked Nov 18 '14 18:11

user505210


People also ask

Can we convert varchar to varbinary in SQL Server?

Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query. The problem is that the query plan hashes are already in binary format, however stored as VARCHAR in the XML Query Plan e.g.

What is Varbinary in SQL?

varbinary [ ( n | max ) ]Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length.


1 Answers

Something like this might work.

ALTER PROCEDURE [dbo].[Test]  
--Params 
@SolutionId   INT 
,@APIKey    varchar(256)   
AS  

SELECT 
           SK.SolutionID        
          ,SK.APIKey    
          ,SK.Enabled
FROM    dbo.SolutionKey SK
where SK.SolutionID = @SolutionId 
  And SK.APIKey = CONVERT(VARBINARY(256), @APIKey, 1) 
  And Enabled = 1
like image 76
bowlturner Avatar answered Nov 09 '22 00:11

bowlturner