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

I have a select 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 problem is what SK.APIKey

is the type varbinary

, but in the stored procedure from the code it is passed as 'sampledata' and so I get the error

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

Can someone tell me how I can solve this?

+3


source to share


1 answer


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

      

+12


source







All Articles