gpt4 book ai didi

sql - 输出子句 : The multi-part identifier could not be bound

转载 作者:行者123 更新时间:2023-12-04 13:44:32 32 4
gpt4 key购买 nike

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

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

  • .
    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;

    最佳答案

    请重新检查 OUTPUT 子句的语法 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.

    看起来您在 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

    关于sql - 输出子句 : The multi-part identifier could not be bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4855595/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com