Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Clause: The multi-part identifier could not be bound

I am running in to the dreaded "The multi-part identifier could not be bound" error on a stored procedure I am currently working on. I have a few questions in regards to the query below.

  1. Why am I getting this error?
  2. Why would this error occur on ImportFundingDateTime instead of FloorplanId given that they both come from the same query, but FloorplanId is listed first in the output clause?
  3. Can I adjust this query to not get the error while still keeping the general structure the same?

.

DECLARE @Results                Table(
    [FloorPlanId]               UNIQUEIDENTIFIER,
    [ImportFundingDateTime]     DATETIME,
    [TimeStamp]                 VARBINARY(8),
    [BusinessId]                UNIQUEIDENTIFIER
    )

UPDATE CacRecord 
    SET MatchFound = 1
    OUTPUT  fp.[FloorplanId], cr.[ImportFundingDateTime],
            fp.[TimeStamp], buyer.[BusinessId]
    INTO @Results(  [FloorplanId], [ImportFundingDateTime], 
                    [TimeStamp], [BusinessId])
    FROM CacRecord cr WITH (NOLOCK)
    INNER JOIN CacBatch cb WITH (NOLOCK)
        ON cr.CacBatchId = cb.CacBatchId
    INNER JOIN Floorplan fp WITH (NOLOCK)
        ON fp.UnitVIN = cr.ImportVin
        AND COALESCE(fp.UnitVIN, '') <> ''
    INNER JOIN Business buyer WITH (NOLOCK)
        ON buyer.BusinessId = fp.BuyerBusinessId
    LEFT OUTER JOIN BusinessContact bc WITH (NOLOCK)
        ON bc.BusinessId = buyer.BusinessId
    LEFT OUTER JOIN Contact c WITH (NOLOCK)
        ON c.ContactId = bc.ContactId
    WHERE cb.CacJobInstanceId = @cacJobInstanceId
        AND fp.FloorplanStatusId = 1 --Approved
        AND COALESCE(cr.ImportVin, '') <> ''
        AND 1 = 
            CASE
                WHEN cr.ImportFein = buyer.FederalTaxID  
                    AND COALESCE(cr.ImportFein, '') <> '' THEN 1
                WHEN cr.ImportSsn = c.Ssn 
                    AND COALESCE(cr.ImportSsn, '') <> '' THEN 1
                ELSE 0
            END;
like image 337
Phillip Benages Avatar asked Jan 31 '11 20:01

Phillip Benages


People also ask

How do I fix the multi-part identifier could not be bound in SQL?

If you get an error telling you that the “The multi-part identifier could not be bound.”, it usually means that you're prefixing one or more columns with either a table that isn't included in your query, or an alias that you haven't actually assigned to a table.

What is multipart identifier?

A multipart identifier is any description of a field or table that contains multiple parts - for instance MyTable. SomeRow - if it can't be bound that means there's something wrong with it - either you've got a simple typo, or a confusion between table and column.

Could not be bound 4104?

Error 4104 indicates that the specified multi-part identifier could not be mapped to an existing entity. This error can be returned under the following conditions: The qualifier supplied as a prefix for a column name does not correspond to any table or alias name used in the query.


1 Answers

Please recheck the syntax of the OUTPUT clause OUTPUT on MSDN

Syntax

<column_name> ::=
{ DELETED | INSERTED | from_table_name } . { * | column_name }

from_table_name

Is a column prefix that specifies a table included in the FROM clause
of a DELETE or UPDATE statement that is used tospecify the rows to
update or delete.

It looks like you have aliased CacRecord in the FROM clause as "cr", but have not correlated that with the UPDATE clause.

Note: Even with it aliases in the FROM clause and NOT aliased in the UPDATE cause, SQL Server appears to recognize CacRecord as the UPDATE table, requiring you to use INSERTED instead of cr as the virtual table name.

UPDATE cr
SET MatchFound = 1
OUTPUT fp.[FloorplanId], INSERTED.[ImportFundingDateTime],
  fp.[TimeStamp], buyer.[BusinessId]
INTO @Results( [FloorplanId], [ImportFundingDateTime], 
    [TimeStamp], [BusinessId])
FROM CacRecord cr WITH (NOLOCK)
INNER JOIN CacBatch cb WITH (NOLOCK)
 ON cr.CacBatchId = cb.CacBatchId
INNER JOIN Floorplan fp WITH (NOLOCK)
 ON fp.UnitVIN = cr.ImportVin
 AND COALESCE(fp.UnitVIN, '') <> ''
INNER JOIN Business buyer WITH (NOLOCK)
 ON buyer.BusinessId = fp.BuyerBusinessId
LEFT OUTER JOIN BusinessContact bc WITH (NOLOCK)
 ON bc.BusinessId = buyer.BusinessId
LEFT OUTER JOIN Contact c WITH (NOLOCK)
 ON c.ContactId = bc.ContactId
WHERE cb.CacJobInstanceId = @cacJobInstanceId
 AND fp.FloorplanStatusId = 1 --Approved
 AND COALESCE(cr.ImportVin, '') <> ''
 AND 1 = 
  CASE
   WHEN cr.ImportFein = buyer.FederalTaxID  
    AND COALESCE(cr.ImportFein, '') <> '' THEN 1
   WHEN cr.ImportSsn = c.Ssn 
    AND COALESCE(cr.ImportSsn, '') <> '' THEN 1
   ELSE 0
  END;

For visitors to this question, this code block shows multiple tables being referenced in the OUTPUT clause correctly.

create table TO1 (id int, a int);
create table TO2 (id int, b int);
create table TO3 (id int, c int);
insert into TO1 select 1,1;
insert into TO2 select 1,2;
insert into TO3 select 1,3;
insert into TO3 select 1,4;

declare @catch table (a int, b int, c int)
update c
set c = a.a
output a.a, b.b, INSERTED.c
into @catch(a,b,c)
from TO1 a
inner join TO2 b on a.id=b.id
inner join TO3 c on a.id=c.id
like image 194
RichardTheKiwi Avatar answered Sep 19 '22 23:09

RichardTheKiwi