ColdFusion Query Error: Value Can Not Be Converted To Requested Type

Posted August 4, 2006 at 2:40 PM by Ben Nadel

Tags: ColdFusion, SQL

My friend Si was getting this error on one of our sites:

Error Executing Database Query. [Macromedia][SQLServer JDBC Driver]Value can not be converted to requested type.

This error goes hand in hand with the error I have blogged about before "Error Occurred While Processing Request N >= N" and is, in fact, caused by the same thing. The problem is inevitably SQL SELECT statements that use (*) such as:

  • SELECT
  • *
  • FROM
  • [table]

I am not 100% sure why this takes place, but here's my version: ColdFusion caches the SQL statement (or SQL does the caching). Then you go and you change the database structure such as adding a column. Then you run the query again. Since the actually SQL string didn't change, SQL thinks nothing has changed and tries to run the same query. However, since a column was added to the database, the cached structure not longer maps properly to the database structure, which is why it thinks it has to convert data types.

THIS IS WHY SELECT * IS NOOOO GOOD.

I can't remember off hand, but I think this is made even worse if you use CFQueryParam and SELECT *. If you spell out your query column instead of using (*), you are alllll good.



Reader Comments

Apr 12, 2007 at 4:20 PM // reply »
17 Comments

I get this error occasionally and I use * in my select statements because the SQL server selects fields and does all the joins for me. I also use cfqueryparam and it seems to have a problem with it. if I remove the cfqueryparam and just put #Arguments.value# it works, but of course that's no good. your post helped me figure out what was going wrong. It seems like if I restart the coldfusion application service, the problem goes away and in a development environment, that's ok.


Aug 28, 2007 at 8:30 PM // reply »
1 Comments

I know this is an old post, but I just wanted to say that this post has saved my life. i was tearing my hair out with this error


Aug 29, 2007 at 7:14 AM // reply »
11,238 Comments

Always glad to help save some time (and hair) :)


Oct 17, 2007 at 7:29 AM // reply »
2 Comments

To circumvent that CF caches select * queries our DBmanager came up with a clever trick.

Instead of writing "select * from tblcustomer", then type "select c.* from tblcustomer as c"

CF is stupid enough to not understand c.* so it dosen't cache it.

However SQL server is smart enough to understand that * and c.* is identical statements and therefore SQL server will cache it (caching it the way it should be cached i might add)

That way you dont mess up with SQL servers caching (which is a good thing)


Oct 17, 2007 at 7:33 AM // reply »
11,238 Comments

@Johansen,

Cool tip. When I see people get this error, I try to advocate them writing out the explicit column requests, but this might prove to be the quicker fix.


Nov 2, 2007 at 8:14 AM // reply »
21 Comments

Fantastic Tip - popped up in Google as link number 2 when searching for "Value Can Not Be Converted To Requested Type" and was exactly what what was wrong... I can't even begin to think how long it would have taken me to figure this out on my own. I really hate going into the lowdown nitty gritty of how CF, JDBC and MSSQL interact and as a fellow lazy programmer this tip solved my problem in 5min!

Respect!


Nov 2, 2007 at 8:23 AM // reply »
11,238 Comments

Glad to have helped :)


Dec 31, 2007 at 3:53 PM // reply »
1 Comments

Wow. Thank you very much. This post probably saved me 3 hours worth of debugging


Mar 21, 2008 at 1:08 PM // reply »
1 Comments

I was getting this error when using cfqueryparam to pass a value to a stored proc; restarting the coldfusion application service solved my problem.


Mar 21, 2008 at 1:21 PM // reply »
11,238 Comments

@Carole,

That will work since it will flush the cached database structure. Are you using SELECT * in your queries?


Jun 27, 2008 at 5:55 AM // reply »
2 Comments

Thanks for blogging about this error Ben. It seems like there are all sorts of database caching issues caused by cfqueryparam.

I was only able to resolve this issue by a restart of the CF service after changing the db structure. It's interesting to note that I'm not using * in my SELECT query where CF throws an error, yet it still seems to cache the structure.


May 28, 2009 at 5:42 PM // reply »
7 Comments

Great answer! I have one client using Fusebox so my error messages were obscure at best. I would have AN error after a db change, and figured it was some kind of caching. The only fix I could come up with was to restart ColdFusion service (not great, I know). So I got the same error on another app (no Fusebox) and could see the actual error message. I started to replace the * based on your recommendation, then had an idea. I'm still using ColdFusion studio, so I refreshed the database in the db tab and it fixed the error. Still, I agree that the * is not great.

Thanks!


May 28, 2009 at 5:49 PM // reply »
7 Comments

The error just came back. I guess I was wrong about the CF Studio fix.


Jun 1, 2009 at 1:28 PM // reply »
11,238 Comments

@Cheryl,

You're saying you're still getting the error on non-select-* queries?


Jun 1, 2009 at 1:48 PM // reply »
7 Comments

No. I'm saying that I could swear the error went away when I refreshed the datasource through CFStudio. Then I posted my comment, then I went back and refreshed the page and the error returned. So I went in and corrected the query and the error went away for good. I really appreciate the insight on this.


Jun 1, 2009 at 1:49 PM // reply »
11,238 Comments

@Cheryl,

Ah, ok. As long as it's gone now :)


Jul 24, 2009 at 7:30 PM // reply »
2 Comments

Yup. Your post saved me time!
Everything was working fine then WHAT Happened??? Lucky for me I didn't change too much (except for that deleted column).
Narrowed down my problem to the CFQUERYPARAM usage and then googled the error.

Thank you!


Jul 27, 2009 at 8:47 AM // reply »
11,238 Comments

@Stephanie,

Glad to help :)


Aug 26, 2009 at 1:22 PM // reply »
1 Comments

Replacing the "select *" with the column names worked like a charm. Thanks!


Sep 2, 2009 at 10:08 AM // reply »
11,238 Comments

@Sharon,

Awesome!


Sep 18, 2009 at 12:39 PM // reply »
3 Comments

Three questions about this issue:

1. Does this error occur when you query a CF query object or only when you query a DB table?

2. I assume this error occurs when you query views as well as tables.

3. Is this problem solely the byproduct of a "Select *" (i.e., if you explicitly reference columns in the select statement, no problem).

The reason I ask about item 3 is that Craig McDonald said in his post above that he wasn't using "Select *" and was still experiencing this problem.

BTW, this is my first posting here but I want to join others in thanking Ben for hosting this great discussion, as well as others on his site.


Sep 21, 2009 at 8:10 AM // reply »
11,238 Comments

@Mike,

The problem comes from the query caching that ColdFusion is performing. I assume that this happens if you hit a database table OR a view.

As far as I have personally experienced, as long as you are not using "select *", then you should not experience this error as ColdFusion will not try to execute a cached structure that does NOT match your given query.


Nov 20, 2009 at 10:02 AM // reply »
2 Comments

Doing a select c.* totally fixed this issue for me! Thanks


Nov 20, 2009 at 2:37 PM // reply »
3 Comments

The aliasing (e.g., "select c.* from tblcustomer as c") has also worked in my case.

While I don't like to do a "select *", I have one table with 150 fields (a related view of that table has even more) and I use all of them in one of my apps, so listing all the individual fields in the query would be a pain and really futz up the code.

Thanks again everyone.


Nov 20, 2009 at 2:45 PM // reply »
11,238 Comments

@Roman, @Mike,

Interesting; I wonder why aliasing the table makes a difference.

@Mike,

150 columns! Bananas :)


Nov 20, 2009 at 3:03 PM // reply »
7 Comments

@Mike,

I resisted the long select statements too, but finally I started using CFCs so it only has to be done once (if queries are written flexibly). <cfinvoke>s are nice and clean in your code. I'm glad I made the switch.

Using ColdFusion Builder, double clicking on the field name places it at the cursor. That combined with a ", c." on the clip board and you've got yourself a proper query in no-time. : )


Nov 28, 2009 at 11:09 AM // reply »
3 Comments

An old post which won't die! :-) Obviously still an irritation to many of us.

Here are my observations:

1) This effects all (inc. non-cached) cfquery's.

2) Replacing the * with column names works.

3) Aliasing the * (e.g. a.*) only works if it was not previously aliased (for me). So if you already had a.* in the query, then deleted a column in table a, the aliasing doesn't protect you from seeing this error.

4) Late breaking news - rebooting the ColdFusion server seems to clear the problem!

My thinking is that table structures used by cfquery are cached somehow. Make a change on the SQL side, the query may fail. Make a change in the cfquery, it is then re-cached, so the query works again. Rebooting of course also clears the cache. Just a hypothesis!

Hope this helps - keep up the good work, Ben!


Tri
Apr 1, 2010 at 11:12 AM // reply »
1 Comments

You can also clear the cache by ticking "Disable Connections" under the data source's Advance Settings in CF admin. Save. Then untick. Then save again.


Apr 1, 2010 at 11:14 AM // reply »
11,238 Comments

@Tri,

I've definitely used that a few times, especially before I realized that "select *" was the culprit.


Nov 10, 2010 at 5:24 PM // reply »
5 Comments

Does anybody know why this error is appearing for us now when it never did in the past? Are we caching queries when we shouldn't be? Is this something new to CF9?

Commenting on Oxide said...

I had the following select statement that was still throwing this error. So, I don't think that is a definite fix.

SELECT UI.salutation, UI.firstname, UI.lastname, UI.email, UI.nickname, UI.photo, UA.*, S.state_abbr,


Nov 13, 2010 at 11:58 AM // reply »
11,238 Comments

@Josh,

When in doubt, remove the ".*". Pretty much the only place I would ever use * in a SELECT statement is inside a query-of-queries.

As far as why this is happening now and not before, there might be a change in the underlying caching algorithms. Not sure.


Jan 9, 2011 at 8:18 PM // reply »
9 Comments

Thanks for this post, very helpful.

I was looking for a way to flush this DB/query cache. It looks like the way to do it is 'suspend all client connections' on the datasource.
Then re-run your query, then re-enable connections. Maybe.


May 24, 2011 at 3:36 PM // reply »
10 Comments

Just ran into this issue with some legacy code. Thanks for getting this out there for us, Ben.


Jul 25, 2011 at 10:28 AM // reply »
1 Comments

Thanks for the post, it was helpful.

I would like to point the fact that a * in the query as nothing to do with the problem itself. I had a query with no * in it and had this problem. Cf version was 9,0,0,251028. Changed an int field to a varchar(255).


Aug 25, 2011 at 10:39 AM // reply »
1 Comments

I hit this on CF9, but I am explicitly naming all the columns instead of doing a select *.

We are using SQL Server and have a lot of our columns as bit flags. I changed one of the views to do a case statement to translate these to Yes/No on the request of a client who didn't like seeing the 1/0.

Since the datatype is now a string and not a bit, it must not have matched the structure and would throw an error.


Jan 10, 2012 at 8:20 PM // reply »
17 Comments

Thanks, saved me yet again.


May 30, 2012 at 12:47 PM // reply »
2 Comments

Just received this same error but with named columns. The problem was an empty space ' ' after the last column (ex: desc as description' '). Took a few minutes to spot that. Might save somebody some troubles.


AcS
Aug 27, 2012 at 2:54 PM // reply »
1 Comments

If you get this error and add a space to the query that does the (select *) the error will go away. If you undo you space there error will come back. Until you restart the server. Something figured out the hard way. I found this post after alot of debugging. Ugh....


Aug 29, 2012 at 8:56 PM // reply »
63 Comments

I've had some issues using select * ... and no longer do it ...

select t1.* seems to work for me though ...



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools