Friday, March 18, 2011

ColdFusion queries and SQL comment

Here's one problem I experienced using a query in ColdFusion which was working perfectly in SQL server manager, but refused to work in ColdFusion.
The query was something like:

SELECT * --some comment
FROM users
WHERE user_id = 1

The query would only work if the comment is removed. The source of the problem is that ColdFusion removes all the line breaks in a query before executing. In this case only "SELECT *" would be executed which results (luckily) in a SQL error.
Good to know this before I tried the following query:

DELETE
FROM users --some comment
WHERE user_id = 1

Note: Normally you would use a SQL param and replace it with queryService.addParam(), this would result in an error because the param could not be found.

To work around this problem use /* and */ for SQL comment, or strip all your comments in SQL if you execute it through ColdFusion.

No comments:

Post a Comment