Question : The multi-part identifier  could not be bound and Ambiguous column name in SQL Server 2008

Hi all I have been working with this for two days. I have checked the column names, created aliases and nothing seems to work.  I used the same code for a similar query and it works fine. I don't understand.  The query works as long as I dot use Inventory_ID as the sortExpression.I continue to get the error messages :

Ambiguous column name 'Inventory_ID'.
The multi-part identifier "dealership_Inventory.Inventory_ID" could not be bound.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0

Can anyone please help?

Thanks,

Jus
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
This doesnt work:

ALTER PROCEDURE [dbo].[dealership_Inventory_PagedAndSorted]
	(
	@sortExpression nvarchar(100),
	@startRowIndex int,
	@maximumRows int
	)	
	AS	
	
	IF LEN(@sortExpression) = 0
	SET @sortExpression = 'InventoryYear'
	
DECLARE @sql nvarchar (4000)
SET @sql = 'SELECT dealership_Inventory.Inventory_ID, InventoryYear, ImageLocation
FROM (SELECT dealership_Inventory.Inventory_ID, InventoryYear, ImageLocation,
            ROW_NUMBER() OVER (ORDER BY ' + @sortExpression + ') AS RowRank
FROM dealership_Inventory
INNER JOIN dealership_InventoryImages
ON (dealership_Inventory.Inventory_ID = dealership_InventoryImages.Inventory_ID)
) AS ModelsWithRowNumbers
WHERE RowRank >' + CONVERT(nvarchar(10), @startRowIndex) +
' AND RowRank <= (' + CONVERT(nvarchar(10), @startRowIndex) + ' + '
+ CONVERT(nvarchar(10), @maximumRows) + ')'

EXEC sp_executesql @sql

Answer : The multi-part identifier  could not be bound and Ambiguous column name in SQL Server 2008

>When I use dealership.Inventory_ID as opposed to Inventory_ID as the sortExpression it works.
exactly.
as you have the @sortExpression inside the inner query, Inventory_ID as such is "ambigeous", it could be either table from the join (even if YOU know that the value is the same, SQL "cannot" know at that time), hence the error.

Random Solutions  
 
programming4us programming4us