Ciarán Reilly
Ciarán Reilly

Reputation: 15

The multi part identifier ... could not be bound [37000] - SQL 2005 query

I realise this is quite a common one, but I've been unable to rectify it so far after going through the similar issues listed on this site and elsewhere.

Anyway, I have a PHP based app in a forum which allows users to give reputation points to each other, I'm sure you're all aware how those simple setups work so I won't dwell on it. The issue is, this bit of code was originally written for MySQL I believe, and while 95% of it has been converted to, and now works, in MS SQL, there remain some queries which seem to crap out on SQL 2005, I suspect due to slight syntax inconsistencies in joins etc. This one has me stuck. The sample query is as follows:

SELECT TOP 25 p.post_id, p.post_subject, p.forum_id, u.username, u.user_id, 
 u.user_colour, r.rep_id, r.rep_from, r.rep_to, r.rep_time, r.rep_post_id, 
 r.rep_point, r.rep_comment, r.enable_urls, r.rep_ip_address, r.username 
FROM forum_reputations r, forum_users u 
LEFT JOIN forum_posts p ON (r.rep_post_id = p.post_id) 
WHERE r.rep_to = 61
ORDER BY r.rep_id DESC

The data is as follows:

**Forum_Posts**:
post_id (int, PK)
post_subject (varchar)
forum_id (int)

**Forum_users**:
username (varchar)
user_id (int, PK)
user_colour (varchar)

**Forum_reputations**:
rep_id (int, PK)
rep_from (int) 
rep_to (int)
rep_time (int)
rep_post_id (int)
rep_point (int)
rep_comment (text)
enable_urls (int) 
rep_ip_address (varchar) 
username (varchar)

When the query runs, SQL Server spits out the following, familiar error:

[Microsoft][ODBC SQL Server Driver][SQL Server]The multi-part identifier "r.rep_post_id" could not be bound. [37000]

I'm struggling to understand why this is happening. Its obviously insinuating that rep_post_id has a problem but I've checked the query syntax and column / table names and they're all correct, I even tried enclosing any objects that sounded remotely like SQL reserved words in square brackets (i.e u.[username], r.[username] etc), but it makes no difference. Interestingly if I change the FROM clause so Forum_users u is before Forum_users r, the error no longer occurs, and data is returned, but this isn't much use because it ruins the Left Join and pulls in the top 25 from the users table, ignoring whether they have any associated entries in the reputations table.

The output is supposed to comprise of a table listing the following for each rep entry for a user (r.rep_to):

Number of rep points given (r.rep_point)

User who gave the points (r.rep_from, u.user_id, u.username)

Date points were given (r.rep_time)

Comment entered by user giving the rep points (r.rep_comment)

Forum post that rep was given for (r.rep_post_id, p.post_id, p.post_subject)

If anyone has any thoughts on where this is going wrong or how it can be restructured, I'd greatly appreciate hearing about it. I realise this is probably something very rudimentary but I really can't see the wood for the trees here!

Thanks

Upvotes: 0

Views: 4340

Answers (2)

Ed Harper
Ed Harper

Reputation: 21505

try

SELECT TOP 25 p.post_id, p.post_subject, p.forum_id, u.username, u.user_id, 
 u.user_colour, r.rep_id, r.rep_from, r.rep_to, r.rep_time, r.rep_post_id, 
 r.rep_point, r.rep_comment, r.enable_urls, r.rep_ip_address, r.username 
FROM forum_reputations r
JOIN forum_users u 
ON u.user_id = r.rep_from
LEFT JOIN forum_posts p ON r.rep_post_id = p.post_id 
WHERE r.rep_to = 61
ORDER BY r.rep_id DESC

It's not directly relevant to your question, but a join condition between forum_users and forum_reputation is missing from your query - this will be producing a Cartesian product. I have added a join above, but am guessing a bit as to whether it's on the right column.

Upvotes: 1

HLGEM
HLGEM

Reputation: 96570

You cannot combine implicit and explicit joins and expect it to work correctly. You must make all the joins explicit which you should be doing in any event as implicit joins are a SQL antipattern.

This is the explict join version of what you have:

SELECT TOP 25 p.post_id, p.post_subject, p.forum_id, u.username, u.user_id,
u.user_colour, r.rep_id, r.rep_from, r.rep_to, r.rep_time, r.rep_post_id,  
r.rep_point, r.rep_comment, r.enable_urls, r.rep_ip_address, r.username  
FROM forum_reputations r
CROSS JOIN forum_users u  
LEFT JOIN forum_posts p ON r.rep_post_id = p.post_id  
WHERE r.rep_to = 61 
ORDER BY r.rep_id DESC 

However, I suspect it is NOT what you want. I think @EdHarper has the right idea.

Upvotes: 0

Related Questions