我正在运行我正在处理的存储过程中可怕的“多部分标识符无法绑定”错误。关于以下查询,我有几个问题。
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;
答案 0 :(得分:7)
请重新检查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