Your problem is that you swap marks around your parameters in your application, so when he evaluates his search for things from the element table where itemCode is a "toy" (note the single quotes)
The string concatenation you are doing is how it might be bad to add parameters to your dynamic queries. Instead, check this:
UPDATE item SET itemImageFileName = @fileName WHERE (itemCode = @itemCode5 ) AND (itemCheckDigit = @itemCkDigit) AND (itemSuffix IS NULL);
To handle optional search parameters, this article by Bill Graziano is excellent: Using dynamic SQL in stored procedures . I believe this is a good balance between avoiding recompiling the query when setting the recompilation option and avoiding table scans.
Please enjoy this code. It creates a temporary table to simulate your actual item table and loads it using 8 rows of data. I am announcing some parameters that you most likely will not need, as the ado.net library will do some magic for you.
Based on the values provided for the first 3 parameters, you will get an equivalent match for the row in the table and update the file name value. In my example, you will see that the entire NULL string will have the file name changed from f07.bar to f07.bar.updated.
A print statement is not required, but I put it there so you can see the query that was created to help understand the pattern.
IF NOT EXISTS (SELECT * FROM tempdb.sys.tables T WHERE T.name like '%#item%') BEGIN CREATE TABLE
Front
itemid itemCode itemCheckDigit itemSuffx itemImageFileName 1 abc X cba f00.bar 2 ac NULL ca f01.bar 3 ab x NULL f02.bar 4 a NULL NULL f03.bar 5 NULL X cba f04.bar 6 NULL NULL ca f05.bar 7 NULL x NULL f06.bar 8 NULL NULL NULL f07.bar
after
itemid itemCode itemCheckDigit itemSuffx itemImageFileName 1 abc X cba f00.bar 2 ac NULL ca f01.bar 3 ab x NULL f02.bar 4 a NULL NULL f03.bar 5 NULL X cba f04.bar 6 NULL NULL ca f05.bar 7 NULL x NULL f06.bar 8 NULL NULL NULL f07.bar.updated
source share