9

Here is my code:

var qryStr = "
            UPDATE templates_email 
            SET title = :title, test_emails = :testEmail, body = :body
            WHERE id = :templateID";

q = New Query();
q.setSQL(qryStr);
q.addParam(name="title", value="#arguments.title#", cfsqltype="cf_sql_char");
q.addParam(name="body", value="#arguments.templateContent#", cfsqltype="cf_sql_char");
q.addParam(name="testEmail", value="#arguments.test_emails#", cfsqltype="cf_sql_char");
q.addParam(name="templateID", value="#arguments.id#", cfsqltype="cf_sql_integer");

return q.execute().getResult();

This is the error:

Parameter 'body WHERE' not found in the list of parameters specified

SQL: UPDATE templates_email SET title = :title, test_emails = :testEmail, body = :body WHERE id = :templateID

The error occurred in C:\ColdFusion9\CustomTags\com\adobe\coldfusion\query.cfc: line 108

I can only assume I have done something wrong with the way my SQL is structured with the parameters, but can't work out what it is. Can anyone see what I am doing wrong here?

2
  • 1
    Have you tried switching around the order of the columns, so that "body" is not the last one? If so, did you get a similar error, but this time with the new, last column? Dec 8, 2011 at 6:30
  • Hi Jake..yes, tried that, and did get the same error on the column that had been placed at the end. Marks explanation below explains very accurately what the problem was.
    – Jason
    Dec 9, 2011 at 11:28

3 Answers 3

14

The parser for getting the params doesn't tokenize on return values, only on whitespace (which is really annoying). Try the following:

var qryStr = "
    UPDATE templates_email 
    SET title = ( :title ), test_emails = ( :testEmail ), body = ( :body )
    WHERE ( id = :templateID )
";

The ( and ) should remove any issue with where the parser not being able to recognise where the :params stop and start.

3
  • Spot on! Thanks Mark! Appreciated.
    – Jason
    Dec 8, 2011 at 22:10
  • I originally missed the fact that you have spaces between the parentheses and the parameters - these are important, without them, it doesn't work. Generous spaces FTW :)
    – RaeLehman
    Oct 8, 2012 at 17:27
  • 4
    Your explanation is a little misleading. The problem is that it doesn't tokenize on whitespace, it tokenizes on space. It considers tab and newline to be part of the param names. Adding a single space character after the param name (without even using the parens) will also fix it. That said, the paren is better because someone's editor might be set to remove trailing whitespace when they touch a file.
    – Kip
    Jul 30, 2013 at 15:03
6

This error occurs because of the tab and line break characters found in your SQL statement. I normally run below function on my SQL statement to remove these characters.

string function cleanSQL(required string sqlStatement)
    output="false"
{
return trim(reReplace(arguments.sqlStatement, "\t|\n", " ", "all"));
}

So, your setSQL() can look like:

q.setSQL(cleanSQL(qryStr))

or simply:

q.setSQL(reReplace(qryStr, "\t|\n", " ", "all"))
0

ColdFusion can get confused when parsing the SQL string to use parameters. The easiest way to solve this shortcoming is to put a space after each of your parameters.

Since some text editors may remove trailing whitespace, like when saving, I include an empty comment after any space at the end of a line.

var qryStr = "
    UPDATE templates_email 
    SET title = :title , test_emails = :testEmail , body = :body /**/
    WHERE id = :templateID /**/
";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.