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

Posted August 4, 2006 at 2:40 PM

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:

 Launch code in new window » Download code as text file »

  • 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

michael white
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.


Jamie
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 »
6,371 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 »
6,371 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 »
4 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 »
6,371 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


Carole
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 »
6,371 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 »
4 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 »
4 Comments

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


Jun 1, 2009 at 1:28 PM // reply »
6,371 Comments

@Cheryl,

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


Jun 1, 2009 at 1:48 PM // reply »
4 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 »
6,371 Comments

@Cheryl,

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


Stephanie
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 »
6,371 Comments

@Stephanie,

Glad to help :)


Sharon
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 »
6,371 Comments

@Sharon,

Awesome!


Mike
Sep 18, 2009 at 12:39 PM // reply »
1 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 »
6,371 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.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »