Skip to content
Advertisement

Conversion failed when converting the varchar value ‘ AND ID =’ to data type int

I was looking how I can parameterize table names and so I found dynamic sql queries. I finally got the proc saved, but when I execute it errors out with “Conversion failed when converting the varchar value ‘ AND ID =’ to data type int.” I have no idea what is going wrong when I try to execute this stored proc. I feel like it must be a typo but I cannot tell. The stored pro.

EDITED to incorporate the suggestion below. Still no luck unless I am doing this wrong.

Advertisement

Answer

You should pass the parameters all the way through to sp_executesql. Do not inject them, unless they are object names, in which case you need QUOTENAME.

You can also use OBJECT_ID to check for table existence, and throw an exception if there is no such object.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement