5

我在当前正在处理的存储过程中遇到了可怕的“无法绑定多部分标识符”错误。关于下面的查询,我有几个问题。

  1. 为什么我会收到此错误?
  2. 为什么会在 ImportFundingDateTime 而不是 FloorplanId 上发生此错误,因为它们都来自同一个查询,但 FloorplanId 在输出子句中首先列出?
  3. 我可以调整此查询以在保持总体结构不变的同时不出现错误吗?

.

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;
4

1 回答 1

8

请重新检查MSDN 上OUTPUT 子句 OUTPUT 的语法

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.

看起来您在 FROM 子句中将 CacRecord 别名为“cr”,但没有将其与 UPDATE 子句相关联。

注意:即使它在 FROM 子句中有别名,而在 UPDATE 原因中没有别名,SQL Server 似乎将 CacRecord 识别为 UPDATE 表,要求您使用INSERTED而不是cr作为虚拟表名。

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;

对于此问题的访问者,此代码块显示在 OUTPUT 子句中正确引用了多个表。

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
于 2011-01-31T20:18:37.367 回答