Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a varbinary type conversion on a variable in a dynamic query. I deal with dynamic query errors very well, and I know something is wrong while converting nvarchar(max) and varbinary(max) in a dynamic query.
But I am not able to find its solution.
SQL
DECLARE @Oldlogo	varbinary(max)
SET @Oldlogo = 'some binary code'
declare @query nvarchar(max)
SET @query = ''
SET @query = @query + '  IF EXISTS(SELECT 1 FROM  Company WHERE CONVERT(varbinary,Company_Logo_TopLeft) = CONVERT(varbinary,''' + @Oldlogo + ''')) '
	SET @query = @query + '  BEGIN '
	SET @query = @query + ' Do someting'	
       SET @query = @query + '   END '
SET @query = @query + '   ELSE'
       SET @query = @query + '  BEGIN '
	SET @query = @query + ' Do someting else'	
	SET @query = @query + '   END '
EXEC (@query)

Error message is
"The data types nvarchar(max) and varbinary(max) are incompatible in the add operator."
Any help?
Thanks

What I have tried:

I tried using parameter query and execute it with sp_executesql but no luck. used sp_executeSQL and @params
Posted
Updated 14-Mar-17 3:57am
Comments
CHill60 14-Mar-17 9:51am    
What about the error before that: "Msg 257, Level 16, State 3, Line 2
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."

First fix the first problem:
Quote:
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."
I did this like this:
SQL
SET @Oldlogo = convert(varbinary,'some binary code')
The problem you quote in your question is caused by :
SQL
SET @query = @query + '  IF EXISTS(SELECT 1 FROM  Company WHERE CONVERT(varbinary,Company_Logo_TopLeft) = CONVERT(varbinary,''' + @Oldlogo + ''')) '

It is not clear why you are trying to insert 'some binary code' and convert it to varbinary again.
Once you've sorted out what you are actually trying to do, replace
EXEC (@query)
with
PRINT @query
until you are completely happy that the SQL you are generating is correct.

It's a good idea to start with a specific set of data and do raw SQL statements so that you can work out how to build the dynamic SQL - i.e. don't just dive straight in
 
Share this answer
 
Comments
Schatak 15-Mar-17 7:57am    
Thanks, i understand.
When you write this:
SQL
DECLARE @Oldlogo	varbinary(max)
... CONVERT(varbinary,Company_Logo_TopLeft) = CONVERT(varbinary,''' + @Oldlogo + ''')) '

You are trying to convert a VARBINARY value (from @OldLogo) to a VARCHAR (as part of your string concatenation) and then convert it back to a VARBINARY) via the CONVERT function)

You can't convert VARBINARY to VARCHAR - that kind of testing really needs to be done in your presentation layer rather than in SQL - and concatenating the binary value into a string (even if it worked) is an invitation for SQL Injection.
Why are you trying to do that as a dynamic query?
You could compare @OldLogo and Company_Logo_TopLeft with a simple '=' without making that dynamic at all.
 
Share this answer
 
Comments
Schatak 15-Mar-17 4:46am    
Thanks for your reply.
without dynamic query its fine for me. No issues there. But with dynamic query it's a problem
Due to some reason i am making dynamic query, i will try to avoid it . Let's see

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900