[英]Resolve the error: The multi-part identifier could not be bound?
I am creating a stored procedure and the final "When not matched" statement is throwing an error for the tmp.DelDate
and tmp.DelUser
fields.我正在创建一个存储过程,最后的“当不匹配时”语句为
tmp.DelDate
和tmp.DelUser
字段抛出错误。 The "tmp" table is a User-Defined Table Type and the definition is below the sp code. “tmp”表是用户定义的表类型,定义在 sp 代码下方。 99 times out of 100, the problem is a bad alias or other typo.
100 次中有 99 次,问题是错误的别名或其他拼写错误。 I've been staring at this and I have to be missing something small.
我一直在盯着这个,我必须错过一些小东西。 This last statement is almost identical to the first "When Matched" statement.
最后一条语句与第一个“匹配时”语句几乎相同。
ALTER Procedure dbo.spInsertUpdateProtocolRiskStrats
@riskStratsTable ProtocolRiskStrats READONLY
WITH RECOMPILE
AS
BEGIN
WITH riskStrats as (
SELECT ol.StratId,
ol.LinkType,
ol.LinkId,
ol.add_user,
ol.add_date,
ol.del_user,
ol.del_date
FROM ots_StratTriggerOutcomesLinks ol
JOIN @riskStratsTable rst on ol.LinkId = rst.LinkId
WHERE ol.LinkId = rst.LinkId
AND ol.LinkType = 2
)
MERGE riskStrats
USING @riskStratsTable as tmp
ON riskStrats.LinkId = tmp.LinkId
WHEN MATCHED THEN
UPDATE SET riskStrats.add_date = tmp.AddDate,
riskStrats.add_user = tmp.AddUser,
del_date = null,
del_user= null
WHEN NOT MATCHED THEN
INSERT (StratId, LinkType, LinkId, add_user, add_date)
VALUES (tmp.StratId, tmp.LinkType, tmp.LinkId, tmp.AddUser, tmp.AddDate)
WHEN NOT MATCHED BY SOURCE THEN
UPDATE SET riskStrats.del_date = tmp.DelDate,
riskStrats.del_user = tmp.DelUser;
END
User Table definition用户表定义
CREATE TYPE dbo.ProtocolRiskStrats AS TABLE
(
KeyId int null,
StratId int null,
LinkType int null,
LinkId int null,
AddUser int null,
AddDate datetime null,
DelUser int null,
DelDate datetime null
)
As noted by @AlwaysLearning, I was assigning values that couldn't exist because it was a "not matched" condition.正如@AlwaysLearning 所指出的,我正在分配不存在的值,因为它是一个“不匹配”的条件。 I updated my last statement to use constant values.
我更新了上一条语句以使用常量值。 I had to add another parameter to pass in user name.
我不得不添加另一个参数来传递用户名。 I could have also done a "Top 1" on my TVP but my dev lead didn't like that.
我也可以在我的 TVP 上获得“Top 1”,但我的开发主管不喜欢那样。
UPDATE SET riskStrats.del_date = GETDATE(),
riskStrats.del_user = @userName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.