views:

673

answers:

5

I'm getting the error:

 the multi-part identifier "IC.industry" could not be bound

when making this SQL query from a JSP page via JDBC:

select C.company, C.shname, C.fullname, count(d_to_c.designer)
from companies C
     left join ind_to_c IC on C.company = IC.company
     left join d_to_c on C.company= d_to_c.company
where IC.industry = ?
group by C.company, C.shname, C.fullname
order by C.shname

and I'm trying to run it as a prepared statement, where I'm setting the parameter via (for example) stmt.setObject(1, 7) prior to running stmt.executeQuery().

Now, what's weird is: If I execute this with the ? and set the parameter as I just mentioned, I get the "could not be bound" error. If, however, I just change the query and hardcode the number 7 into the text of the query, it works!

So it has something to do with binding that parameter.

But I can't seem to figure out what.

Anybody?

UPDATE: Per request, the table definition for ind_to_c:

 industry - int(11)
 company - int(11)

(it's just a table that defines the m2m relationship between industries and companies)

UPDATE 2: Also per request, the full JSP code. I had to pull this out of a call to an abstraction of the database connection (which we use to store prepared statements, etc.

// conn has been initialized as the db connection object.

int parent_id = 7;
PreparedStatement ps = conn.prepareStatement("select C.company, C.shname, C.fullname, count(d_to_c.designer) from companies C left join ind_to_c IC on C.company = IC.company left join d_to_c on C.company = d_to_c.company where IC.industry = ? group by C.company, C.shname, C.fullname order by C.shname");
ps.setObject(1, parent_id);
ResultSet rs = null;
rs = ps.executeQuery();
A: 

Have you tried passing a named parameter (i.e: @industry) instead of a question mark?

Sam
No -- how does one do that in the context of JSP/JDBC? I tried just replacing the question mark placeholder with "@industry", and it appeared to not recognize it as a placeholder at all.
DanM
+2  A: 

What's the data type for industry? Does it make a difference if you use the type specific bind methods like stmt.setInt(1,7) instead?

edit: also, not related to the question, but you should probably remove C.cid from the SELECT. Some variants of T-SQL will infer that you want to group by that column since it is not the subject of an aggregation function, even though you don't specifiy it in the GROUP BY clause.

Back on topic, can you post the table definition for ind_2_c? The nature of the error would seem to indicate that it has no column called industry.

ninesided
actually, I can't test that -- have to operate through a framework (looong story) that only gives me access to setObject. Yes, I realize that's silly. Anyway, though, I need to use setObject.
DanM
(Industry is an int(11), by the way)
DanM
C.cid was a typo, as was ind_2_c (should have read ind_to_c). I renamed some of the columns/tables for purposes of this post, hence the typos. Also, posted the table def.
DanM
aah that makes more sense, also rather than tidying up the code for our benefit, can you just copy and paste the code for creating the prepared statement?
ninesided
A: 

Maybe I am just thinking to simple here because i do not know JSP to well but would dit not just work:

int parent_id = 7;
PreparedStatement ps = conn.prepareStatement("select C.company, C.shname, C.fullname, count(d_to_c.designer) from companies C left join ind_to_c IC on C.company = IC.company left join d_to_c on C.company = d_to_c.company where IC.industry = ? group by C.company, C.shname, C.fullname order by C.shname");

ps.setInt(1, parent_id );

ResultSet rs = null;
rs = ps.executeQuery();
Coentje
A: 

I see from your comments that you can only use SetObject.

But why do you pass an object array instead of a single object? (if I read Java correctly)

devio
I don't, that was a typo... I'm pulling all of this code out of an object structure we use to store prepared stmts and do some other high-level stuff... and the way we pass params to that framework is via an object array. Accidentally left it as an array instead of a single object. Oops!
DanM
(that is to say, the code you see now in the question is the correct code, and it does still have the same problem.)
DanM
A: 

I upgraded to the newer v. 2.0 of the MS-SQL JDBC driver, and magically it worked.

Sigh.

DanM